"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-6 p-6 ${className}`}>
      {/* 여백 섹션 */}
      <div className="space-y-3">
        <div className="flex items-center gap-2">
          <Box className="text-primary h-4 w-4" />
          <h3 className="text-sm font-semibold">여백</h3>
        </div>
        <Separator className="my-2" />
        <div className="space-y-3">
          <div className="grid grid-cols-2 gap-3">
            <div className="space-y-1.5">
              <Label htmlFor="margin" className="text-xs font-medium">
                외부 여백
              </Label>
              <Input
                id="margin"
                type="text"
                placeholder="10px"
                value={localStyle.margin || ""}
                onChange={(e) => handleStyleChange("margin", e.target.value)}
                className="h-8"
              />
            </div>
            <div className="space-y-1.5">
              <Label htmlFor="padding" className="text-xs font-medium">
                내부 여백
              </Label>
              <Input
                id="padding"
                type="text"
                placeholder="10px"
                value={localStyle.padding || ""}
                onChange={(e) => handleStyleChange("padding", e.target.value)}
                className="h-8"
              />
            </div>
          </div>

          <div className="space-y-1.5">
            <Label htmlFor="gap" className="text-xs font-medium">
              간격
            </Label>
            <Input
              id="gap"
              type="text"
              placeholder="10px"
              value={localStyle.gap || ""}
              onChange={(e) => handleStyleChange("gap", e.target.value)}
              className="h-8"
            />
          </div>
        </div>
      </div>

      {/* 테두리 섹션 */}
      <div className="space-y-3">
        <div className="flex items-center gap-2">
          <Square className="text-primary h-4 w-4" />
          <h3 className="text-sm font-semibold">테두리</h3>
        </div>
        <Separator className="my-2" />
        <div className="space-y-3">
          <div className="grid grid-cols-2 gap-3">
            <div className="space-y-1.5">
              <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-8"
              />
            </div>
            <div className="space-y-1.5">
              <Label htmlFor="borderStyle" className="text-xs font-medium">
                스타일
              </Label>
              <Select
                value={localStyle.borderStyle || "solid"}
                onValueChange={(value) => handleStyleChange("borderStyle", value)}
              >
                <SelectTrigger className="h-8">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="solid">실선</SelectItem>
                  <SelectItem value="dashed">파선</SelectItem>
                  <SelectItem value="dotted">점선</SelectItem>
                  <SelectItem value="none">없음</SelectItem>
                </SelectContent>
              </Select>
            </div>
          </div>

          <div className="grid grid-cols-2 gap-3">
            <div className="space-y-1.5">
              <Label htmlFor="borderColor" className="text-xs font-medium">
                색상
              </Label>
              <div className="flex gap-2">
                <Input
                  id="borderColor"
                  type="color"
                  value={localStyle.borderColor || "#000000"}
                  onChange={(e) => handleStyleChange("borderColor", e.target.value)}
                  className="h-8 w-14 p-1"
                />
                <Input
                  type="text"
                  value={localStyle.borderColor || "#000000"}
                  onChange={(e) => handleStyleChange("borderColor", e.target.value)}
                  placeholder="#000000"
                  className="h-8 flex-1"
                />
              </div>
            </div>
            <div className="space-y-1.5">
              <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-8"
              />
            </div>
          </div>
        </div>
      </div>

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

          <div className="space-y-1.5">
            <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-8"
            />
          </div>
        </div>
      </div>

      {/* 텍스트 섹션 */}
      <div className="space-y-3">
        <div className="flex items-center gap-2">
          <Type className="text-primary h-4 w-4" />
          <h3 className="text-sm font-semibold">텍스트</h3>
        </div>
        <Separator className="my-2" />
        <div className="space-y-3">
          <div className="grid grid-cols-2 gap-3">
            <div className="space-y-1.5">
              <Label htmlFor="color" className="text-xs font-medium">
                색상
              </Label>
              <div className="flex gap-2">
                <Input
                  id="color"
                  type="color"
                  value={localStyle.color || "#000000"}
                  onChange={(e) => handleStyleChange("color", e.target.value)}
                  className="h-8 w-14 p-1"
                />
                <Input
                  type="text"
                  value={localStyle.color || "#000000"}
                  onChange={(e) => handleStyleChange("color", e.target.value)}
                  placeholder="#000000"
                  className="h-8 flex-1"
                />
              </div>
            </div>
            <div className="space-y-1.5">
              <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-8"
              />
            </div>
          </div>

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