import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../../../app/i18n/app_localizations.dart';
import '../../../../app/theme/app_colors.dart';
import '../../../../app/theme/app_typography.dart';
import '../../../../core/updater/update_service.dart';

/// 设置页面
///
/// 账号安全、通知、支付管理、语言、关于
class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

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

class _SettingsPageState extends State<SettingsPage> {
  String _appVersion = '';

  @override
  void initState() {
    super.initState();
    _loadVersion();
  }

  Future<void> _loadVersion() async {
    try {
      final info = await PackageInfo.fromPlatform();
      if (mounted) {
        setState(() => _appVersion = 'v${info.version}');
      }
    } catch (_) {
      if (mounted) {
        setState(() => _appVersion = 'v1.0.0');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(context.t('settings.title'))),
      body: ListView(
        children: [
          // Account & Security
          _buildSection(context.t('settings.accountSecurity'), [
            _buildTile(context.t('settings.phone'), subtitle: '138****8888', icon: Icons.phone_rounded),
            _buildTile(context.t('settings.email'), subtitle: 'u***@email.com', icon: Icons.email_rounded),
            _buildTile(context.t('settings.changePassword'), icon: Icons.lock_rounded),
            _buildTile(context.t('settings.identity'), subtitle: 'L1', icon: Icons.verified_user_rounded, onTap: () {
              Navigator.pushNamed(context, '/kyc');
            }),
          ]),

          // Payment
          _buildSection(context.t('settings.paymentManage'), [
            _buildTile(context.t('settings.paymentMethod'), subtitle: 'Visa •••• 4242', icon: Icons.credit_card_rounded),
            _buildTile(context.t('settings.bankAccount'), subtitle: 'BoA •••• 6789', icon: Icons.account_balance_rounded),
            _buildTile(context.t('settings.paymentPassword'), icon: Icons.password_rounded),
          ]),

          // Notifications
          _buildSection(context.t('settings.notifications'), [
            _buildSwitchTile(context.t('settings.tradeNotify'), true),
            _buildSwitchTile(context.t('settings.expiryRemind'), true),
            _buildSwitchTile(context.t('settings.marketChange'), false),
            _buildSwitchTile(context.t('settings.marketingPush'), false),
          ]),

          // General
          _buildSection(context.t('settings.general'), [
            _buildTile(context.t('settings.language'), subtitle: context.t('profile.simplifiedChinese'), icon: Icons.language_rounded),
            _buildTile(context.t('settings.currency'), subtitle: 'USD', icon: Icons.attach_money_rounded),
            _buildTile(context.t('settings.clearCache'), icon: Icons.cleaning_services_rounded),
          ]),

          // About
          _buildSection(context.t('settings.about'), [
            _buildTile(context.t('settings.version'), subtitle: _appVersion, icon: Icons.info_outline_rounded, onTap: () {
              UpdateService().manualCheckUpdate(context);
            }),
            _buildTile(context.t('settings.userAgreement'), icon: Icons.description_rounded),
            _buildTile(context.t('settings.privacyPolicy'), icon: Icons.privacy_tip_rounded),
            _buildTile(context.t('settings.helpCenter'), icon: Icons.help_outline_rounded),
          ]),

          // Logout
          Padding(
            padding: const EdgeInsets.all(20),
            child: OutlinedButton(
              onPressed: () {
                Navigator.of(context).pushNamedAndRemoveUntil('/', (_) => false);
              },
              style: OutlinedButton.styleFrom(
                foregroundColor: AppColors.error,
                side: const BorderSide(color: AppColors.error),
                minimumSize: const Size(double.infinity, 48),
              ),
              child: Text(context.t('settings.logout')),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSection(String title, List<Widget> children) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(20, 20, 20, 8),
          child: Text(title, style: AppTypography.labelSmall),
        ),
        Container(
          color: AppColors.surface,
          child: Column(children: children),
        ),
      ],
    );
  }

  Widget _buildTile(String title, {String? subtitle, IconData? icon, VoidCallback? onTap}) {
    return ListTile(
      leading: icon != null ? Icon(icon, size: 22, color: AppColors.textSecondary) : null,
      title: Text(title, style: AppTypography.bodyMedium),
      subtitle: subtitle != null ? Text(subtitle, style: AppTypography.caption) : null,
      trailing: const Icon(Icons.chevron_right_rounded, size: 20, color: AppColors.textTertiary),
      onTap: onTap ?? () {},
    );
  }

  Widget _buildSwitchTile(String title, bool value) {
    return SwitchListTile(
      title: Text(title, style: AppTypography.bodyMedium),
      value: value,
      onChanged: (_) {},
      activeColor: AppColors.primary,
    );
  }
}
