import React, { useState } from 'react';
import { FileText, Table, Box } from 'lucide-react';
import { Button } from './ui/button';
import { useI18n } from '../store/i18nStore';
import { useWindStore } from '../store/appStore';
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from './ui/dialog';
import { Input } from './ui/input';
import { exportGalpaoToCSV } from '../lib/export-csv';
import { exportGalpaoToPDF } from '../lib/export-pdf';
import { exportGalpaoToFtool } from '../lib/export-ftool';

interface ExportMenuProps {
  onExportCSV?: () => void;
  onExportPDF?: () => void;
  onExportFtool?: () => void;
}

const ExportMenu: React.FC<ExportMenuProps> = ({ onExportCSV, onExportPDF, onExportFtool }) => {
  const { t } = useI18n();
  const handleCSV = onExportCSV || exportGalpaoToCSV;
  const handlePDF = onExportPDF || exportGalpaoToPDF;
  
  // Exibir o Ftool apenas se explicitamente fornecido, ou se for a configuração padrão (Galpão)
  const isGalpao = !onExportCSV && !onExportPDF && !onExportFtool;

  const [isPdfDialogOpen, setIsPdfDialogOpen] = useState(false);
  const windStore = useWindStore();
  const [localMeta, setLocalMeta] = useState({
    projectName: windStore.projectName,
    clientName: windStore.clientName,
    authorName: windStore.authorName,
    projectRev: windStore.projectRev,
  });

  const openPdfDialog = () => {
    setLocalMeta({
      projectName: windStore.projectName,
      clientName: windStore.clientName,
      authorName: windStore.authorName,
      projectRev: windStore.projectRev,
    });
    setIsPdfDialogOpen(true);
  };

  const confirmPdfExport = () => {
    windStore.setProjectMetadata(localMeta);
    setIsPdfDialogOpen(false);
    // Give a small delay to ensure state updates before PDF rendering
    setTimeout(() => {
      handlePDF();
    }, 100);
  };

  return (
    <>
      <div className="flex gap-2">
      <Button
        variant="outline"
        size="sm"
        onClick={handleCSV}
        className="text-orange-600 border-orange-200 hover:bg-orange-50 hover:text-orange-700"
        title={t('export_csv')}
      >
        <Table className="w-4 h-4 mr-2" />
        {t('export_csv')}
      </Button>
      <Button
        variant="outline"
        size="sm"
        onClick={openPdfDialog}
        className="text-purple-600 border-purple-200 hover:bg-purple-50 hover:text-purple-700"
        title={t('export_pdf')}
      >
        <FileText className="w-4 h-4 mr-2" />
        {t('export_pdf')}
      </Button>
      {(onExportFtool || isGalpao) && (
        <Button
          variant="outline"
          size="sm"
          onClick={onExportFtool || exportGalpaoToFtool}
          className="text-emerald-600 border-emerald-200 hover:bg-emerald-50 hover:text-emerald-700"
          title={t('ftool_desc')}
        >
          <Box className="w-4 h-4 mr-2" />
          {t('export_ftool')}
        </Button>
      )}
    </div>

      <Dialog open={isPdfDialogOpen} onOpenChange={setIsPdfDialogOpen}>
        <DialogContent className="sm:max-w-[425px]">
          <DialogHeader>
            <DialogTitle>Gerar Relatório PDF</DialogTitle>
          </DialogHeader>
          <div className="grid gap-4 py-4">
            <div className="grid grid-cols-4 items-center gap-4">
              <label htmlFor="projectName" className="text-right text-sm font-medium">
                Projeto
              </label>
              <Input
                id="projectName"
                value={localMeta.projectName}
                onChange={(e) => setLocalMeta({ ...localMeta, projectName: e.target.value })}
                className="col-span-3"
              />
            </div>
            <div className="grid grid-cols-4 items-center gap-4">
              <label htmlFor="clientName" className="text-right text-sm font-medium">
                Cliente
              </label>
              <Input
                id="clientName"
                value={localMeta.clientName}
                onChange={(e) => setLocalMeta({ ...localMeta, clientName: e.target.value })}
                className="col-span-3"
              />
            </div>
            <div className="grid grid-cols-4 items-center gap-4">
              <label htmlFor="authorName" className="text-right text-sm font-medium">
                Autor
              </label>
              <Input
                id="authorName"
                value={localMeta.authorName}
                onChange={(e) => setLocalMeta({ ...localMeta, authorName: e.target.value })}
                className="col-span-3"
              />
            </div>
            <div className="grid grid-cols-4 items-center gap-4">
              <label htmlFor="projectRev" className="text-right text-sm font-medium">
                Revisão
              </label>
              <Input
                id="projectRev"
                value={localMeta.projectRev}
                onChange={(e) => setLocalMeta({ ...localMeta, projectRev: e.target.value })}
                className="col-span-3"
              />
            </div>
          </div>
          <DialogFooter>
            <Button onClick={confirmPdfExport} className="bg-purple-600 hover:bg-purple-700 text-white">
              <FileText className="w-4 h-4 mr-2" />
              Exportar
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </>
  );
};

export default ExportMenu;
