/**
 * WordPress dependencies
 */
import { __ } from "@wordpress/i18n";
import { MediaUpload } from "@wordpress/block-editor";
import {
    ToggleControl,
    TextControl,
    SelectControl,
    BaseControl,
    Button,
    __experimentalToggleGroupControl as ToggleGroupControl,
    __experimentalToggleGroupControlOption as ToggleGroupControlOption,
    __experimentalDivider as Divider,
} from "@wordpress/components";

import {
    ColorControl,
    ResponsiveDimensionsControl,
    ResponsiveRangeController,
    TypographyDropdown,
    GradientColorControl,
    ImageAvatar,
    EBIconPicker,
    InspectorPanel
} from "@essential-blocks/controls";

import {
    SEPARATOR_OPTIONS,
    LAYOUT_OPTIONS,
    MEDIA_TYPES,
    ICON_IMAGE_BG_TYPES,
    MEDIA_ALIGNMENTS_ON_FLEX_COLUMN,
    MEDIA_ALIGNMENTS_ON_FLEX_ROW,
    CONTENTS_ALIGNMENTS_ON_FLEX_ROW,
    sizeUnitTypes,
    imgHeightUnits,
    LAYOUT_TYPES,
    HEADER_TAGS,
    CONTENTS_ALIGNMENTS,
    // HOVER_EFFECT,
} from "./constants";

import {
    typoPrefix_numPrefix,
    typoPrefix_numSuffix,
    typoPrefix_number,
    typoPrefix_title,
} from "./constants/typographyPrefixConstants";

import {
    wrapperMargin,
    wrapperPadding,

    //
    mediaBgPadding,
    mediaBgMargin,
    mediaBgRadius,
} from "./constants/dimensionsConstants";
import { WrapBg } from "./constants/backgroundsConstants";
import { wrpBdShadow } from "./constants/borderShadowConstants";
import {
    rgNumTitle,
    rgNumPrefix,
    rgNumSuffix,

    //
    mediaIconSize,
    mediaImageWidth,
    mediaImageHeight,
    mediaContentGap,
} from "./constants/rangeNames";

const Inspector = (props) => {
    const { attributes, setAttributes } = props;
    const {
        // responsive control attributes ⬇
        resOption,

        // counter settings attributes ⬇
        startValue,
        target,
        counterPrefix,
        counterSuffix,
        duration,
        isShowSeparator,
        separastorSelectLabel,
        layoutLabel,

        // counter color attributes ⬇
        titleColor,
        numberColor,
        numPrefixColor,
        numSuffixColor,

        //
        //
        layoutPreset,

        //
        rootFlexDirection,
        contentAlignment,
        mediaAlignSelf,
        contentsAlignSelf,

        //
        media,
        selectedIcon,
        iconColor,
        useIconBg,
        iconBgType,
        iconBgColor,
        iconBgGradient,
        imageUrl,
        imageId,
        isMediaImgHeightAuto,
        counterTitleLevel,
        titleLevel,
    } = attributes;

    const handleSeparatorChange = (separastorSelectLabel) => {
        switch (separastorSelectLabel) {
            case "Default":
                setAttributes({ separator: "," });
                break;

            case "Dot":
                setAttributes({ separator: "." });
                break;

            case "Space":
                setAttributes({ separator: " " });
                break;
        }
        setAttributes({ separastorSelectLabel });
    };

    const handleLayoutChange = (layoutLabel) => {
        switch (layoutLabel) {
            case "Default":
                setAttributes({ wrapperFlexDirection: "column" });
                break;

            case "Reverse":
                setAttributes({ wrapperFlexDirection: "column-reverse" });
                break;
        }
        setAttributes({ layoutLabel });
    };

    const handlePresetChange = (layoutPreset) => {
        switch (layoutPreset) {
            case "preset1":
                setAttributes({
                    rootFlexDirection: "column",
                    contentAlignment: "center",
                    mediaAlignSelf: "center",
                });
                break;

            case "preset2":
                setAttributes({
                    rootFlexDirection: "column-reverse",
                    contentAlignment: "center",
                    mediaAlignSelf: "center",
                });
                break;

            case "preset3":
                setAttributes({
                    rootFlexDirection: "row",
                    contentAlignment: "left",
                    mediaAlignSelf: "flex-start",
                });
                break;

            case "preset4":
                setAttributes({
                    rootFlexDirection: "row-reverse",
                    contentAlignment: "right",
                    mediaAlignSelf: "flex-start",
                });
                break;
        }
        setAttributes({ layoutPreset });
    };

    return (
        <InspectorPanel advancedControlProps={{
            marginPrefix: wrapperMargin,
            paddingPrefix: wrapperPadding,
            backgroundPrefix: WrapBg,
            borderPrefix: wrpBdShadow,
            hasMargin: true
        }}>
            <InspectorPanel.General>
                <>
                    <InspectorPanel.PanelBody
                        initialOpen={true}
                    >
                        <ToggleGroupControl
                            label={__(
                                "Media Options",
                                "essential-blocks"
                            )}

                            value={media}
                            onChange={(value) =>
                                setAttributes({
                                    media: value,
                                })
                            }
                            isBlock
                            __next40pxDefaultSize
                            __nextHasNoMarginBottom
                        >
                            {MEDIA_TYPES.map(
                                (value, index) => (
                                    <ToggleGroupControlOption
                                        key={index}
                                        value={value}
                                        label={value}
                                    />
                                )
                            )}
                        </ToggleGroupControl>
                        {media === "icon" && (
                            <EBIconPicker
                                value={selectedIcon}
                                attributeName={'selectedIcon'}
                            />
                        )}
                        {media === "image" && (
                            <>
                                {!imageUrl && (
                                    <MediaUpload
                                        onSelect={({ id, url }) =>
                                            setAttributes({
                                                imageUrl: url,
                                                imageId: id,
                                            })
                                        }
                                        type="image"
                                        value={imageId}
                                        render={({ open }) => {
                                            return (
                                                <Button
                                                    className="eb-background-control-inspector-panel-img-btn components-button"
                                                    label={__(
                                                        "Upload Image",
                                                        "essential-blocks"
                                                    )}
                                                    icon="format-image"
                                                    onClick={open}
                                                />
                                            );
                                        }}
                                    />
                                )}
                                {imageUrl && (
                                    <ImageAvatar
                                        imageUrl={imageUrl}
                                        onDeleteImage={() =>
                                            setAttributes({
                                                imageUrl: null,
                                            })
                                        }
                                    />
                                )}
                            </>
                        )}
                    </InspectorPanel.PanelBody>

                    {media !== "none" && (
                        <>
                            <InspectorPanel.PanelBody
                                initialOpen={true}
                            >
                                <SelectControl
                                    label={__(
                                        "Layout Preset ",
                                        "essential-blocks"
                                    )}
                                    value={layoutPreset}
                                    options={LAYOUT_TYPES}
                                    onChange={
                                        handlePresetChange
                                    }
                                    __next40pxDefaultSize
                                    __nextHasNoMarginBottom
                                />

                                <ResponsiveRangeController
                                    baseLabel={__(
                                        "Media & content spacing",
                                        "Infobox"
                                    )}
                                    controlName={
                                        mediaContentGap
                                    }
                                    min={0}
                                    max={500}
                                    step={1}
                                    noUnits
                                />
                            </InspectorPanel.PanelBody>
                        </>
                    )}

                    <InspectorPanel.PanelBody
                        title={__(
                            "Counter Settings",
                            "essential-blocks"
                        )}
                        initialOpen={true}
                    >
                        <BaseControl id="eb-counter-start-value" __nextHasNoMarginBottom>
                            <TextControl
                                label={__(
                                    "Starting Number",
                                    "essential-blocks"
                                )}
                                value={startValue}
                                type="number"
                                onChange={(value) =>
                                    !/[\-\.]/.test(value) &&
                                    setAttributes({
                                        startValue: value,
                                    })
                                }
                                __next40pxDefaultSize
                                __nextHasNoMarginBottom
                            />
                        </BaseControl>
                        <BaseControl id="eb-counter-end-value" __nextHasNoMarginBottom>
                            <TextControl
                                label={__(
                                    "Ending Number",
                                    "essential-blocks"
                                )}
                                value={target}
                                type="number"
                                onChange={(value) =>
                                    !/[\-\.]/.test(value) &&
                                    setAttributes({
                                        target: value,
                                    })
                                }
                                __next40pxDefaultSize
                                __nextHasNoMarginBottom
                            />
                        </BaseControl>
                        <BaseControl id="eb-counter-duration" __nextHasNoMarginBottom>
                            <TextControl
                                label={__(
                                    "Animation Duration",
                                    "essential-blocks"
                                )}
                                value={duration}
                                type="number"
                                onChange={(value) =>
                                    !/[\-\.]/.test(value) &&
                                    setAttributes({
                                        duration: value,
                                    })
                                }
                                __next40pxDefaultSize
                                __nextHasNoMarginBottom
                            />
                        </BaseControl>
                        <BaseControl id="eb-counter-prefix" __nextHasNoMarginBottom>
                            <TextControl
                                label={__(
                                    "Number Prefix",
                                    "essential-blocks"
                                )}
                                value={counterPrefix}
                                onChange={(counterPrefix) =>
                                    setAttributes({
                                        counterPrefix,
                                    })
                                }
                                __next40pxDefaultSize
                                __nextHasNoMarginBottom
                            />
                        </BaseControl>
                        <BaseControl id="eb-counter-suffix" __nextHasNoMarginBottom>
                            <TextControl
                                label={__(
                                    "Number Suffix",
                                    "essential-blocks"
                                )}
                                value={counterSuffix}
                                onChange={(counterSuffix) =>
                                    setAttributes({
                                        counterSuffix,
                                    })
                                }
                                __next40pxDefaultSize
                                __nextHasNoMarginBottom
                            />
                        </BaseControl>

                        <SelectControl
                            label={__(
                                "Content layouts",
                                "essential-blocks"
                            )}
                            options={LAYOUT_OPTIONS}
                            value={layoutLabel}
                            onChange={(value) =>
                                handleLayoutChange(value)
                            }
                            __next40pxDefaultSize
                            __nextHasNoMarginBottom
                        />

                        <ToggleControl
                            label={__(
                                "Thousand Separator",
                                "essential-blocks"
                            )}
                            checked={isShowSeparator}
                            onChange={() =>
                                setAttributes({
                                    isShowSeparator: !isShowSeparator,
                                })
                            }
                            __nextHasNoMarginBottom
                        />

                        {isShowSeparator && (
                            <SelectControl
                                label={__(
                                    "Separator",
                                    "essential-blocks"
                                )}
                                options={SEPARATOR_OPTIONS}
                                value={separastorSelectLabel}
                                onChange={(value) =>
                                    handleSeparatorChange(value)
                                }
                                __next40pxDefaultSize
                                __nextHasNoMarginBottom
                            />
                        )}
                    </InspectorPanel.PanelBody>
                    <InspectorPanel.PanelBody initialOpen={true}>
                        <ToggleGroupControl
                            label={__(
                                "Counter Title Level",
                                "essential-blocks"
                            )}
                            className="eb-advance-heading-alignment eb-html-tag-buttongroup newtogglegroupcontrol"
                            value={counterTitleLevel}
                            onChange={(value) =>
                                setAttributes({
                                    counterTitleLevel: value,
                                })
                            }
                            isBlock
                            __next40pxDefaultSize
                            __nextHasNoMarginBottom
                        >
                            {HEADER_TAGS.map(
                                (item, key) => (
                                    <ToggleGroupControlOption
                                        key={key}
                                        value={item.value}
                                        label={item.label}
                                    />
                                )
                            )}
                        </ToggleGroupControl>
                        <Divider />
                        <ToggleGroupControl
                            label={__(
                                "Title Level",
                                "essential-blocks"
                            )}
                            className="eb-advance-heading-alignment eb-html-tag-buttongroup newtogglegroupcontrol"
                            value={titleLevel}
                            onChange={(value) =>
                                setAttributes({
                                    titleLevel: value,
                                })
                            }
                            isBlock
                            __next40pxDefaultSize
                            __nextHasNoMarginBottom
                        >
                            {HEADER_TAGS.map(
                                (item, key) => (
                                    <ToggleGroupControlOption
                                        key={key}
                                        value={item.value}
                                        label={item.label}
                                    />
                                )
                            )}
                        </ToggleGroupControl>
                    </InspectorPanel.PanelBody>
                </>
            </InspectorPanel.General>
            <InspectorPanel.Style>
                <>
                    {media !== "none" && (
                        <InspectorPanel.PanelBody
                            title={__(
                                "Media",
                                "essential-blocks"
                            )}
                            initialOpen={true}
                        >
                            {media === "icon" && (
                                <>

                                    {selectedIcon && (
                                        <ResponsiveRangeController
                                            baseLabel={__(
                                                "Icon Size",
                                                "essential-blocks"
                                            )}
                                            controlName={
                                                mediaIconSize
                                            }
                                            min={8}
                                            max={200}
                                            step={1}
                                        />
                                    )}

                                    <ColorControl
                                        label={__(
                                            "Color",
                                            "essential-blocks"
                                        )}
                                        color={iconColor}
                                        attributeName={'iconColor'}
                                    />

                                    <ResponsiveDimensionsControl
                                        controlName={
                                            mediaBgPadding
                                        }
                                        baseLabel="Padding"
                                    />

                                    <ToggleControl
                                        label={__(
                                            "Use Background",
                                            "essential-blocks"
                                        )}
                                        checked={useIconBg}
                                        onChange={() =>
                                            setAttributes({
                                                useIconBg: !useIconBg,
                                            })
                                        }
                                        __nextHasNoMarginBottom
                                    />

                                    {useIconBg && (
                                        <>
                                            <ToggleGroupControl
                                                label={__(
                                                    "Background Type",
                                                    "essential-blocks"
                                                )}

                                                value={iconBgType}
                                                onChange={(value) =>
                                                    setAttributes({
                                                        iconBgType: value,
                                                    })
                                                }
                                                isBlock
                                                __next40pxDefaultSize
                                                __nextHasNoMarginBottom
                                            >
                                                {ICON_IMAGE_BG_TYPES.map(
                                                    (
                                                        {
                                                            value,
                                                            label,
                                                        },
                                                        index
                                                    ) => (
                                                        <ToggleGroupControlOption
                                                            key={index}
                                                            value={value}
                                                            label={label}
                                                        />
                                                    )
                                                )}
                                            </ToggleGroupControl>

                                            {iconBgType ===
                                                "fill" && (
                                                    <ColorControl
                                                        label={__(
                                                            "Background Color",
                                                            "essential-blocks"
                                                        )}
                                                        color={
                                                            iconBgColor
                                                        }
                                                        attributeName={'iconBgColor'}
                                                    />
                                                )}

                                            {iconBgType ===
                                                "gradient" && (
                                                    <InspectorPanel.PanelBody
                                                        title={__(
                                                            "Gradient",
                                                            "essential-blocks"
                                                        )}
                                                    // initialOpen={false}
                                                    >
                                                        <GradientColorControl
                                                            gradientColor={
                                                                iconBgGradient
                                                            }
                                                            onChange={(
                                                                iconBgGradient
                                                            ) =>
                                                                setAttributes(
                                                                    {
                                                                        iconBgGradient,
                                                                    }
                                                                )
                                                            }
                                                        />
                                                    </InspectorPanel.PanelBody>
                                                )}
                                        </>
                                    )}
                                </>
                            )}

                            {media === "image" && imageUrl && (
                                <>
                                    <ResponsiveRangeController
                                        baseLabel={__(
                                            "Image Width",
                                            "essential-blocks"
                                        )}
                                        controlName={
                                            mediaImageWidth
                                        }
                                        units={sizeUnitTypes}
                                        min={0}
                                        max={500}
                                        step={1}
                                    />
                                    <ToggleControl
                                        label={__(
                                            "Auto Image Height",
                                            "essential-blocks"
                                        )}
                                        checked={
                                            isMediaImgHeightAuto
                                        }
                                        onChange={() =>
                                            setAttributes({
                                                isMediaImgHeightAuto: !isMediaImgHeightAuto,
                                            })
                                        }
                                        __nextHasNoMarginBottom
                                    />

                                    {!isMediaImgHeightAuto && (
                                        <>
                                            <ResponsiveRangeController
                                                baseLabel={__(
                                                    "Image Height",
                                                    "essential-blocks"
                                                )}
                                                controlName={
                                                    mediaImageHeight
                                                }
                                                units={
                                                    imgHeightUnits
                                                }
                                                min={0}
                                                max={500}
                                                step={1}
                                            />
                                        </>
                                    )}
                                </>
                            )}

                            <Divider />

                            <ResponsiveDimensionsControl
                                forBorderRadius
                                controlName={mediaBgRadius}
                                baseLabel="Border Radius"
                            />

                            <ResponsiveDimensionsControl
                                controlName={mediaBgMargin}
                                baseLabel="Margin"
                            />
                        </InspectorPanel.PanelBody>
                    )}

                    <InspectorPanel.PanelBody
                        title={__(
                            "Alignments",
                            "essential-blocks"
                        )}
                        initialOpen={true}
                    >
                        {media !== "none" && (
                            <>
                                {(rootFlexDirection === "row" ||
                                    rootFlexDirection ===
                                    "row-reverse") && (
                                        <>
                                            <ToggleGroupControl
                                                label="Media Vertical alignments"

                                                value={mediaAlignSelf}
                                                onChange={(value) =>
                                                    setAttributes({
                                                        mediaAlignSelf: value,
                                                    })
                                                }
                                                isBlock
                                                __next40pxDefaultSize
                                                __nextHasNoMarginBottom
                                            >
                                                {MEDIA_ALIGNMENTS_ON_FLEX_ROW.map(
                                                    (
                                                        {
                                                            value,
                                                            label,
                                                        },
                                                        index
                                                    ) => (
                                                        <ToggleGroupControlOption
                                                            key={index}
                                                            value={value}
                                                            label={label}
                                                        />
                                                    )
                                                )}
                                            </ToggleGroupControl>

                                            <ToggleGroupControl
                                                label="Content Vertical alignments"

                                                value={contentsAlignSelf}
                                                onChange={(value) =>
                                                    setAttributes({
                                                        contentsAlignSelf: value,
                                                    })
                                                }
                                                isBlock
                                                __next40pxDefaultSize
                                                __nextHasNoMarginBottom
                                            >
                                                {CONTENTS_ALIGNMENTS_ON_FLEX_ROW.map(
                                                    (
                                                        {
                                                            value,
                                                            label,
                                                        },
                                                        index
                                                    ) => (
                                                        <ToggleGroupControlOption
                                                            key={index}
                                                            value={value}
                                                            label={label}
                                                        />
                                                    )
                                                )}
                                            </ToggleGroupControl>
                                        </>
                                    )}

                                {(rootFlexDirection ===
                                    "column" ||
                                    rootFlexDirection ===
                                    "column-reverse") && (
                                        <ToggleGroupControl
                                            label="Media alignments"

                                            value={mediaAlignSelf}
                                            onChange={(value) =>
                                                setAttributes({
                                                    mediaAlignSelf: value,
                                                })
                                            }
                                            isBlock
                                            __next40pxDefaultSize
                                            __nextHasNoMarginBottom
                                        >
                                            {MEDIA_ALIGNMENTS_ON_FLEX_COLUMN.map(
                                                (
                                                    {
                                                        value,
                                                        label,
                                                    },
                                                    index
                                                ) => (
                                                    <ToggleGroupControlOption
                                                        key={index}
                                                        value={value}
                                                        label={label}
                                                    />
                                                )
                                            )}
                                        </ToggleGroupControl>
                                    )}
                            </>
                        )}

                        <ToggleGroupControl
                            label="Contents alignments"

                            value={contentAlignment}
                            onChange={(value) =>
                                setAttributes({
                                    contentAlignment: value,
                                })
                            }
                            isBlock
                            __next40pxDefaultSize
                            __nextHasNoMarginBottom
                        >
                            {CONTENTS_ALIGNMENTS.map(
                                (
                                    { value, label },
                                    index
                                ) => (
                                    <ToggleGroupControlOption
                                        key={index}
                                        value={value}
                                        label={label}
                                    />
                                )
                            )}
                        </ToggleGroupControl>
                    </InspectorPanel.PanelBody>

                    <InspectorPanel.PanelBody
                        title={__("Number", "essential-blocks")}
                        initialOpen={false}
                    >
                        <TypographyDropdown
                            baseLabel="Typography"
                            typographyPrefixConstant={
                                typoPrefix_number
                            }
                        />

                        <ColorControl
                            label={__(
                                "Color",
                                "essential-blocks"
                            )}
                            color={numberColor}
                            attributeName={'numberColor'}
                        />
                    </InspectorPanel.PanelBody>

                    <InspectorPanel.PanelBody
                        title={__("Title", "essential-blocks")}
                        initialOpen={false}
                    >
                        <TypographyDropdown
                            baseLabel="Typography"
                            typographyPrefixConstant={
                                typoPrefix_title
                            }
                        />

                        <ColorControl
                            label={__(
                                "Color",
                                "essential-blocks"
                            )}
                            color={titleColor}
                            attributeName={'titleColor'}
                        />
                    </InspectorPanel.PanelBody>

                    <InspectorPanel.PanelBody
                        title={__(
                            "Number prefix",
                            "essential-blocks"
                        )}
                        initialOpen={false}
                    >
                        <TypographyDropdown
                            baseLabel="Typography"
                            typographyPrefixConstant={
                                typoPrefix_numPrefix
                            }
                        />

                        <ColorControl
                            label={__(
                                "Color",
                                "essential-blocks"
                            )}
                            color={numPrefixColor}
                            attributeName={'numPrefixColor'}
                        />
                    </InspectorPanel.PanelBody>

                    <InspectorPanel.PanelBody
                        title={__(
                            "Number Suffix",
                            "essential-blocks"
                        )}
                        initialOpen={false}
                    >
                        <TypographyDropdown
                            baseLabel="Typography"
                            typographyPrefixConstant={
                                typoPrefix_numSuffix
                            }
                        />

                        <ColorControl
                            label={__(
                                "Color",
                                "essential-blocks"
                            )}
                            color={numSuffixColor}
                            attributeName={'numSuffixColor'}
                        />
                    </InspectorPanel.PanelBody>

                    <InspectorPanel.PanelBody
                        title={__(
                            "Spacing",
                            "essential-blocks"
                        )}
                        initialOpen={false}
                    >
                        <ResponsiveRangeController
                            baseLabel={__(
                                "Number & Title",
                                "Number-counter"
                            )}
                            controlName={rgNumTitle}
                            max={100}
                        />
                        <ResponsiveRangeController
                            baseLabel={__(
                                "Number & Prefix",
                                "Number-counter"
                            )}
                            controlName={rgNumPrefix}
                            max={100}
                        />
                        <ResponsiveRangeController
                            baseLabel={__(
                                "Number & Suffix",
                                "Number-counter"
                            )}
                            controlName={rgNumSuffix}
                            max={100}
                        />
                    </InspectorPanel.PanelBody>
                </>
            </InspectorPanel.Style>
        </InspectorPanel>
    );
};
export default Inspector;
