"use client";

import { ConversionSettings, ObjectPosition } from "@/types";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Checkbox } from "@/components/ui/checkbox";
import { ArrowRightLeft, HelpCircle } from "lucide-react";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { ObjectPositionControl } from "@/components/object-position-control";

const aspectRatios = [
  { name: "Custom", value: "custom" },
  { name: "1:1 (Square)", value: "1/1" },
  { name: "4:3 (Standard)", value: "4/3" },
  { name: "3:2 (Photography)", value: "3/2" },
  { name: "16:9 (Widescreen)", value: "16/9" },
];

interface ImageSettingsProps {
  settings: ConversionSettings;
  onSettingsChange: (settings: Partial<ConversionSettings>) => void;
  onAspectRatioChange: (value: string) => void;
  onSwapDimensions: () => void;
}

export function ImageSettings({
  settings,
  onSettingsChange,
  onAspectRatioChange,
  onSwapDimensions,
}: ImageSettingsProps) {
  return (
    <div className="space-y-4">
      <div>
        <div className="flex items-center gap-1.5">
          <Label htmlFor="aspect-ratio">Aspect Ratio</Label>
          <Tooltip>
            <TooltipTrigger><HelpCircle className="h-4 w-4 text-muted-foreground" /></TooltipTrigger>
            <TooltipContent><p>Select a preset aspect ratio or 'Custom' to enter dimensions manually.</p></TooltipContent>
          </Tooltip>
        </div>
        <Select value={settings.aspectRatio} onValueChange={onAspectRatioChange}>
          <SelectTrigger id="aspect-ratio" className="mt-2"><SelectValue placeholder="Select aspect ratio" /></SelectTrigger>
          <SelectContent>
            {aspectRatios.map((ratio) => (
              <SelectItem key={ratio.value} value={ratio.value}>{ratio.name}</SelectItem>
            ))}
          </SelectContent>
        </Select>
      </div>
      <div className="flex items-end gap-2">
        <div className="space-y-2 flex-1">
          <div className="flex items-center gap-1.5">
            <Label htmlFor="width">Width (px)</Label>
            <Tooltip>
              <TooltipTrigger><HelpCircle className="h-4 w-4 text-muted-foreground" /></TooltipTrigger>
              <TooltipContent><p>Set the output width in pixels. Leave empty to use the original width.</p></TooltipContent>
            </Tooltip>
          </div>
          <Input id="width" type="number" placeholder="Auto" value={settings.width} onChange={(e) => { onSettingsChange({ width: e.target.value, aspectRatio: 'custom' }) }} />
        </div>
        <Tooltip>
          <TooltipTrigger asChild>
            <Button variant="outline" size="icon" onClick={onSwapDimensions} className="shrink-0" aria-label="Swap width and height">
              <ArrowRightLeft className="h-4 w-4" />
            </Button>
          </TooltipTrigger>
          <TooltipContent><p>Swap the entered values for width and height.</p></TooltipContent>
        </Tooltip>
        <div className="space-y-2 flex-1">
          <div className="flex items-center gap-1.5">
            <Label htmlFor="height">Height (px)</Label>
            <Tooltip>
              <TooltipTrigger><HelpCircle className="h-4 w-4 text-muted-foreground" /></TooltipTrigger>
              <TooltipContent><p>Set the output height in pixels. Leave empty to use the original height.</p></TooltipContent>
            </Tooltip>
          </div>
          <Input id="height" type="number" placeholder="Auto" value={settings.height} onChange={(e) => { onSettingsChange({ height: e.target.value, aspectRatio: 'custom' }) }} />
        </div>
      </div>
      <div className="flex items-center space-x-2 pt-2">
        <Checkbox id="keep-orientation" checked={settings.keepOrientation} onCheckedChange={(checked) => onSettingsChange({ keepOrientation: Boolean(checked) })} />
        <Label htmlFor="keep-orientation" className="cursor-pointer flex items-center gap-1.5">
          Keep original orientation
          <Tooltip>
            <TooltipTrigger onClick={(e) => e.preventDefault()}><HelpCircle className="h-4 w-4 text-muted-foreground" /></TooltipTrigger>
            <TooltipContent><p>Automatically swaps width and height to match the original image's orientation.</p></TooltipContent>
          </Tooltip>
        </Label>
      </div>
      <div className="mt-4 space-y-2">
        <div className="flex items-center gap-1.5">
          <Label htmlFor="scale-mode">Scaling</Label>
          <Tooltip>
            <TooltipTrigger><HelpCircle className="h-4 w-4 text-muted-foreground" /></TooltipTrigger>
            <TooltipContent><p>Determines how the image fits into the new dimensions.</p></TooltipContent>
          </Tooltip>
        </div>
        <Select value={settings.scaleMode} onValueChange={(value) => onSettingsChange({ scaleMode: value as any })}>
          <SelectTrigger id="scale-mode"><SelectValue placeholder="Select scale mode" /></SelectTrigger>
          <SelectContent>
            <SelectItem value="fill">Fill (stretch)</SelectItem>
            <SelectItem value="cover">Cover (crop)</SelectItem>
            <SelectItem value="contain">Contain (letterbox)</SelectItem>
          </SelectContent>
        </Select>
      </div>
      {settings.scaleMode !== 'fill' && (
        <div className="mt-4 space-y-2">
          <div className="flex items-center gap-1.5">
            <Label>Position</Label>
            <Tooltip>
              <TooltipTrigger><HelpCircle className="h-4 w-4 text-muted-foreground" /></TooltipTrigger>
              <TooltipContent><p>Sets the anchor point for 'Cover' or 'Contain' scaling.</p></TooltipContent>
            </Tooltip>
          </div>
          <ObjectPositionControl value={settings.objectPosition} onChange={(pos) => onSettingsChange({ objectPosition: pos as ObjectPosition })} />
        </div>
      )}
    </div>
  );
}