'use client';
import { useState, useEffect } from 'react';
import CreateCardComponent from '@/app/infra/basic-component/create-card-component/CreateCardComponent';
import PipelineFormComponent from './components/pipeline-form/PipelineFormComponent';
import { httpClient } from '@/app/infra/http/HttpClient';
import { PipelineCardVO } from '@/app/home/pipelines/components/pipeline-card/PipelineCardVO';
import PipelineCard from '@/app/home/pipelines/components/pipeline-card/PipelineCard';
import { PipelineFormEntity } from '@/app/infra/entities/pipeline';
import styles from './pipelineConfig.module.css';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { Button } from '@/components/ui/button';

export default function PluginConfigPage() {
  const [modalOpen, setModalOpen] = useState<boolean>(false);
  const [isEditForm, setIsEditForm] = useState(false);
  const [pipelineList, setPipelineList] = useState<PipelineCardVO[]>([]);
  const [selectedPipelineId, setSelectedPipelineId] = useState('');
  const [selectedPipelineFormValue, setSelectedPipelineFormValue] =
    useState<PipelineFormEntity>({
      basic: {},
      ai: {},
      trigger: {},
      safety: {},
      output: {},
    });
  const [disableForm, setDisableForm] = useState(false);

  useEffect(() => {
    getPipelines();
  }, []);

  function getPipelines() {
    httpClient
      .getPipelines()
      .then((value) => {
        let currentTime = new Date();
        const pipelineList = value.pipelines.map((pipeline) => {
          let lastUpdatedTimeAgo = Math.floor((currentTime.getTime() - new Date(pipeline.updated_at ?? currentTime.getTime()).getTime()) / 1000 / 60 / 60 / 24);
          
          let lastUpdatedTimeAgoText = lastUpdatedTimeAgo > 0 ? ` ${lastUpdatedTimeAgo} 天前` : '今天';
          
          return new PipelineCardVO({
            lastUpdatedTimeAgo: lastUpdatedTimeAgoText,
            description: pipeline.description,
            id: pipeline.uuid ?? '',
            name: pipeline.name,
            isDefault: pipeline.is_default ?? false,
          });
        });
        setPipelineList(pipelineList);
      })
      .catch((error) => {
        // TODO toast
        console.log(error);
      });
  }

  function getSelectedPipelineForm(id?: string) {
    httpClient.getPipeline(id ?? selectedPipelineId).then((value) => {
      setSelectedPipelineFormValue({
        ai: value.pipeline.config.ai,
        basic: {
          description: value.pipeline.description,
          name: value.pipeline.name,
        },
        output: value.pipeline.config.output,
        safety: value.pipeline.config.safety,
        trigger: value.pipeline.config.trigger,
      });
      setDisableForm(false);
    });
  }

  return (
    <div className={styles.configPageContainer}>

      <Dialog open={modalOpen} onOpenChange={setModalOpen}>
        <DialogContent  className="w-[700px] max-h-[80vh] p-0 flex flex-col">
          <DialogHeader className="px-6 pt-6 pb-4">
            <DialogTitle>
              {isEditForm ? '编辑流水线' : '创建流水线'}
            </DialogTitle>
          </DialogHeader>
          <div className="flex-1 overflow-y-auto px-6">
            <PipelineFormComponent
              onNewPipelineCreated={(pipelineId) => {
                setDisableForm(true);
                setIsEditForm(true);
                setModalOpen(true);
                setSelectedPipelineId(pipelineId);
                getSelectedPipelineForm(pipelineId);
              }}
              onFinish={() => {
                getPipelines();
                setModalOpen(false);
              }}
              isEditMode={isEditForm}
              pipelineId={selectedPipelineId}
              disableForm={disableForm}
              initValues={selectedPipelineFormValue}
            />
          </div>
        </DialogContent>
      </Dialog>
      

      {pipelineList.length > 0 && (
        <div className={styles.pipelineListContainer}>
          <CreateCardComponent
            width={'24rem'}
            height={'10rem'}
            plusSize={'90px'}
            onClick={() => {
              setIsEditForm(false);
              setModalOpen(true);
            }}
          />

          {pipelineList.map((pipeline) => {
            return (
              <div
                key={pipeline.id}
                onClick={() => {
                  setDisableForm(true);
                  setIsEditForm(true);
                  setModalOpen(true);
                  setSelectedPipelineId(pipeline.id);
                  getSelectedPipelineForm(pipeline.id);
                }}
              >
                <PipelineCard cardVO={pipeline} />
              </div>
            );
          })}
        </div>
      )}
    </div>
  );
}
