import { useStorage } from '@vueuse/core'

const SETTINGS_VERSION = 2 // 当前版本号

export interface MarkingSettings {
  version?: number
  // 图片位置
  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'

  // 一键打分配置：key 为 "stepSize_fullScore"，value 为排序后的分数数组
  onekeyScoreConfigs: Record<string, number[]>

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

// 默认设置
const defaultSettings: MarkingSettings = {
  version: SETTINGS_VERSION,
  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: [0.5, 1, 2, 3],
  quickScoreLayout: 'single',

  // 一键打分默认值
  onekeyScoreConfigs: {},

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

const rawSettings = ref<MarkingSettings>(uni.getStorageSync('marking_settings') || defaultSettings)
watch(rawSettings, (newVal) => {
  uni.setStorageSync('marking_settings', newVal)
})

// 版本检查和迁移
if (!rawSettings.value.version || rawSettings.value.version < SETTINGS_VERSION) {
  console.log(`Settings version mismatch (${rawSettings.value.version} < ${SETTINGS_VERSION}), resetting to defaults`)
  rawSettings.value = { ...defaultSettings }
}

export const markingSettings = rawSettings

/**
 * 生成默认的一键打分分数数组（按步长从0到满分）
 */
function generateDefaultScores(stepSize: number, fullScore: number): number[] {
  const scores: number[] = []
  for (let i = 0; i <= fullScore; i += stepSize) {
    scores.push(i)
  }
  return scores
}

/**
 * 获取一键打分配置的 key
 */
function getConfigKey(stepSize: number, fullScore: number): string {
  return `${stepSize}_${fullScore}`
}

/**
 * 获取一键打分的分数列表（已排序、已过滤）
 */
export function getOnekeyScores(stepSize: number, fullScore: number): number[] {
  const key = getConfigKey(stepSize, fullScore)
  const config = markingSettings.value.onekeyScoreConfigs[key]
  return config || generateDefaultScores(stepSize, fullScore)
}

/**
 * 设置一键打分的分数列表
 */
export function setOnekeyScores(stepSize: number, fullScore: number, scores: number[]): void {
  const key = getConfigKey(stepSize, fullScore)
  markingSettings.value.onekeyScoreConfigs[key] = scores
}

/**
 * 重置一键打分配置为默认值
 */
export function resetOnekeyScores(stepSize: number, fullScore: number): void {
  const key = getConfigKey(stepSize, fullScore)
  delete markingSettings.value.onekeyScoreConfigs[key]
}

export function useMarkingSettings() {
  return markingSettings
}
