import 'package:flutter/material.dart';
import 'halo_tokens.dart';
import 'widgets/widgets.dart';

const bool kThemePreviewEnabled = bool.fromEnvironment(
  'THEME_PREVIEW',
  defaultValue: false,
);

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

  @override
  State<ThemePreviewScreen> createState() => _ThemePreviewScreenState();
}

class _ThemePreviewScreenState extends State<ThemePreviewScreen> {
  final Set<String> _selectedChips = {'gak nyenyak'};
  bool _disablePrimary = false;

  @override
  Widget build(BuildContext context) {
    final text = Theme.of(context).textTheme;
    return Scaffold(
      appBar: AppBar(title: const Text('Halo theme preview')),
      body: ListView(
        padding: const EdgeInsets.all(HaloSpacing.s20),
        children: [
          _section('Typography'),
          Text('display large 36/700', style: text.displayLarge),
          const SizedBox(height: HaloSpacing.s8),
          Text('title large 22/700', style: text.titleLarge),
          const SizedBox(height: HaloSpacing.s8),
          Text(
            'body medium 15/400 — Poppins',
            style: text.bodyMedium,
          ),
          const SizedBox(height: HaloSpacing.s8),
          Text(
            'label small 10/600 — caption tracking',
            style: text.labelSmall,
          ),
          _divider(),

          _section('Color tokens'),
          const Wrap(
            spacing: HaloSpacing.s8,
            runSpacing: HaloSpacing.s8,
            children: [
              _Swatch('brand', HaloTokens.brand),
              _Swatch('brandDark', HaloTokens.brandDark),
              _Swatch('brandSoft', HaloTokens.brandSoft),
              _Swatch('accent', HaloTokens.accent),
              _Swatch('mint', HaloTokens.mint),
              _Swatch('lilac', HaloTokens.lilac),
              _Swatch('success', HaloTokens.success),
              _Swatch('danger', HaloTokens.danger),
              _Swatch('ink', HaloTokens.ink, label: Colors.white),
              _Swatch('inkSoft', HaloTokens.inkSoft, label: Colors.white),
            ],
          ),
          _divider(),

          _section('HaloButton'),
          Wrap(
            spacing: HaloSpacing.s8,
            runSpacing: HaloSpacing.s8,
            children: [
              HaloButton(
                label: 'primary md',
                onPressed: _disablePrimary ? null : () {},
              ),
              HaloButton(
                label: 'secondary',
                variant: HaloButtonVariant.secondary,
                onPressed: () {},
              ),
              HaloButton(
                label: 'ghost',
                variant: HaloButtonVariant.ghost,
                onPressed: () {},
              ),
              HaloButton(
                label: 'small',
                size: HaloButtonSize.sm,
                onPressed: () {},
              ),
              HaloButton(
                label: 'large with icon',
                size: HaloButtonSize.lg,
                icon: const Icon(Icons.send_rounded),
                onPressed: () {},
              ),
              const HaloButton(
                label: 'disabled',
                onPressed: null,
              ),
            ],
          ),
          const SizedBox(height: HaloSpacing.s12),
          Row(
            children: [
              Switch(
                value: _disablePrimary,
                onChanged: (v) => setState(() => _disablePrimary = v),
              ),
              const Text('disable primary'),
            ],
          ),
          _divider(),

          _section('HaloOrb'),
          Wrap(
            spacing: HaloSpacing.s12,
            runSpacing: HaloSpacing.s12,
            children: List.generate(
              6,
              (i) => HaloOrb(seed: i, label: 'ABCDEF'[i]),
            ),
          ),
          _divider(),

          _section('HaloStepDots'),
          for (int c = 1; c <= 4; c++) ...[
            Padding(
              padding: const EdgeInsets.only(bottom: HaloSpacing.s8),
              child: HaloStepDots(total: 4, current: c),
            ),
          ],
          _divider(),

          _section('HaloChip (ESP-style multi-select)'),
          Wrap(
            spacing: HaloSpacing.s8,
            runSpacing: HaloSpacing.s8,
            children: [
              for (final t in const [
                'gak nyenyak',
                'overthinking',
                'putus',
                'kerjaan',
                'keluarga',
                'sendiri',
              ])
                HaloChip(
                  label: t,
                  selected: _selectedChips.contains(t),
                  onTap: () => setState(() {
                    if (!_selectedChips.add(t)) _selectedChips.remove(t);
                  }),
                ),
            ],
          ),
          _divider(),

          _section('HaloBottomSheet / HaloPopup / HaloSnackbar'),
          Wrap(
            spacing: HaloSpacing.s8,
            runSpacing: HaloSpacing.s8,
            children: [
              HaloButton(
                label: 'show bottom sheet',
                variant: HaloButtonVariant.secondary,
                onPressed: () => HaloBottomSheet.show<void>(
                  context,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Text('halo bestie', style: text.titleLarge),
                      const SizedBox(height: HaloSpacing.s8),
                      Text(
                        'mau verif nomor dulu, atau ngobrol anonim?',
                        style: text.bodyMedium,
                      ),
                      const SizedBox(height: HaloSpacing.s24),
                      HaloButton(
                        label: 'verif nomor',
                        fullWidth: true,
                        onPressed: () => Navigator.of(context).pop(),
                      ),
                      const SizedBox(height: HaloSpacing.s8),
                      HaloButton(
                        label: 'lanjut anonim',
                        variant: HaloButtonVariant.ghost,
                        fullWidth: true,
                        onPressed: () => Navigator.of(context).pop(),
                      ),
                    ],
                  ),
                ),
              ),
              HaloButton(
                label: 'show popup',
                variant: HaloButtonVariant.secondary,
                onPressed: () => HaloPopup.show<void>(
                  context,
                  title: 'verif lagi penuh',
                  body: 'coba lagi nanti, atau lanjut tanpa verif aja.',
                  icon: const Icon(
                    Icons.lock_clock_rounded,
                    size: 40,
                    color: HaloTokens.brand,
                  ),
                  primary: HaloPopupAction(
                    label: 'lanjut tanpa verif',
                    onPressed: () {},
                  ),
                  secondary: HaloPopupAction(
                    label: 'hubungi admin',
                    onPressed: () {},
                  ),
                ),
              ),
              HaloButton(
                label: 'show snackbar',
                variant: HaloButtonVariant.secondary,
                onPressed: () => HaloSnackbar.show(
                  context,
                  'sisa 3 menit lagi ya',
                  icon: '⏳',
                ),
              ),
            ],
          ),
          _divider(),

          _section('Input'),
          const TextField(
            decoration: InputDecoration(
              hintText: 'mau dipanggil apa?',
              labelText: 'nama panggilan',
            ),
          ),
          const SizedBox(height: HaloSpacing.s48),
        ],
      ),
    );
  }

  Widget _section(String title) => Padding(
        padding: const EdgeInsets.only(top: HaloSpacing.s16, bottom: HaloSpacing.s8),
        child: Text(
          title,
          style: const TextStyle(
            fontFamily: HaloTokens.fontDisplay,
            fontSize: 18,
            fontWeight: FontWeight.w700,
            color: HaloTokens.brandDark,
          ),
        ),
      );

  Widget _divider() => const Padding(
        padding: EdgeInsets.symmetric(vertical: HaloSpacing.s16),
        child: Divider(),
      );
}

class _Swatch extends StatelessWidget {
  const _Swatch(this.name, this.color, {this.label = HaloTokens.ink});
  final String name;
  final Color color;
  final Color label;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 110,
      height: 64,
      padding: const EdgeInsets.all(HaloSpacing.s8),
      alignment: Alignment.bottomLeft,
      decoration: BoxDecoration(
        color: color,
        borderRadius: HaloRadius.md,
        border: Border.all(color: HaloTokens.border),
      ),
      child: Text(
        name,
        style: TextStyle(
          fontFamily: HaloTokens.fontBody,
          fontSize: 12,
          fontWeight: FontWeight.w600,
          color: label,
        ),
      ),
    );
  }
}
