import { useState, useRef } from 'react';
import { System } from '@/lib/types';
import { toast } from '@/hooks/use-toast';
import { StartESILogin, ESILoginStatus, SetDestinationByName } from 'wailsjs/go/main/App';

interface SystemContextMenuProps {
  x: number;
  y: number;
  system: System;
  onRename: (newName: string) => void;
  onDelete: (system: System) => void;
  onClearConnections: (system: System) => void;
  onClose: () => void;
}

export const SystemContextMenu = ({ x, y, system, onRename, onDelete, onClearConnections, onClose }: SystemContextMenuProps) => {
  if (!system) {
    return null;
  }
  const [isRenaming, setIsRenaming] = useState(false);
  const [newName, setNewName] = useState(system.solarSystemName);
  const menuRef = useRef<HTMLDivElement>(null);

  const handleRename = () => {
    if (newName.trim() && newName !== system.solarSystemName) {
      onRename(newName);
      onClose();
    }
    setIsRenaming(false);
  };

  const handleSetDestination = async () => {
    try {
      const status = await ESILoginStatus();
      if (status.includes('not logged in')) {
        await StartESILogin();
        toast({ title: 'EVE Login', description: 'Please complete login in your browser, then retry.' });
        return;
      }
      await SetDestinationByName(system.solarSystemName, true, false);
      toast({ title: 'Destination set', description: `${system.solarSystemName}` });
      onClose();
    } catch (e: any) {
      toast({ title: 'Failed to set destination', description: `${system.solarSystemName}: ${String(e)}`, variant: 'destructive' });
    }
  };

  return (
    <div
      ref={menuRef}
      className="fixed bg-slate-800 border border-slate-600 rounded-lg shadow-lg p-2 z-50"
      style={{ left: x, top: y }}
      onClick={(e) => e.stopPropagation()}
    >
      {isRenaming ? (
        <div className="space-y-2">
          <input
            type="text"
            value={newName}
            onChange={(e) => setNewName(e.target.value)}
            className="w-full px-2 py-1 bg-slate-700 border border-slate-600 rounded text-white"
            autoFocus
            onKeyDown={(e) => {
              if (e.key === 'Enter') handleRename();
              if (e.key === 'Escape') setIsRenaming(false);
            }}
          />
          <div className="flex space-x-2">
            <button
              onClick={handleRename}
              className="px-2 py-1 bg-blue-600 hover:bg-blue-700 rounded text-white text-sm"
            >
              Save
            </button>
            <button
              onClick={() => setIsRenaming(false)}
              className="px-2 py-1 bg-slate-600 hover:bg-slate-700 rounded text-white text-sm"
            >
              Cancel
            </button>
          </div>
        </div>
      ) : (
        <div className="space-y-1">
          <button
            onClick={() => setIsRenaming(true)}
            className="w-full px-3 py-1 text-left text-white hover:bg-slate-700 rounded text-sm"
          >
            Rename
          </button>
          <button
            onClick={() => onClearConnections(system)}
            className="w-full px-3 py-1 text-left text-white hover:bg-slate-700 rounded text-sm"
          >
            Clear Connections
          </button>
          <button
            onClick={() => onDelete(system)}
            className="w-full px-3 py-1 text-left text-red-400 hover:bg-slate-700 rounded text-sm"
          >
            Delete
          </button>
          <div className="h-px bg-slate-700 my-1" />
          <button
            onClick={handleSetDestination}
            className="w-full px-3 py-1 text-left text-green-400 hover:bg-slate-700 rounded text-sm"
          >
            Set destination
          </button>
        </div>
      )}
    </div>
  );
}; 