import { useState } from "react";
import type { AppConfig } from "@qjclaw/shared-types";

export interface SettingsState {
  lobsterKeyDraft: string;
  xhsFeishuAppIdDraft: string;
  xhsFeishuAppSecretDraft: string;
  xhsFeishuAppTokenDraft: string;
  xhsFeishuTableIdDraft: string;
  workspacePathDraft: string;
  saving: boolean;
}

export function useSettingsState(config: AppConfig | null) {
  const [lobsterKeyDraft, setLobsterKeyDraft] = useState("");
  const [workspacePathDraft, setWorkspacePathDraft] = useState("");
  const [imageModelApiKeyDraft, setImageModelApiKeyDraft] = useState("");
  const [imageModelBaseUrlDraft, setImageModelBaseUrlDraft] = useState("");
  const [imageModelModelIdDraft, setImageModelModelIdDraft] = useState("");
  const [videoModelApiKeyDraft, setVideoModelApiKeyDraft] = useState("");
  const [videoModelBaseUrlDraft, setVideoModelBaseUrlDraft] = useState("");
  const [videoModelModelIdDraft, setVideoModelModelIdDraft] = useState("");
  const [copywritingModelApiKeyDraft, setCopywritingModelApiKeyDraft] = useState("");
  const [copywritingModelBaseUrlDraft, setCopywritingModelBaseUrlDraft] = useState("");
  const [copywritingModelModelIdDraft, setCopywritingModelModelIdDraft] = useState("");
  const [digitalHumanVolcAccessKeyDraft, setDigitalHumanVolcAccessKeyDraft] = useState("");
  const [digitalHumanVolcSecretKeyDraft, setDigitalHumanVolcSecretKeyDraft] = useState("");
  const [digitalHumanQiniuAccessKeyDraft, setDigitalHumanQiniuAccessKeyDraft] = useState("");
  const [digitalHumanQiniuSecretKeyDraft, setDigitalHumanQiniuSecretKeyDraft] = useState("");
  const [videoAnalyzerBaseUrlDraft, setVideoAnalyzerBaseUrlDraft] = useState("");
  const [videoAnalyzerModelIdDraft, setVideoAnalyzerModelIdDraft] = useState("");
  const [videoAnalyzerApiKeyDraft, setVideoAnalyzerApiKeyDraft] = useState("");
  const [replicationBriefBaseUrlDraft, setReplicationBriefBaseUrlDraft] = useState("");
  const [replicationBriefModelIdDraft, setReplicationBriefModelIdDraft] = useState("");
  const [replicationBriefApiKeyDraft, setReplicationBriefApiKeyDraft] = useState("");
  const [vectcutBaseUrlDraft, setVectcutBaseUrlDraft] = useState("");
  const [vectcutFileBaseUrlDraft, setVectcutFileBaseUrlDraft] = useState("");
  const [vectcutApiKeyDraft, setVectcutApiKeyDraft] = useState("");
  const [xhsFeishuAppIdDraft, setXhsFeishuAppIdDraft] = useState("");
  const [xhsFeishuAppSecretDraft, setXhsFeishuAppSecretDraft] = useState("");
  const [xhsFeishuAppTokenDraft, setXhsFeishuAppTokenDraft] = useState("");
  const [xhsFeishuTableIdDraft, setXhsFeishuTableIdDraft] = useState("");
  const [feishuMobileAppIdDraft, setFeishuMobileAppIdDraft] = useState("");
  const [feishuMobileAppSecretDraft, setFeishuMobileAppSecretDraft] = useState("");
  const [saving, setSaving] = useState(false);

  const hasPendingLobsterKey = lobsterKeyDraft.trim().length > 0;
  const hasPendingXhsFeishuConfig = Boolean(
    xhsFeishuAppIdDraft.trim() ||
    xhsFeishuAppSecretDraft.trim() ||
    xhsFeishuAppTokenDraft.trim() ||
    xhsFeishuTableIdDraft.trim()
  );
  const hasPendingFeishuMobileConfig = Boolean(
    feishuMobileAppIdDraft.trim() ||
    feishuMobileAppSecretDraft.trim()
  );
  const hasPendingModelKeys = Boolean(
    imageModelApiKeyDraft.trim()
    || imageModelBaseUrlDraft.trim() !== (config?.expertModelConfig.image.baseUrl ?? "").trim()
    || imageModelModelIdDraft.trim() !== (config?.expertModelConfig.image.modelId ?? "").trim()
    || videoModelApiKeyDraft.trim()
    || videoModelBaseUrlDraft.trim() !== (config?.expertModelConfig.video.baseUrl ?? "").trim()
    || videoModelModelIdDraft.trim() !== (config?.expertModelConfig.video.modelId ?? "").trim()
    || copywritingModelApiKeyDraft.trim()
    || copywritingModelBaseUrlDraft.trim() !== (config?.expertModelConfig.copywriting.baseUrl ?? "").trim()
    || copywritingModelModelIdDraft.trim() !== (config?.expertModelConfig.copywriting.modelId ?? "").trim()
    || digitalHumanVolcAccessKeyDraft.trim()
    || digitalHumanVolcSecretKeyDraft.trim()
    || digitalHumanQiniuAccessKeyDraft.trim()
    || digitalHumanQiniuSecretKeyDraft.trim()
    || videoAnalyzerBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.videoAnalyzer.baseUrl ?? "").trim()
    || videoAnalyzerModelIdDraft.trim() !== (config?.douyinRuntimeConfig.videoAnalyzer.modelId ?? "").trim()
    || videoAnalyzerApiKeyDraft.trim()
    || replicationBriefBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.replicationBrief.baseUrl ?? "").trim()
    || replicationBriefModelIdDraft.trim() !== (config?.douyinRuntimeConfig.replicationBrief.modelId ?? "").trim()
    || replicationBriefApiKeyDraft.trim()
    || vectcutBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.vectcut.baseUrl ?? "").trim()
    || vectcutFileBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.vectcut.fileBaseUrl ?? "").trim()
    || vectcutApiKeyDraft.trim()
  );

  return {
    lobsterKeyDraft,
    setLobsterKeyDraft,
    workspacePathDraft,
    setWorkspacePathDraft,
    imageModelApiKeyDraft,
    setImageModelApiKeyDraft,
    imageModelBaseUrlDraft,
    setImageModelBaseUrlDraft,
    imageModelModelIdDraft,
    setImageModelModelIdDraft,
    videoModelApiKeyDraft,
    setVideoModelApiKeyDraft,
    videoModelBaseUrlDraft,
    setVideoModelBaseUrlDraft,
    videoModelModelIdDraft,
    setVideoModelModelIdDraft,
    copywritingModelApiKeyDraft,
    setCopywritingModelApiKeyDraft,
    copywritingModelBaseUrlDraft,
    setCopywritingModelBaseUrlDraft,
    copywritingModelModelIdDraft,
    setCopywritingModelModelIdDraft,
    digitalHumanVolcAccessKeyDraft,
    setDigitalHumanVolcAccessKeyDraft,
    digitalHumanVolcSecretKeyDraft,
    setDigitalHumanVolcSecretKeyDraft,
    digitalHumanQiniuAccessKeyDraft,
    setDigitalHumanQiniuAccessKeyDraft,
    digitalHumanQiniuSecretKeyDraft,
    setDigitalHumanQiniuSecretKeyDraft,
    videoAnalyzerBaseUrlDraft,
    setVideoAnalyzerBaseUrlDraft,
    videoAnalyzerModelIdDraft,
    setVideoAnalyzerModelIdDraft,
    videoAnalyzerApiKeyDraft,
    setVideoAnalyzerApiKeyDraft,
    replicationBriefBaseUrlDraft,
    setReplicationBriefBaseUrlDraft,
    replicationBriefModelIdDraft,
    setReplicationBriefModelIdDraft,
    replicationBriefApiKeyDraft,
    setReplicationBriefApiKeyDraft,
    vectcutBaseUrlDraft,
    setVectcutBaseUrlDraft,
    vectcutFileBaseUrlDraft,
    setVectcutFileBaseUrlDraft,
    vectcutApiKeyDraft,
    setVectcutApiKeyDraft,
    xhsFeishuAppIdDraft,
    setXhsFeishuAppIdDraft,
    xhsFeishuAppSecretDraft,
    setXhsFeishuAppSecretDraft,
    xhsFeishuAppTokenDraft,
    setXhsFeishuAppTokenDraft,
    xhsFeishuTableIdDraft,
    setXhsFeishuTableIdDraft,
    feishuMobileAppIdDraft,
    setFeishuMobileAppIdDraft,
    feishuMobileAppSecretDraft,
    setFeishuMobileAppSecretDraft,
    saving,
    setSaving,
    hasPendingLobsterKey,
    hasPendingXhsFeishuConfig,
    hasPendingFeishuMobileConfig,
    hasPendingModelKeys
  };
}
