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

const DEFAULT_VALUES = {
  buttonAnimationTransitionDuration: 2000,
  buttonAnimationTransitionEasing: "ease-in-out",
  buttonAnimationInterval: 1000,
};

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 ButtonAnimationTransitionSetting = ({ attributes, setAttributes, title, initialOpen = false, panelState, onTogglePanel }) => {
  const currentEasing = attributes.buttonAnimationTransitionEasing || DEFAULT_VALUES.buttonAnimationTransitionEasing;
  const isPresetValue = EASING_PRESETS.some((preset) => preset.value === currentEasing && preset.value !== "custom");
  const [isCustomMode, setIsCustomMode] = useState(!isPresetValue);

  const handleReset = () => {
    setAttributes({
      buttonAnimationTransitionDuration: DEFAULT_VALUES.buttonAnimationTransitionDuration,
      buttonAnimationTransitionEasing: DEFAULT_VALUES.buttonAnimationTransitionEasing,
      buttonAnimationInterval: DEFAULT_VALUES.buttonAnimationInterval,
    });
    setIsCustomMode(false);
  };

  const handleEasingChange = (value) => {
    if (value === "custom") {
      setIsCustomMode(true);
    } else {
      setIsCustomMode(false);
      setAttributes({ buttonAnimationTransitionEasing: value });
    }
  };

  const isChanged =
    attributes.buttonAnimationTransitionDuration !== DEFAULT_VALUES.buttonAnimationTransitionDuration ||
    attributes.buttonAnimationTransitionEasing !== DEFAULT_VALUES.buttonAnimationTransitionEasing ||
    attributes.buttonAnimationInterval !== DEFAULT_VALUES.buttonAnimationInterval;

  return (
    <PanelBody title={title || __("Transition", "zenblocks")} initialOpen={initialOpen || panelState} onToggle={onTogglePanel} className="zenblocks-panel zenblocks-panel--level-2 zenblocks-panel--settings zenblocks-panel--transition">
      <div className="zenblocks-inspector-button-animation-transition" style={{ padding: "16px 0" }}>
        <div style={{ marginBottom: "16px" }}>
          <RangeControl
            label={__("Animation Duration (ms)", "zenblocks")}
            value={attributes.buttonAnimationTransitionDuration ?? DEFAULT_VALUES.buttonAnimationTransitionDuration}
            onChange={(value) => setAttributes({ buttonAnimationTransitionDuration: value })}
            min={500}
            max={5000}
            step={100}
          />
        </div>
        <div style={{ marginBottom: "16px" }}>
          <SelectControl label={__("Easing", "zenblocks")} value={isCustomMode ? "custom" : currentEasing} options={EASING_PRESETS} onChange={handleEasingChange} />
          {isCustomMode && (
            <TextControl
              label={__("Custom Value", "zenblocks")}
              value={currentEasing}
              onChange={(value) => setAttributes({ buttonAnimationTransitionEasing: 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>
        <div style={{ marginBottom: "" }}>
          <RangeControl
            label={__("Loop Interval (ms)", "zenblocks")}
            value={attributes.buttonAnimationInterval ?? DEFAULT_VALUES.buttonAnimationInterval}
            onChange={(value) => setAttributes({ buttonAnimationInterval: value })}
            min={0}
            max={5000}
            step={100}
          />
        </div>
        {isChanged && (
          <div style={{ marginTop: "16px" }}>
            <Button variant="secondary" isSmall onClick={handleReset}>
              {__("Reset", "zenblocks")}
            </Button>
          </div>
        )}
      </div>
    </PanelBody>
  );
};

export default ButtonAnimationTransitionSetting;
