export interface PlayingInfo {
  title: string
  url: string
}

export interface ScreenInfo {
  index: number
  name: string
  res: string
  pos: string
  primary: boolean
  playing: PlayingInfo | null
}

export interface PageFile {
  PageID: string
  PageTitle: string
  PageDescribe: string
  ScreenIndex: number
  ControllerIndex: number
  IsDisplay: boolean
  PageUrl: string
  PageParams: string
  PagePreview?: string
}

export interface ProjectJson {
  appName?: string
  version?: string
  pages?: PageFile[]
}

export interface PublishDefaults {
  mode: 'multi' | 'single' | 'main'
  screenIndex: number
  alsoMain: boolean
  intervalSec: number
}

export interface AppSettings {
  projectName: string
  projectVersion: string
  jsonPassword: string
  keyThreshold: number
  keyLockKey: string
  publish: PublishDefaults
}

export type ShortcutKind = 'app' | 'edit' | 'publish'

export interface ShortcutResult {
  ok: boolean
  message?: string
}

export interface LoadPayload {
  path: string
  json: ProjectJson | null
  mode: 'edit' | 'publish'
}

export interface AppInfo {
  appVersion: string
  electron: string
  chrome: string
  node: string
  platform: string
}

export interface SmartAgriApi {
  win: {
    minimize(): void
    toggleMaximize(): void
    close(): void
    onMaximized(cb: (v: boolean) => void): () => void
  }
  screens: {
    list(): Promise<ScreenInfo[]>
    states(): Promise<ScreenInfo[]>
    publish(payload: { index: number; url: string; title: string }): Promise<ScreenInfo[]>
    close(index: number): Promise<ScreenInfo[]>
    closeAll(): Promise<ScreenInfo[]>
    mainIndex(): Promise<number>
    publishMain(payload: { url: string; title: string }): Promise<ScreenInfo[]>
    restoreMain(): Promise<ScreenInfo[]>
    probe(): Promise<ScreenInfo[]>
    keyLock(locked: boolean): Promise<boolean>
    keyLockState(): Promise<boolean>
    onChanged(cb: (list: ScreenInfo[]) => void): () => void
  }
  carousel: {
    start(payload: { index: number; pages: { url: string; title: string }[]; intervalSec?: number }): Promise<ScreenInfo[]>
    stop(index: number): Promise<ScreenInfo[]>
  }
  project: {
    saveAs(data: string, password?: string): Promise<string | null>
    save(path: string, data: string, password?: string): Promise<string | null>
    open(password?: string): Promise<{ path: string; json: ProjectJson | null; error?: string } | null>
    openAt(path: string, password?: string): Promise<{ path: string; json: ProjectJson | null; error?: string }>
  }
  settings: {
    get(): Promise<AppSettings>
    set(patch: Partial<AppSettings>): Promise<AppSettings>
  }
  shortcuts: {
    create(kind: ShortcutKind): Promise<ShortcutResult>
  }
  app: {
    info(): Promise<AppInfo>
    consumeLoad(): Promise<LoadPayload | null>
    onLoadProject(cb: (payload: LoadPayload) => void): () => void
  }
  key: {
    onScreenKey(cb: (n: number) => void): () => void
  }
  page: {
    snapshot(url: string): Promise<string | null>
  }
  syncProject(pages: unknown[]): void
  setDirty(d: boolean): void
  beep(): Promise<void>
  createShortcut(): Promise<boolean>
  confirm(message: string): Promise<boolean>
}

declare global {
  interface Window {
    api: SmartAgriApi
  }
}

export {}
