import { PanelBody, RangeControl, SelectControl, TextControl, Button } from "@wordpress/components";
import { useState } from "@wordpress/element";
import { __ } from "@wordpress/i18n";
import ProFeatureOverlay from "../../ProFeatureOverlay";

const TRANSITION_DEFAULT_VALUES = {
  backgroundTransitionDuration: 150,
  backgroundTransitionDelay: 0,
  backgroundTransitionEasing: "ease",
};

const EASING_PRESETS = [
  { label: "ease", value: "ease" },
  { label: "ease-in", value: "ease-in" },
  { label: "ease-out", value: "ease-out" },
  { label: "ease-in-out", value: "ease-in-out" },
  { label: "linear", value: "linear" },
  { label: "cubic-bezier(0.4, 0, 0.2, 1)", value: "cubic-bezier(0.4, 0, 0.2, 1)" },
  { label: "cubic-bezier(0.4, 0, 1, 1)", value: "cubic-bezier(0.4, 0, 1, 1)" },
  { label: "cubic-bezier(0, 0, 0.2, 1)", value: "cubic-bezier(0, 0, 0.2, 1)" },
  { label: __("Custom", "zenblocks"), value: "custom" },
];

const BackgroundTransitionSetting = ({ attributes, setAttributes, title, initialOpen = false, onToggle, isPro = false, learnMorePath = "" }) => {
  const currentEasing = attributes.backgroundTransitionEasing || TRANSITION_DEFAULT_VALUES.backgroundTransitionEasing;
  const isPresetValue = EASING_PRESETS.some((preset) => preset.value === currentEasing && preset.value !== "custom");
  const [isCustomMode, setIsCustomMode] = useState(!isPresetValue);

  const content = (
    <div className="zenblocks-inspector-background-transition" style={{ padding: "16px 0" }}>
      <div style={{ marginBottom: "16px" }}>
        <RangeControl
          label={__("Duration (ms)", "zenblocks")}
          value={attributes.backgroundTransitionDuration ?? TRANSITION_DEFAULT_VALUES.backgroundTransitionDuration}
          onChange={(value) => setAttributes({ backgroundTransitionDuration: value })}
          min={0}
          max={2000}
          step={50}
        />
      </div>
      <div style={{ marginBottom: "16px" }}>
        <RangeControl
          label={__("Delay (ms)", "zenblocks")}
          value={attributes.backgroundTransitionDelay ?? TRANSITION_DEFAULT_VALUES.backgroundTransitionDelay}
          onChange={(value) => setAttributes({ backgroundTransitionDelay: value })}
          min={0}
          max={2000}
          step={50}
        />
      </div>
      <div style={{ marginBottom: "16px" }}>
        <SelectControl
          label={__("Easing", "zenblocks")}
          value={isCustomMode ? "custom" : currentEasing}
          options={EASING_PRESETS}
          onChange={(value) => {
            if (value === "custom") {
              setIsCustomMode(true);
            } else {
              setIsCustomMode(false);
              setAttributes({ backgroundTransitionEasing: value });
            }
          }}
        />
        {isCustomMode && (
          <TextControl
            label={__("Custom Value", "zenblocks")}
            value={currentEasing}
            onChange={(value) => setAttributes({ backgroundTransitionEasing: value })}
            placeholder="cubic-bezier(0.25, 0.1, 0.25, 1)"
            help={__("e.g. cubic-bezier(0.25, 0.1, 0.25, 1)", "zenblocks")}
          />
        )}
      </div>
      {(attributes.backgroundTransitionDuration !== TRANSITION_DEFAULT_VALUES.backgroundTransitionDuration ||
        attributes.backgroundTransitionDelay !== TRANSITION_DEFAULT_VALUES.backgroundTransitionDelay ||
        attributes.backgroundTransitionEasing !== TRANSITION_DEFAULT_VALUES.backgroundTransitionEasing) && (
        <div style={{ marginTop: "8px" }}>
          <Button
            variant="secondary"
            isSmall
            onClick={() => {
              setAttributes({
                backgroundTransitionDuration: TRANSITION_DEFAULT_VALUES.backgroundTransitionDuration,
                backgroundTransitionDelay: TRANSITION_DEFAULT_VALUES.backgroundTransitionDelay,
                backgroundTransitionEasing: TRANSITION_DEFAULT_VALUES.backgroundTransitionEasing,
              });
              setIsCustomMode(false);
            }}
          >
            {__("Reset", "zenblocks")}
          </Button>
        </div>
      )}
    </div>
  );

  return (
    <PanelBody
      title={title || __("Transition", "zenblocks")}
      initialOpen={initialOpen}
      onToggle={onToggle}
      className="zenblocks-panel zenblocks-panel--level-2 zenblocks-panel--settings zenblocks-panel--background-transition"
    >
      {isPro ? <ProFeatureOverlay featureName="トランジション" learnMorePath={learnMorePath}>{content}</ProFeatureOverlay> : content}
    </PanelBody>
  );
};

export default BackgroundTransitionSetting;
export { TRANSITION_DEFAULT_VALUES as BACKGROUND_TRANSITION_DEFAULT_VALUES };
