import 'package:flutter/material.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../../../shared/widgets/page_header.dart' as widgets;

class FeaturesScreen extends StatefulWidget {
  const FeaturesScreen({super.key});

  @override
  State<FeaturesScreen> createState() => _FeaturesScreenState();
}

class _FeaturesScreenState extends State<FeaturesScreen> {
  bool _dailyReminderEnabled = true;
  bool _dailySummaryEnabled = false;
  bool _weeklyReportEnabled = true;
  bool _weeklyDigestEnabled = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF8FAFC),
      body: SafeArea(
        child: Column(
          children: [
            const widgets.PageHeader(leading: widgets.BackButton()),
            Expanded(
              child: SingleChildScrollView(
                padding: const EdgeInsets.fromLTRB(20, 8, 20, 20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _buildSectionTitle('每日'),
                    const SizedBox(height: 8),
                    _buildDailyList(),
                    const SizedBox(height: 16),
                    _buildSectionTitle('每周'),
                    const SizedBox(height: 8),
                    _buildWeeklyList(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSectionTitle(String title) {
    return Text(
      title,
      style: const TextStyle(
        fontSize: 13,
        fontWeight: FontWeight.w600,
        color: AppColors.slate500,
      ),
    );
  }

  Widget _buildDailyList() {
    return Column(
      children: [
        _buildFeatureCard(
          icon: Icons.alarm,
          iconColor: const Color(0xFF14B8A6),
          iconBg: const Color(0xFFECFEFF),
          iconBorder: const Color(0xFFC9F4F2),
          title: '会议提醒',
          subtitle: '每次会议前 15 分钟提醒',
          value: _dailyReminderEnabled,
          onChanged: (v) => setState(() => _dailyReminderEnabled = v),
        ),
        const SizedBox(height: 10),
        _buildFeatureCard(
          icon: Icons.summarize,
          iconColor: const Color(0xFF2563EB),
          iconBg: const Color(0xFFEEF6FF),
          iconBorder: const Color(0xFFDCEAFF),
          title: '每日摘要',
          subtitle: '每天 18:00 发送当日摘要',
          value: _dailySummaryEnabled,
          onChanged: (v) => setState(() => _dailySummaryEnabled = v),
        ),
      ],
    );
  }

  Widget _buildWeeklyList() {
    return Column(
      children: [
        _buildFeatureCard(
          icon: Icons.calendar_view_week,
          iconColor: const Color(0xFF10B981),
          iconBg: const Color(0xFFECFDF5),
          iconBorder: const Color(0xFFCDEEDC),
          title: '周报生成',
          subtitle: '每周一自动生成周报',
          value: _weeklyReportEnabled,
          onChanged: (v) => setState(() => _weeklyReportEnabled = v),
        ),
        const SizedBox(height: 10),
        _buildFeatureCard(
          icon: Icons.article,
          iconColor: const Color(0xFFF59E0B),
          iconBg: const Color(0xFFFFF7ED),
          iconBorder: const Color(0xFFFDE6CD),
          title: '每周摘要',
          subtitle: '每周日发送本周活动汇总',
          value: _weeklyDigestEnabled,
          onChanged: (v) => setState(() => _weeklyDigestEnabled = v),
        ),
      ],
    );
  }

  Widget _buildFeatureCard({
    required IconData icon,
    required Color iconColor,
    required Color iconBg,
    required Color iconBorder,
    required String title,
    required String subtitle,
    required bool value,
    required ValueChanged<bool> onChanged,
  }) {
    return Container(
      padding: const EdgeInsets.all(14),
      decoration: BoxDecoration(
        color: AppColors.white,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: const Color(0xFFE4ECF6)),
      ),
      child: Row(
        children: [
          Container(
            width: 40,
            height: 40,
            decoration: BoxDecoration(
              color: iconBg,
              borderRadius: BorderRadius.circular(12),
              border: Border.all(color: iconBorder),
            ),
            child: Icon(icon, size: 18, color: iconColor),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                    color: AppColors.slate900,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  subtitle,
                  style: const TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.normal,
                    color: AppColors.slate500,
                  ),
                ),
              ],
            ),
          ),
          _buildToggle(value, onChanged),
        ],
      ),
    );
  }

  Widget _buildToggle(bool value, ValueChanged<bool> onChanged) {
    return GestureDetector(
      onTap: () => onChanged(!value),
      child: Container(
        width: 44,
        height: 24,
        padding: const EdgeInsets.all(2),
        decoration: BoxDecoration(
          color: value ? const Color(0xFFBFDBFE) : const Color(0xFFF1F5FC),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: value ? const Color(0xFF93C5FD) : const Color(0xFFD5DFEE),
          ),
        ),
        child: AnimatedAlign(
          duration: const Duration(milliseconds: 150),
          alignment: value ? Alignment.centerRight : Alignment.centerLeft,
          child: Container(
            width: 20,
            height: 20,
            decoration: BoxDecoration(
              color: AppColors.white,
              borderRadius: BorderRadius.circular(10),
              border: Border.all(color: const Color(0xFFCCDDF8)),
            ),
          ),
        ),
      ),
    );
  }
}
