import type { ChatLaunchState, WorkspaceSummary } from "@qjclaw/shared-types"

export type WorkspaceStatusTone = "positive" | "warning" | "info"

export const startupCurtainCopy = {
  brandTitle: "\u5343\u5320\u00b7\u95ee\u5929",
  brandTagline: "START YOUR IDEAS",
  loadingLabel: "准备中",
  syncingConfig: "准备配置",
  syncingProjects: "同步项目",
  startingRuntime: "启动助手",
  connectingGateway: "连接服务",
  ready: "即将进入",
  errorTitle: "\u542f\u52a8\u73af\u5883\u6682\u672a\u5c31\u7eea",
  errorFootnote: "\u8bf7\u91cd\u65b0\u51c6\u5907\uff1b\u5982\u4ecd\u5931\u8d25\uff0c\u53ef\u524d\u5f80\u8bbe\u7f6e\u6216\u5bfc\u51fa\u8bca\u65ad\u7ee7\u7eed\u6392\u67e5\u3002",
  retryHint: "\u51c6\u5907\u5931\u8d25\u540e\u53ef\u91cd\u65b0\u5c1d\u8bd5\uff0c\u6216\u524d\u5f80\u8bbe\u7f6e\u68c0\u67e5\u5bc6\u94a5\u4e0e\u7f51\u7edc\u3002"
} as const

export function getWorkspaceStatusTone(chatLaunchState: ChatLaunchState, isBound: boolean): WorkspaceStatusTone {
  if (!isBound) {
    return "warning"
  }
  if (chatLaunchState === "ready") {
    return "positive"
  }
  if (chatLaunchState === "error") {
    return "warning"
  }
  return "info"
}

export function getWorkspaceStatusLabel(chatLaunchState: ChatLaunchState, isBound: boolean): string {
  if (!isBound) {
    return "\u5f85\u7ed1\u5b9a"
  }
  if (chatLaunchState === "ready") {
    return "\u5df2\u5c31\u7eea"
  }
  if (chatLaunchState === "error") {
    return "\u5f02\u5e38"
  }
  return "\u51c6\u5907\u4e2d"
}

export function getStartupProgress(phase: WorkspaceSummary["startupPhase"] | undefined): number {
  switch (phase) {
    case "syncing-config":
      return 0.24
    case "syncing-projects":
      return 0.4
    case "starting-runtime":
      return 0.56
    case "connecting-gateway":
      return 0.82
    case "ready":
      return 1
    case "error":
      return 0.82
    default:
      return 0.12
  }
}

export function getNextStartupVisualProgress(currentProgress: number, targetProgress: number): number {
  return Math.max(currentProgress, targetProgress)
}

export function getStartupCurtainStatus(
  phase: WorkspaceSummary["startupPhase"] | undefined,
  launchState: ChatLaunchState
): string {
  if (launchState === "error") {
    return startupCurtainCopy.errorTitle
  }

  switch (phase) {
    case "syncing-config":
      return startupCurtainCopy.syncingConfig
    case "syncing-projects":
      return startupCurtainCopy.syncingProjects
    case "starting-runtime":
      return startupCurtainCopy.startingRuntime
    case "connecting-gateway":
      return startupCurtainCopy.connectingGateway
    case "ready":
      return startupCurtainCopy.ready
    default:
      return startupCurtainCopy.loadingLabel
  }
}

export function getStartupCurtainFootnote(launchState: ChatLaunchState): string {
  return launchState === "error" ? startupCurtainCopy.errorFootnote : ""
}
