/**
 * 设置状态管理
 * @file settings.js
 * @description 应用设置相关的状态管理
 */
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useSettingsStore = defineStore('settings', () => {
  // 侧边栏状态
  const sidebarCollapsed = ref(false)
  
  // 主题设置
  const theme = ref('light') // light, dark
  const primaryColor = ref('#1890ff')
  
  // 语言设置
  const language = ref('zh-CN')
  
  // 页面设置
  const pageSize = ref(10)
  const showBreadcrumb = ref(true)
  const showFooter = ref(true)
  
  // 表格设置
  const tableSize = ref('middle') // small, middle, large
  const tableBordered = ref(false)
  const tableStriped = ref(true)
  
  // 表单设置
  const formSize = ref('middle')
  const formLabelAlign = ref('right')
  
  // 动画设置
  const enableAnimation = ref(true)
  const animationDuration = ref(300)
  
  // 通知设置
  const enableNotification = ref(true)
  const enableSound = ref(false)
  
  // 数据设置
  const autoRefresh = ref(true)
  const refreshInterval = ref(30000) // 30秒
  
  // 切换侧边栏
  function toggleSidebar() {
    sidebarCollapsed.value = !sidebarCollapsed.value
  }
  
  // 设置侧边栏状态
  function setSidebarCollapsed(collapsed) {
    sidebarCollapsed.value = collapsed
  }
  
  // 切换主题
  function toggleTheme() {
    theme.value = theme.value === 'light' ? 'dark' : 'light'
  }
  
  // 设置主题
  function setTheme(newTheme) {
    theme.value = newTheme
  }
  
  // 设置主色调
  function setPrimaryColor(color) {
    primaryColor.value = color
  }
  
  // 设置语言
  function setLanguage(lang) {
    language.value = lang
  }
  
  // 设置页面大小
  function setPageSize(size) {
    pageSize.value = size
  }
  
  // 设置表格大小
  function setTableSize(size) {
    tableSize.value = size
  }
  
  // 设置表格边框
  function setTableBordered(bordered) {
    tableBordered.value = bordered
  }
  
  // 设置表格斑马纹
  function setTableStriped(striped) {
    tableStriped.value = striped
  }
  
  // 设置表单大小
  function setFormSize(size) {
    formSize.value = size
  }
  
  // 设置表单标签对齐
  function setFormLabelAlign(align) {
    formLabelAlign.value = align
  }
  
  // 设置动画
  function setAnimation(enabled, duration = 300) {
    enableAnimation.value = enabled
    animationDuration.value = duration
  }
  
  // 设置通知
  function setNotification(enabled, sound = false) {
    enableNotification.value = enabled
    enableSound.value = sound
  }
  
  // 设置自动刷新
  function setAutoRefresh(enabled, interval = 30000) {
    autoRefresh.value = enabled
    refreshInterval.value = interval
  }
  
  // 重置设置
  function resetSettings() {
    sidebarCollapsed.value = false
    theme.value = 'light'
    primaryColor.value = '#1890ff'
    language.value = 'zh-CN'
    pageSize.value = 10
    showBreadcrumb.value = true
    showFooter.value = true
    tableSize.value = 'middle'
    tableBordered.value = false
    tableStriped.value = true
    formSize.value = 'middle'
    formLabelAlign.value = 'right'
    enableAnimation.value = true
    animationDuration.value = 300
    enableNotification.value = true
    enableSound.value = false
    autoRefresh.value = true
    refreshInterval.value = 30000
  }
  
  // 保存设置到本地存储
  function saveSettings() {
    const settings = {
      sidebarCollapsed: sidebarCollapsed.value,
      theme: theme.value,
      primaryColor: primaryColor.value,
      language: language.value,
      pageSize: pageSize.value,
      showBreadcrumb: showBreadcrumb.value,
      showFooter: showFooter.value,
      tableSize: tableSize.value,
      tableBordered: tableBordered.value,
      tableStriped: tableStriped.value,
      formSize: formSize.value,
      formLabelAlign: formLabelAlign.value,
      enableAnimation: enableAnimation.value,
      animationDuration: animationDuration.value,
      enableNotification: enableNotification.value,
      enableSound: enableSound.value,
      autoRefresh: autoRefresh.value,
      refreshInterval: refreshInterval.value
    }
    
    localStorage.setItem('bank_settings', JSON.stringify(settings))
  }
  
  // 从本地存储加载设置
  function loadSettings() {
    try {
      const saved = localStorage.getItem('bank_settings')
      if (saved) {
        const settings = JSON.parse(saved)
        
        sidebarCollapsed.value = settings.sidebarCollapsed ?? false
        theme.value = settings.theme ?? 'light'
        primaryColor.value = settings.primaryColor ?? '#1890ff'
        language.value = settings.language ?? 'zh-CN'
        pageSize.value = settings.pageSize ?? 10
        showBreadcrumb.value = settings.showBreadcrumb ?? true
        showFooter.value = settings.showFooter ?? true
        tableSize.value = settings.tableSize ?? 'middle'
        tableBordered.value = settings.tableBordered ?? false
        tableStriped.value = settings.tableStriped ?? true
        formSize.value = settings.formSize ?? 'middle'
        formLabelAlign.value = settings.formLabelAlign ?? 'right'
        enableAnimation.value = settings.enableAnimation ?? true
        animationDuration.value = settings.animationDuration ?? 300
        enableNotification.value = settings.enableNotification ?? true
        enableSound.value = settings.enableSound ?? false
        autoRefresh.value = settings.autoRefresh ?? true
        refreshInterval.value = settings.refreshInterval ?? 30000
      }
    } catch (error) {
      console.error('加载设置失败:', error)
    }
  }
  
  // 获取设置快照
  function getSettingsSnapshot() {
    return {
      sidebarCollapsed: sidebarCollapsed.value,
      theme: theme.value,
      primaryColor: primaryColor.value,
      language: language.value,
      pageSize: pageSize.value,
      showBreadcrumb: showBreadcrumb.value,
      showFooter: showFooter.value,
      tableSize: tableSize.value,
      tableBordered: tableBordered.value,
      tableStriped: tableStriped.value,
      formSize: formSize.value,
      formLabelAlign: formLabelAlign.value,
      enableAnimation: enableAnimation.value,
      animationDuration: animationDuration.value,
      enableNotification: enableNotification.value,
      enableSound: enableSound.value,
      autoRefresh: autoRefresh.value,
      refreshInterval: refreshInterval.value
    }
  }
  
  return {
    // 状态
    sidebarCollapsed,
    theme,
    primaryColor,
    language,
    pageSize,
    showBreadcrumb,
    showFooter,
    tableSize,
    tableBordered,
    tableStriped,
    formSize,
    formLabelAlign,
    enableAnimation,
    animationDuration,
    enableNotification,
    enableSound,
    autoRefresh,
    refreshInterval,
    
    // 方法
    toggleSidebar,
    setSidebarCollapsed,
    toggleTheme,
    setTheme,
    setPrimaryColor,
    setLanguage,
    setPageSize,
    setTableSize,
    setTableBordered,
    setTableStriped,
    setFormSize,
    setFormLabelAlign,
    setAnimation,
    setNotification,
    setAutoRefresh,
    resetSettings,
    saveSettings,
    loadSettings,
    getSettingsSnapshot
  }
})