import { __ } from "@wordpress/i18n";
import {
	useBlockProps,
	InnerBlocks,
	useSetting,
	InspectorControls,
	MediaReplaceFlow,
	MediaPlaceholder,
} from "@wordpress/block-editor";
import "./editor.scss";
import { isColorLight } from "../../_utilities/utilities";

import {
	PanelBody,
	__experimentalToggleGroupControl as ToggleGroupControl,
	__experimentalToggleGroupControlOption as ToggleGroupControlOption,
	Button,
	CheckboxControl,
	ColorPalette,
} from "@wordpress/components";

export default function Edit({ attributes, setAttributes, ...props }) {
	const colors = useSetting("color.palette.theme");
	const {
		hasBackgroundColor,
		backgroundColor,
		textColor,
		hasLightBackground,
		hasBorder,
	} = attributes;

	function onBackgroundColorChange(value) {
		if (value === undefined) {
			setAttributes({ hasBackgroundColor: false });
			setAttributes({ backgroundColor: "transparent" });
		}
		const isLightBackgroundColor = isColorLight(backgroundColor);
		setAttributes({ hasLightBackground: isLightBackgroundColor });

		setAttributes({ backgroundColor: value });
	}
	function onHasBackgroundColorChange(value) {
		setAttributes({ hasBackgroundColor: value });
		if (!value) {
			setAttributes({ backgroundColor: null });
		}
	}
	function onTextColorChange(value) {
		setAttributes({ textColor: value });
	}
	return (
		<>
			<InspectorControls>
				<PanelBody
					className="deligraph-blocks-components-image__panel-body"
					title={__("Options", "carhop-blocks")}
				>
					<CheckboxControl
						label="Bordure"
						checked={hasBorder}
						onChange={(value) => setAttributes({ hasBorder: value })}
					/>
				</PanelBody>
				{/* <PanelBody
					className="deligraph-blocks-components-image__panel-body"
					title={__("Arrière plan", "deligraph-blocks")}
				>
					<CheckboxControl
						label="Arrière plan coloré"
						checked={hasBackgroundColor}
						onChange={onHasBackgroundColorChange}
					/>
					{hasBackgroundColor && (
						<>
							<ColorPalette
								colors={colors}
								value={backgroundColor}
								onChange={onBackgroundColorChange}
							/>
						</>
					)}
				</PanelBody>
				<PanelBody
					className="deligraph-blocks-components-image__panel-body"
					title={__("Couleur du texte", "deligraph-blocks")}
				>
					{hasBackgroundColor && (
						<>
							<ColorPalette
								colors={colors}
								value={textColor}
								onChange={onTextColorChange}
							/>
						</>
					)}
				</PanelBody> */}
			</InspectorControls>
			<div
				{...useBlockProps({
					className: `content-card ${
						hasLightBackground
							? "content-card--bg-light"
							: "content-card--bg-dark"
					} ${hasBorder ? "content-card--has-border" : ""}`,
					style: {
						"--content-card-text-color": textColor ?? "inherit",
						"--content-card-background-color": hasBackgroundColor
							? backgroundColor
							: "transparent",
					},
				})}
			>
				<div className="content-card__innerblocks">
					<InnerBlocks
						template={[
							["core/paragraph", { placeholder: "Ajouter ici le texte" }],
						]}
						allowedBlocks={[
							"core/heading",
							"core/paragraph",
							"core/group",
							"core/list",
							"core/button",
							"core/buttons",
							"carhop-blocks/heading",
							"carhop-blocks/cta-group",
							"carhop-blocks/cta",
						]}
					/>
				</div>
			</div>
		</>
	);
}
