import {
	createElement,
	Component,
	useEffect,
	useState,
	useContext,
	createContext,
	Fragment,
} from '@wordpress/element'
import { __ } from 'ct-i18n'
import classnames from 'classnames'
import { DemosContext } from '../../DemoInstall'
import Checkbox from '../../../../helpers/Checkbox'

import { animated } from 'blocksy-options'

export const getPluginsMap = (plugins) => ({
	gutenberg: 'Gutenberg',
	'stackable-ultimate-gutenberg-blocks': 'Stackable - Gutenberg Blocks',
	'wpforms-lite': 'WPForms - Contact Form',
	woocommerce: 'WooCommerce',
	elementor: 'Elementor',
	brizy: 'Brizy',
	getwid: 'Getwid',
	'simply-gallery-block': 'SimpLy Gallery Block & Lightbox',
	'recipe-card-blocks-by-wpzoom': 'Recipe Card Blocks by WPZOOM',
	'map-block-gutenberg': 'Map Block for Google Maps',
	'mb-custom-post-type': 'MB Custom Post Types & Custom Taxonomies',
	leadin: 'HubSpot',
	'block-slider': 'Block Slider',
	'ht-slider-for-elementor': 'HT Slider For Elementor',
	'modula-best-grid-gallery': 'Modula - Image Gallery',
	'advanced-custom-fields': 'Advanced Custom Fields (ACF)',
	'greenshift-animation-and-page-builder-blocks':
		'Greenshift – Gutenberg Blocks',
	fluentform: 'Fluent Forms',
	'translatepress-multilingual': 'TranslatePress',
	'fluent-booking': 'Fluent Booking',
})

export const getNameForPlugin = (plugin) => {
	return (getPluginsMap()[plugin] || plugin).replace(/\b\w/, (v) =>
		v.toUpperCase()
	)
}

const Plugins = ({ demoConfiguration, setDemoConfiguration, style }) => {
	const { currentDemo, demos_list, pluginsStatus, setCurrentDemo } =
		useContext(DemosContext)

	const [properDemoName, _] = (currentDemo || '').split(':')

	const demosCollection = demos_list.filter(
		({ name }) => name === properDemoName || ''
	)

	return (
		<animated.div style={style}>
			<div className="ct-demo-plugins">
				<i className="ct-demo-icon">
					<svg width="40" height="40" viewBox="0 0 40 40">
						<path
							fill="#0C7AB3"
							d="M20,0v7.6c0,0.3-0.2,0.5-0.5,0.5h-1.5c0,0,0,0,0,0c0,0,0,0-0.1,0c0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					</svg>
				</i>

				<h2>{__('Install & Activate Plugins', 'blocksy-companion')}</h2>
				<p>
					{__(
						'The following plugins are required for this starter site in order to work properly.',
						'blocksy-companion'
					)}
				</p>

				<div className="ct-checkboxes-container" data-type="reversed">
					{demoConfiguration.plugins.map(({ plugin, enabled }) => (
						<Fragment key={plugin}>
							{!pluginsStatus[plugin] && (
								<Checkbox
									key={plugin}
									checked={enabled}
									onChange={() =>
										setDemoConfiguration({
											...demoConfiguration,
											plugins:
												demoConfiguration.plugins.map(
													(demo) =>
														demo.plugin === plugin
															? {
																	...demo,
																	enabled:
																		!enabled,
															  }
															: demo
												),
										})
									}>
									{getNameForPlugin(plugin)}
								</Checkbox>
							)}

							{pluginsStatus[plugin] && (
								<Checkbox activated checked onChange={() => {}}>
									{getNameForPlugin(plugin)}
								</Checkbox>
							)}
						</Fragment>
					))}
				</div>
			</div>
		</animated.div>
	)
}

export default Plugins
