import { useStorage } from '@vueuse/core'

export interface MarkingSettings {
  // 图片位置
  imagePosition: 'left' | 'center' | 'right'
  // 背景颜色
  backgroundColor: string
  // 键盘大小
  keyboardSize: 'normal' | 'large'
  // 快捷打分鼠标右键提交
  quickScoreRightClick: boolean
  // 图片排列
  imageLayout: 'horizontal' | 'vertical'

  // MarkingToolbar 相关设置
  showTraceToolbar: boolean
  showScoreKeyboard: boolean
  showQuickScore: boolean
  stepSize: number
  scoreMode: 'add' | 'subtract'
  autoSubmit: boolean
  // 隐藏打分栏
  hideMarkingToolbar: boolean

  // 快捷打分点击模式
  quickScoreClickMode: boolean
  quickScoreClickValue: number

  // TraceToolbar 相关设置
  penColor: string
  penWidth: number
  textColor: string
  textSize: number
  showToolOptions: boolean

  // MultiModeToolbar 相关设置
  displayCount: number
  hideMarked: boolean

  // 快捷打分相关设置
  quickScoreScores: number[]
  quickScoreMode: 'onekey' | 'quick'
  quickScoreLayout: 'single' | 'double'

  // 一键打分相关设置 - 保存用户自定义的选择状态（哪些分数被选中）
  onekeyScoreSelections: Record<string, boolean> // key: "stepSize_fullScore", value: 是否选中该分数

  // 缩放控制面板相关设置
  showScalePanel: boolean
  scalePanelCollapsed: boolean
}

// 默认设置
const defaultSettings: MarkingSettings = {
  imagePosition: 'left',
  backgroundColor: '#F0F0F0',
  keyboardSize: 'normal',
  quickScoreRightClick: false,
  imageLayout: 'vertical',

  // MarkingToolbar 默认值
  showTraceToolbar: true,
  showScoreKeyboard: false,
  showQuickScore: false,
  stepSize: 1,
  scoreMode: 'add',
  autoSubmit: true,
  hideMarkingToolbar: false,

  // 快捷打分点击模式默认值
  quickScoreClickMode: false,
  quickScoreClickValue: 0,

  // TraceToolbar 默认值
  penColor: '#ff0000',
  penWidth: 2,
  textColor: '#000000',
  textSize: 14,
  showToolOptions: false,

  // MultiModeToolbar 默认值
  displayCount: 4,
  hideMarked: false,

  // 快捷打分默认值
  quickScoreMode: 'onekey',
  quickScoreScores: [1, 2],
  quickScoreLayout: 'single',

  // 一键打分默认值 - 用户选择状态
  onekeyScoreSelections: {},

  // 缩放控制面板默认值
  showScalePanel: true,
  scalePanelCollapsed: false,
}

export const markingSettings = useStorage<MarkingSettings>('marking_settings', defaultSettings)

/**
 * 生成一键打分的分数选项
 * @param stepSize 步长
 * @param fullScore 满分
 * @returns 分数数组
 */
export function generateOnekeyScores(stepSize: number, fullScore: number): number[] {
  const scores: number[] = []
  const max = Math.max(fullScore, 2)

  for (let i = 0; i <= max; i += stepSize) {
    scores.push(i)
  }

  return scores
}

/**
 * 获取一键打分的选中分数
 * @param stepSize 步长
 * @param fullScore 满分
 * @param selections 用户选择状态
 * @returns 选中的分数数组
 */
export function getSelectedOnekeyScores(
  stepSize: number,
  fullScore: number,
  selections: Record<string, boolean> = {},
): number[] {
  const allScores = generateOnekeyScores(stepSize, fullScore)
  const selectionKey = `${stepSize}_${fullScore}`

  // 如果没有保存的选择状态，默认全选
  const hasSelections = Object.keys(selections).some(key => key.startsWith(`${stepSize}_`))
  if (!hasSelections) {
    return allScores
  }

  // 根据保存的选择状态过滤
  return allScores.filter((score) => {
    const scoreKey = `${selectionKey}_${score}`
    return selections[scoreKey] !== false // 默认选中，除非明确设置为false
  })
}

/**
 * 更新一键打分分数的选择状态
 * @param stepSize 步长
 * @param fullScore 满分
 * @param score 分数
 * @param selected 是否选中
 * @param selections 选择状态对象
 */
export function updateOnekeyScoreSelection(
  stepSize: number,
  fullScore: number,
  score: number,
  selected: boolean,
  selections: Record<string, boolean>,
): void {
  const selectionKey = `${stepSize}_${fullScore}_${score}`
  selections[selectionKey] = selected
}

export function useMarkingSettings() {
  return markingSettings
}
