import 'package:flutter/material.dart';
import '../core/chart_engine.dart';
import '../core/cross_chart.dart';
import '../core/models.dart';
import '../data/tutorial_cases.dart';
import '../l10n/l10n.dart';
import 'theme.dart';
class CrossChartScreen extends StatefulWidget {
  const CrossChartScreen({super.key});

  @override
  State<CrossChartScreen> createState() => _CrossChartScreenState();
}

class _CrossChartScreenState extends State<CrossChartScreen> {
  String caseA = 'jason';
  String caseB = 'brother';
  CrossChartResult? result;

  BaziChart _chartFromCase(TutorialCase c) {
    final p = c.expectedPillars.split(' ');
    return ChartEngine.fromPillars(
      year: p[0],
      month: p[1],
      day: p[2],
      hour: p[3],
      input: c.input,
    );
  }

  void _run() {
    final a = TutorialCases.all.firstWhere((e) => e.id == caseA);
    final b = TutorialCases.all.firstWhere((e) => e.id == caseB);
    setState(() {
      result = CrossChartAnalyzer.compareCharts(
        _chartFromCase(a),
        _chartFromCase(b),
      );
    });
  }

  String _title(TutorialCase c, L10n l) => switch (l.lang) {
        AppLang.zh => c.titleZh,
        AppLang.ja => c.titleJa,
        AppLang.en => c.titleEn,
      };

  @override
  Widget build(BuildContext context) {
    final l = LangScope.of(context).l10n;
    return Scaffold(
      appBar: AppBar(title: Text(l('crossChart'))),
      body: ListView(
        padding: const EdgeInsets.all(20),
        children: [
          Text(l('personA'),
              style: const TextStyle(fontWeight: FontWeight.w600)),
          DropdownButtonFormField<String>(
            value: caseA,
            items: TutorialCases.all
                .map((c) => DropdownMenuItem(
                      value: c.id,
                      child: Text(_title(c, l)),
                    ))
                .toList(),
            onChanged: (v) => setState(() => caseA = v!),
          ),
          const SizedBox(height: 12),
          Text(l('personB'),
              style: const TextStyle(fontWeight: FontWeight.w600)),
          DropdownButtonFormField<String>(
            value: caseB,
            items: TutorialCases.all
                .map((c) => DropdownMenuItem(
                      value: c.id,
                      child: Text(_title(c, l)),
                    ))
                .toList(),
            onChanged: (v) => setState(() => caseB = v!),
          ),
          const SizedBox(height: 20),
          ElevatedButton(onPressed: _run, child: Text(l('compare'))),
          if (result != null) ...[
            const SizedBox(height: 20),
            Text(result!.summary,
                style: const TextStyle(fontSize: 15, height: 1.4)),
            const SizedBox(height: 8),
            Text(
              '${result!.chartA.fourPillarsText}\n↔\n${result!.chartB.fourPillarsText}',
              textAlign: TextAlign.center,
              style: const TextStyle(fontFamily: 'monospace', height: 1.5),
            ),
            const SizedBox(height: 16),
            ...result!.links.map(
              (link) => Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 8, vertical: 2),
                      color: AppTheme.jadeDeep.withValues(alpha: 0.12),
                      child: Text(
                        link.type,
                        style: const TextStyle(
                            fontSize: 11, color: AppTheme.jadeDeep),
                      ),
                    ),
                    const SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        '${link.detail}  ×${link.multiplier}',
                        style: const TextStyle(fontSize: 13, height: 1.35),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            if (result!.peerEntanglements.isNotEmpty) ...[
              const SizedBox(height: 12),
              const Text('同行纠缠',
                  style: TextStyle(fontWeight: FontWeight.w700)),
              ...result!.peerEntanglements.map((e) => Text('· $e')),
            ],
            if (result!.clashEntanglements.isNotEmpty) ...[
              const SizedBox(height: 12),
              const Text('对冲纠缠',
                  style: TextStyle(fontWeight: FontWeight.w700)),
              ...result!.clashEntanglements.map((e) => Text('· $e')),
            ],
            const SizedBox(height: 24),
            Text(
              l('copyright'),
              style: TextStyle(
                  fontSize: 11, color: AppTheme.ink.withValues(alpha: 0.45)),
            ),
          ],
        ],
      ),
    );
  }
}
