import { motion } from 'framer-motion'
import { Sun, Moon, Palette, BrainCircuit } from 'lucide-react'
import { useAppStore, type PracticeMode } from '@/stores/useAppStore'

const PRACTICE_MODE_OPTIONS: Array<{
  value: PracticeMode
  label: string
  description: string
}> = [
  {
    value: 'standard',
    label: '标准模式',
    description: '按当前顺序逐项、逐字符输入，适合建立记忆路径。',
  },
  {
    value: 'proficient',
    label: '熟练模式',
    description: '单输入框自动核对，组内可乱序输入，适合冲刺强化。',
  },
]

export function SettingsPage() {
  const { darkMode, setDarkMode, practiceMode, setPracticeMode } = useAppStore()

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-bold text-gray-900 dark:text-white">设置</h1>
        <p className="text-gray-500 dark:text-gray-400 mt-1">
          自定义您的使用体验
        </p>
      </div>

      {/* 主题设置 */}
      <motion.div
        initial={{ opacity: 0, y: 20 }}
        animate={{ opacity: 1, y: 0 }}
        className="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 overflow-hidden"
      >
        <div className="flex items-center gap-3 px-6 py-4 border-b border-gray-100 dark:border-gray-700">
          <Palette size={20} className="text-indigo-600 dark:text-indigo-400" />
          <h2 className="font-semibold text-gray-900 dark:text-white">外观设置</h2>
        </div>
        <div className="p-6">
          <div className="mb-4">
            <label className="text-sm font-medium text-gray-700 dark:text-gray-300">
              主题模式
            </label>
            <p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
              选择您喜欢的界面主题
            </p>
          </div>
          <div className="grid grid-cols-2 gap-4 max-w-xs">
            {[
              { value: false, label: '浅色', icon: Sun },
              { value: true, label: '深色', icon: Moon },
            ].map((option) => {
              const Icon = option.icon
              const isSelected = darkMode === option.value
              return (
                <button
                  key={option.label}
                  onClick={() => setDarkMode(option.value)}
                  className={`flex flex-col items-center gap-2 p-4 rounded-lg border-2 transition-all ${
                    isSelected
                      ? 'border-indigo-500 bg-indigo-50 dark:bg-indigo-900/30'
                      : 'border-gray-200 dark:border-gray-600 hover:border-gray-300 dark:hover:border-gray-500'
                  }`}
                >
                  <Icon
                    size={24}
                    className={isSelected ? 'text-indigo-600 dark:text-indigo-400' : 'text-gray-500'}
                  />
                  <span
                    className={`text-sm font-medium ${
                      isSelected ? 'text-indigo-600 dark:text-indigo-400' : 'text-gray-700 dark:text-gray-300'
                    }`}
                  >
                    {option.label}
                  </span>
                </button>
              )
            })}
          </div>
        </div>
      </motion.div>

      {/* 练习设置 */}
      <motion.div
        initial={{ opacity: 0, y: 20 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ delay: 0.05 }}
        className="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 overflow-hidden"
      >
        <div className="flex items-center gap-3 px-6 py-4 border-b border-gray-100 dark:border-gray-700">
          <BrainCircuit size={20} className="text-indigo-600 dark:text-indigo-400" />
          <h2 className="font-semibold text-gray-900 dark:text-white">练习设置</h2>
        </div>
        <div className="p-6 space-y-4">
          <div>
            <label className="text-sm font-medium text-gray-700 dark:text-gray-300">
              过程详情页练习模式
            </label>
            <p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
              标准模式适合逐步记忆，熟练模式适合在输入、工具、输出三个分组中自由回忆。
            </p>
          </div>
          <div className="grid gap-4 md:grid-cols-2">
            {PRACTICE_MODE_OPTIONS.map((option) => {
              const isSelected = practiceMode === option.value
              return (
                <button
                  key={option.value}
                  type="button"
                  onClick={() => setPracticeMode(option.value)}
                  className={`text-left rounded-xl border-2 p-4 transition-all ${
                    isSelected
                      ? 'border-indigo-500 bg-indigo-50 dark:bg-indigo-900/30'
                      : 'border-gray-200 dark:border-gray-600 hover:border-gray-300 dark:hover:border-gray-500'
                  }`}
                >
                  <div className="flex items-center justify-between gap-3">
                    <span className={`font-semibold ${
                      isSelected
                        ? 'text-indigo-700 dark:text-indigo-300'
                        : 'text-gray-900 dark:text-white'
                    }`}>
                      {option.label}
                    </span>
                    {isSelected && (
                      <span className="px-2 py-0.5 rounded-full text-xs font-medium bg-indigo-100 text-indigo-700 dark:bg-indigo-900/50 dark:text-indigo-300">
                        当前使用
                      </span>
                    )}
                  </div>
                  <p className="mt-2 text-sm text-gray-500 dark:text-gray-400 leading-relaxed">
                    {option.description}
                  </p>
                </button>
              )
            })}
          </div>
        </div>
      </motion.div>

      {/* 联系方式 */}
      <motion.div
        initial={{ opacity: 0, y: 20 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ delay: 0.1 }}
        className="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 p-6"
      >
        <h2 className="font-semibold text-gray-900 dark:text-white mb-4">联系作者</h2>
        <div className="flex flex-col items-center gap-3">
          <img
            src="/wechat-qrcode.jpg"
            alt="微信二维码"
            className="w-48 rounded-lg border border-gray-200 dark:border-gray-600"
          />
          <p className="text-sm text-gray-500 dark:text-gray-400">
            扫一扫上面的二维码图案，加我为朋友
          </p>
        </div>
      </motion.div>

      {/* 关于 */}
      <motion.div
        initial={{ opacity: 0, y: 20 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ delay: 0.2 }}
        className="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 p-6"
      >
        <h2 className="font-semibold text-gray-900 dark:text-white mb-4">关于 ITTOView</h2>
        <div className="space-y-3 text-sm text-gray-500 dark:text-gray-400">
          <p>版本：1.0.0</p>
          <p>包含 49 个项目管理过程的完整 ITTO 数据</p>
          <div className="flex flex-wrap gap-2 pt-1">
            <a
              href="/doc"
              className="inline-flex items-center rounded-lg bg-indigo-50 px-3 py-2 font-medium text-indigo-700 transition-colors hover:bg-indigo-100 dark:bg-indigo-900/30 dark:text-indigo-300 dark:hover:bg-indigo-900/50"
            >
              API 调用说明
            </a>
            <a
              href="/apidoc"
              target="_blank"
              rel="noreferrer"
              className="inline-flex items-center rounded-lg bg-gray-100 px-3 py-2 font-medium text-gray-700 transition-colors hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-200 dark:hover:bg-gray-600"
            >
              Markdown 接口文档
            </a>
          </div>
        </div>
      </motion.div>
    </div>
  )
}
