import 'package:flutter/material.dart';
import '../l10n/l10n.dart';
import '../services/stripe_service.dart';
import 'cases_screen.dart';
import 'cross_chart_screen.dart';
import 'input_screen.dart';
import 'privacy_screen.dart';
import 'tutorial_screen.dart';
import 'theme.dart';
class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final l = LangScope.of(context).l10n;
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [
              Color(0xFFE8E2D6),
              Color(0xFFD5E0DC),
              Color(0xFFC5D0C8),
            ],
          ),
        ),
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 28, vertical: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    if (AuthorAccess.isUnlocked)
                      Text(
                        'AUTHOR · 作者权限',
                        style: TextStyle(
                          fontSize: 11,
                          letterSpacing: 1.2,
                          color: AppTheme.jadeDeep.withValues(alpha: 0.85),
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    const Spacer(),
                    _LangChips(
                      current: l.lang,
                      onChanged: LangScope.of(context).onChanged,
                    ),
                  ],
                ),
                const Spacer(flex: 2),
                Text(
                  l('appTitle'),
                  style: const TextStyle(
                    fontSize: 36,
                    fontWeight: FontWeight.w600,
                    height: 1.15,
                    letterSpacing: -0.5,
                    color: AppTheme.ink,
                  ),
                ),
                const SizedBox(height: 12),
                Text(
                  l('subtitle'),
                  style: TextStyle(
                    fontSize: 16,
                    color: AppTheme.ink.withValues(alpha: 0.7),
                    height: 1.4,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  l('accuracy'),
                  style: const TextStyle(
                    fontSize: 13,
                    color: AppTheme.jade,
                  ),
                ),
                const Spacer(flex: 2),
                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(builder: (_) => const InputScreen()),
                      );
                    },
                    child: Text(l('newAnalysis')),
                  ),
                ),
                const SizedBox(height: 10),
                SizedBox(
                  width: double.infinity,
                  child: OutlinedButton(
                    onPressed: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                            builder: (_) => const CrossChartScreen()),
                      );
                    },
                    style: OutlinedButton.styleFrom(
                      foregroundColor: AppTheme.jadeDeep,
                      side: const BorderSide(color: AppTheme.jadeDeep),
                      padding: const EdgeInsets.symmetric(vertical: 14),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(4)),
                    ),
                    child: Text(l('crossChart')),
                  ),
                ),
                const SizedBox(height: 12),
                Wrap(
                  spacing: 4,
                  children: [
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                              builder: (_) => const CasesScreen()),
                        );
                      },
                      child: Text(l('cases')),
                    ),
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                              builder: (_) => const TutorialScreen()),
                        );
                      },
                      child: Text(l('tutorial')),
                    ),
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                              builder: (_) => const PrivacyScreen()),
                        );
                      },
                      child: Text(l('privacy')),
                    ),
                  ],
                ),
                const Spacer(),
                Text(
                  l('copyright'),
                  style: TextStyle(
                    fontSize: 11,
                    color: AppTheme.ink.withValues(alpha: 0.45),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _LangChips extends StatelessWidget {
  final AppLang current;
  final ValueChanged<AppLang> onChanged;
  const _LangChips({required this.current, required this.onChanged});

  @override
  Widget build(BuildContext context) {
    Widget chip(String label, AppLang lang) {
      final selected = current == lang;
      return GestureDetector(
        onTap: () => onChanged(lang),
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          margin: const EdgeInsets.only(left: 6),
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
          decoration: BoxDecoration(
            color: selected ? AppTheme.jadeDeep : Colors.white54,
            borderRadius: BorderRadius.circular(2),
          ),
          child: Text(
            label,
            style: TextStyle(
              fontSize: 12,
              color: selected ? AppTheme.sand : AppTheme.ink,
            ),
          ),
        ),
      );
    }

    return Row(
      children: [
        chip('EN', AppLang.en),
        chip('中文', AppLang.zh),
        chip('日本語', AppLang.ja),
      ],
    );
  }
}
