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: "\u6b63\u5728\u4e3a\u60a8\u51c6\u5907\u5bf9\u8bdd\u73af\u5883",
  syncingConfig: "\u6b63\u5728\u540c\u6b65\u5de5\u4f5c\u914d\u7f6e",
  syncingProjects: "\u6b63\u5728\u540c\u6b65\u9879\u76ee\u914d\u7f6e",
  startingRuntime: "\u6b63\u5728\u5524\u8d77\u672c\u5730\u52a9\u624b",
  connectingGateway: "\u6b63\u5728\u5efa\u7acb\u5bf9\u8bdd\u8fde\u63a5",
  ready: "\u51c6\u5907\u5b8c\u6210\uff0c\u6b63\u5728\u8fdb\u5165\u5bf9\u8bdd",
  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 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
    : startupCurtainCopy.loadingLabel
}
