import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/localization/app_localizations.dart';
import '../../../core/theme/app_theme.dart';
import '../../../shared/widgets/common_button.dart';
import '../widgets/language_panel.dart';
import '../widgets/password_panel.dart';

/// 设置页菜单
enum _SettingsMenu { upgrade, language, password }

/// 系统设置页面
class SettingsPage extends ConsumerStatefulWidget {
  const SettingsPage({super.key});

  @override
  ConsumerState<SettingsPage> createState() => _SettingsPageState();
}

class _SettingsPageState extends ConsumerState<SettingsPage> {
  String _currentVersion = 'V1.0.0';
  _SettingsMenu _currentMenu = _SettingsMenu.upgrade;

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);

    return Scaffold(
      body: Container(
        color: AppTheme.backgroundColor,
        child: Row(
          children: [
            // 左侧导航菜单
            SizedBox(
              width: 280,
              child: Container(
                color: Colors.white,
                child: Column(
                  children: [
                    // 设置标题
                    Container(
                      height: 60,
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: AppTheme.primaryColor.withValues(alpha: 0.1),
                      ),
                      child: Row(
                        children: [
                          Icon(Icons.settings,
                              color: AppTheme.primaryColor, size: 24),
                          const SizedBox(width: 12),
                          Text(
                            l10n?.settings ?? '系统设置',
                            style: TextStyle(
                              color: AppTheme.primaryColor,
                              fontSize: 18,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ],
                      ),
                    ),
                    // 软件升级
                    _buildMenuItem(
                      icon: Icons.system_update,
                      title: l10n?.upgrade ?? '软件升级',
                      selected: _currentMenu == _SettingsMenu.upgrade,
                      onTap: () => setState(
                          () => _currentMenu = _SettingsMenu.upgrade),
                    ),
                    // 语言设置
                    _buildMenuItem(
                      icon: Icons.language,
                      title: l10n?.language ?? '语言设置',
                      selected: _currentMenu == _SettingsMenu.language,
                      onTap: () => setState(
                          () => _currentMenu = _SettingsMenu.language),
                    ),
                    // 密码修改
                    _buildMenuItem(
                      icon: Icons.lock,
                      title: l10n?.password ?? '密码修改',
                      selected: _currentMenu == _SettingsMenu.password,
                      onTap: () => setState(
                          () => _currentMenu = _SettingsMenu.password),
                    ),
                  ],
                ),
              ),
            ),

            // 右侧内容区域
            Expanded(
              child: Container(
                margin: const EdgeInsets.all(16),
                padding: const EdgeInsets.all(24),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12),
                ),
                child: _buildContent(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildContent() {
    return switch (_currentMenu) {
      _SettingsMenu.language => const LanguagePanel(),
      _SettingsMenu.password => const PasswordPanel(),
      _SettingsMenu.upgrade => _buildUpgradeContent(),
    };
  }

  Widget _buildUpgradeContent() {
    final l10n = AppLocalizations.of(context);
    return ListView(
      padding: EdgeInsets.zero,
      children: [
        Text(
          l10n?.upgrade ?? '软件升级',
          style: TextStyle(
            color: AppTheme.textPrimary,
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        const SizedBox(height: 24),
        Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: AppTheme.backgroundColor,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Row(
            children: [
              Icon(Icons.info_outline, color: AppTheme.primaryColor),
              const SizedBox(width: 12),
              Text(
                '${l10n?.currentVersion ?? '当前版本'}: $_currentVersion',
                style: TextStyle(color: AppTheme.textPrimary),
              ),
            ],
          ),
        ),
        const SizedBox(height: 24),
        CommonButton(
          text: l10n?.checkUpdate ?? '检查更新',
          icon: Icons.refresh,
          type: ButtonType.primary,
          onPressed: () {
            ScaffoldMessenger.of(context).showSnackBar(
              SnackBar(
                content: Text(l10n?.latestVersion ?? '已是最新版本'),
                backgroundColor: AppTheme.successColor,
              ),
            );
          },
        ),
      ],
    );
  }

  /// 导航菜单项
  Widget _buildMenuItem({
    required IconData icon,
    required String title,
    required VoidCallback onTap,
    bool selected = false,
  }) {
    return Container(
      color: selected
          ? AppTheme.primaryColor.withValues(alpha: 0.08)
          : Colors.transparent,
      child: ListTile(
        leading: Icon(
          icon,
          color: selected ? AppTheme.primaryColor : AppTheme.textSecondary,
        ),
        title: Text(
          title,
          style: TextStyle(
            color: selected ? AppTheme.primaryColor : AppTheme.textPrimary,
            fontWeight: selected ? FontWeight.w600 : FontWeight.normal,
          ),
        ),
        trailing: Icon(Icons.chevron_right, color: AppTheme.idleColor),
        onTap: onTap,
      ),
    );
  }
}
