"use client";

import { useState, useEffect } from "react";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Separator } from "@/components/ui/separator";
import { Palette, Type, Square, Box } from "lucide-react";
import { ComponentStyle } from "@/types/screen";

interface StyleEditorProps {
  style: ComponentStyle;
  onStyleChange: (style: ComponentStyle) => void;
  className?: string;
}

export default function StyleEditor({ style, onStyleChange, className }: StyleEditorProps) {
  const [localStyle, setLocalStyle] = useState<ComponentStyle>(style || {});

  useEffect(() => {
    setLocalStyle(style || {});
  }, [style]);

  const handleStyleChange = (property: keyof ComponentStyle, value: any) => {
    const newStyle = { ...localStyle, [property]: value };
    setLocalStyle(newStyle);
    onStyleChange(newStyle);
  };

  return (
    <div className={`space-y-4 p-3 ${className}`}>
      {/* 테두리 섹션 */}
      <div className="space-y-2">
        <div className="flex items-center gap-2">
          <Square className="text-primary h-3.5 w-3.5" />
          <h3 className="text-sm font-semibold">테두리</h3>
        </div>
        <Separator className="my-1.5" />
        <div className="space-y-2">
          <div className="grid grid-cols-2 gap-2">
            <div className="space-y-1">
              <Label htmlFor="borderWidth" className="text-xs font-medium">
                두께
              </Label>
              <Input
                id="borderWidth"
                type="text"
                placeholder="1px"
                value={localStyle.borderWidth || ""}
                onChange={(e) => handleStyleChange("borderWidth", e.target.value)}
                className="h-6 w-full px-2 py-0 text-xs"
                style={{ fontSize: "12px" }}
              />
            </div>
            <div className="space-y-1">
              <Label htmlFor="borderStyle" className="text-xs font-medium">
                스타일
              </Label>
              <Select
                value={localStyle.borderStyle || "solid"}
                onValueChange={(value) => handleStyleChange("borderStyle", value)}
              >
                <SelectTrigger className="h-6 w-full px-2 py-0" style={{ fontSize: "12px" }}>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="solid" style={{ fontSize: "12px" }}>
                    실선
                  </SelectItem>
                  <SelectItem value="dashed" style={{ fontSize: "12px" }}>
                    파선
                  </SelectItem>
                  <SelectItem value="dotted" style={{ fontSize: "12px" }}>
                    점선
                  </SelectItem>
                  <SelectItem value="none" style={{ fontSize: "12px" }}>
                    없음
                  </SelectItem>
                </SelectContent>
              </Select>
            </div>
          </div>

          <div className="grid grid-cols-2 gap-2">
            <div className="space-y-1">
              <Label htmlFor="borderColor" className="text-xs font-medium">
                색상
              </Label>
              <div className="flex gap-1">
                <Input
                  id="borderColor"
                  type="color"
                  value={localStyle.borderColor || "#000000"}
                  onChange={(e) => handleStyleChange("borderColor", e.target.value)}
                  className="h-6 w-12 p-1"
                  style={{ fontSize: "12px" }}
                />
                <Input
                  type="text"
                  value={localStyle.borderColor || "#000000"}
                  onChange={(e) => handleStyleChange("borderColor", e.target.value)}
                  placeholder="#000000"
                  className="h-6 flex-1 text-xs"
                  style={{ fontSize: "12px" }}
                />
              </div>
            </div>
            <div className="space-y-1">
              <Label htmlFor="borderRadius" className="text-xs font-medium">
                모서리
              </Label>
              <Input
                id="borderRadius"
                type="text"
                placeholder="5px"
                value={localStyle.borderRadius || ""}
                onChange={(e) => handleStyleChange("borderRadius", e.target.value)}
                className="h-6 w-full px-2 py-0 text-xs"
                style={{ fontSize: "12px" }}
              />
            </div>
          </div>
        </div>
      </div>

      {/* 배경 섹션 */}
      <div className="space-y-2">
        <div className="flex items-center gap-2">
          <Palette className="text-primary h-3.5 w-3.5" />
          <h3 className="text-sm font-semibold">배경</h3>
        </div>
        <Separator className="my-1.5" />
        <div className="space-y-2">
          <div className="space-y-1">
            <Label htmlFor="backgroundColor" className="text-xs font-medium">
              색상
            </Label>
            <div className="flex gap-1">
              <Input
                id="backgroundColor"
                type="color"
                value={localStyle.backgroundColor || "#ffffff"}
                onChange={(e) => handleStyleChange("backgroundColor", e.target.value)}
                className="h-6 w-12 p-1"
                style={{ fontSize: "12px" }}
              />
              <Input
                type="text"
                value={localStyle.backgroundColor || "#ffffff"}
                onChange={(e) => handleStyleChange("backgroundColor", e.target.value)}
                placeholder="#ffffff"
                className="h-6 flex-1 text-xs"
                style={{ fontSize: "12px" }}
              />
            </div>
          </div>

          <div className="space-y-1">
            <Label htmlFor="backgroundImage" className="text-xs font-medium">
              이미지
            </Label>
            <Input
              id="backgroundImage"
              type="text"
              placeholder="url('image.jpg')"
              value={localStyle.backgroundImage || ""}
              onChange={(e) => handleStyleChange("backgroundImage", e.target.value)}
              className="h-6 w-full px-2 py-0 text-xs"
              style={{ fontSize: "12px" }}
            />
          </div>
        </div>
      </div>

      {/* 텍스트 섹션 */}
      <div className="space-y-2">
        <div className="flex items-center gap-2">
          <Type className="text-primary h-3.5 w-3.5" />
          <h3 className="text-sm font-semibold">텍스트</h3>
        </div>
        <Separator className="my-1.5" />
        <div className="space-y-2">
          <div className="grid grid-cols-2 gap-2">
            <div className="space-y-1">
              <Label htmlFor="color" className="text-xs font-medium">
                색상
              </Label>
              <div className="flex gap-1">
                <Input
                  id="color"
                  type="color"
                  value={localStyle.color || "#000000"}
                  onChange={(e) => handleStyleChange("color", e.target.value)}
                  className="h-6 w-12 p-1"
                  style={{ fontSize: "12px" }}
                />
                <Input
                  type="text"
                  value={localStyle.color || "#000000"}
                  onChange={(e) => handleStyleChange("color", e.target.value)}
                  placeholder="#000000"
                  className="h-6 flex-1 text-xs"
                  style={{ fontSize: "12px" }}
                />
              </div>
            </div>
            <div className="space-y-1">
              <Label htmlFor="fontSize" className="text-xs font-medium">
                크기
              </Label>
              <Input
                id="fontSize"
                type="text"
                placeholder="14px"
                value={localStyle.fontSize || ""}
                onChange={(e) => handleStyleChange("fontSize", e.target.value)}
                className="h-6 w-full px-2 py-0 text-xs"
                style={{ fontSize: "12px" }}
              />
            </div>
          </div>

          <div className="grid grid-cols-2 gap-2">
            <div className="space-y-1">
              <Label htmlFor="fontWeight" className="text-xs font-medium">
                굵기
              </Label>
              <Select
                value={localStyle.fontWeight || "normal"}
                onValueChange={(value) => handleStyleChange("fontWeight", value)}
              >
                <SelectTrigger className="h-6 w-full px-2 py-0" style={{ fontSize: "12px" }}>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="normal" style={{ fontSize: "12px" }}>
                    보통
                  </SelectItem>
                  <SelectItem value="bold" style={{ fontSize: "12px" }}>
                    굵게
                  </SelectItem>
                  <SelectItem value="100" style={{ fontSize: "12px" }}>
                    100
                  </SelectItem>
                  <SelectItem value="400" style={{ fontSize: "12px" }}>
                    400
                  </SelectItem>
                  <SelectItem value="500" style={{ fontSize: "12px" }}>
                    500
                  </SelectItem>
                  <SelectItem value="600" style={{ fontSize: "12px" }}>
                    600
                  </SelectItem>
                  <SelectItem value="700" style={{ fontSize: "12px" }}>
                    700
                  </SelectItem>
                </SelectContent>
              </Select>
            </div>
            <div className="space-y-1">
              <Label htmlFor="textAlign" className="text-xs font-medium">
                정렬
              </Label>
              <Select
                value={localStyle.textAlign || "left"}
                onValueChange={(value) => handleStyleChange("textAlign", value)}
              >
                <SelectTrigger className="h-6 w-full px-2 py-0" style={{ fontSize: "12px" }}>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="left" style={{ fontSize: "12px" }}>
                    왼쪽
                  </SelectItem>
                  <SelectItem value="center" style={{ fontSize: "12px" }}>
                    가운데
                  </SelectItem>
                  <SelectItem value="right" style={{ fontSize: "12px" }}>
                    오른쪽
                  </SelectItem>
                  <SelectItem value="justify" style={{ fontSize: "12px" }}>
                    양쪽
                  </SelectItem>
                </SelectContent>
              </Select>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
