import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../di/injector.dart';
import '../i18n/strings.g.dart';
import '../services/export_import_service.dart';
import '../services/locale_store.dart';
import 'backup_section.dart';
import 'theme.dart';

/// Basic settings: app language, backup (export/import) and an "about"
/// section. [exportImport] and [localeStore] are injectable so widget tests can
/// pass fakes; the app resolves them lazily from the service locator.
class SettingsScreen extends StatelessWidget {
  const SettingsScreen({this.exportImport, this.localeStore, super.key});

  final ExportImportService? exportImport;
  final LocaleStore? localeStore;

  @override
  Widget build(BuildContext context) {
    final t = context.t;
    // Compare the full locale (not just the language code) so `es` and the
    // Asturian `es-AST` — which share the `es` language — are told apart.
    final current = TranslationProvider.of(context).locale;
    void pick(AppLocale locale) =>
        (localeStore ?? getIt<LocaleStore>()).setLocale(locale);
    return Scaffold(
      appBar: AppBar(title: Text(t.menu.settings)),
      body: ListView(
        children: [
          _SectionHeader(t.settings.language),
          _LanguageTile(
            label: t.settings.langEs,
            selected: current == AppLocale.es,
            onTap: () => pick(AppLocale.es),
          ),
          _LanguageTile(
            label: t.settings.langAst,
            selected: current == AppLocale.ast,
            onTap: () => pick(AppLocale.ast),
          ),
          _LanguageTile(
            label: t.settings.langEn,
            selected: current == AppLocale.en,
            onTap: () => pick(AppLocale.en),
          ),
          _LanguageTile(
            label: t.settings.langPt,
            selected: current == AppLocale.pt,
            onTap: () => pick(AppLocale.pt),
          ),
          ListTile(
            leading: const Icon(Icons.smartphone_outlined),
            title: Text(t.settings.systemLanguage),
            onTap: () => (localeStore ?? getIt<LocaleStore>()).useDeviceLocale(),
          ),
          const Divider(),
          _SectionHeader(t.backup.title),
          BackupSection(service: exportImport),
          const Divider(),
          _SectionHeader(t.settings.about),
          ListTile(
            leading: Image.asset('assets/logo.png', width: 32),
            title: Text(t.settings.aboutOpen),
            subtitle: Text(t.settings.aboutText),
            trailing: const Icon(Icons.chevron_right),
            onTap: () => context.push('/about'),
          ),
        ],
      ),
    );
  }
}

class _SectionHeader extends StatelessWidget {
  const _SectionHeader(this.text);

  final String text;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(16, 16, 16, 4),
      child: Text(
        text,
        style: Theme.of(context).textTheme.titleSmall?.copyWith(
          color: seedGreen,
          fontWeight: FontWeight.w600,
          letterSpacing: 0.4,
        ),
      ),
    );
  }
}

class _LanguageTile extends StatelessWidget {
  const _LanguageTile({
    required this.label,
    required this.selected,
    required this.onTap,
  });

  final String label;
  final bool selected;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(label),
      trailing: selected ? const Icon(Icons.check, color: seedGreen) : null,
      onTap: onTap,
    );
  }
}
