import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:moneymgr_mobile/providers/settings.dart';
import 'package:moneymgr_mobile/services/storage/prefs.dart';
import 'package:moneymgr_mobile/utils/extensions.dart';

class SettingsScreen extends ConsumerWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final prefs = ref.watch(prefsProvider).requireValue;
    final themeMode = ref.watch(currentThemeModeProvider);

    void onTapThemeMode() =>
        showDialog(context: context, builder: (_) => const _ThemeModeDialog());

    void onTapLicenses() => context.showAppLicensePage();

    handleToggleStartScreen(v) async {
      await prefs.setStartOnScansListScreen(v);
      ref.invalidate(prefsProvider);
    }

    handleToggleDisableExtractDate(v) async {
      await prefs.setDisableExtractDates(v);
      ref.invalidate(prefsProvider);
    }

    return Scaffold(
      appBar: AppBar(title: const Text('Settings')),
      body: ListView(
        children: [
          ListTile(
            leading: const Icon(Icons.brightness_6),
            title: const Text('Theme mode'),
            trailing: Text(themeMode.label),
            onTap: onTapThemeMode,
          ),
          SwitchListTile(
            value: prefs.startOnScansListScreen(),
            onChanged: handleToggleStartScreen,
            title: Text("Start on scans screen"),
            subtitle: Text(
              "Do not start camera automatically on application startup",
            ),
          ),
          SwitchListTile(
            value: prefs.disableExtractDates(),
            onChanged: handleToggleDisableExtractDate,
            title: Text("Do not extract dates"),
            subtitle: Text(
              "Do not attempt to extract dates from scanned expenses",
            ),
          ),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.info_outline),
            title: const Text('Licenses'),
            onTap: onTapLicenses,
          ),
        ],
      ),
    );
  }
}

/// Select theme dialog
class _ThemeModeDialog extends ConsumerWidget {
  const _ThemeModeDialog();

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    void onTapOption(ThemeMode themeMode) {
      ref.read(currentThemeModeProvider.notifier).set(themeMode);
      Navigator.of(context).pop();
    }

    return SimpleDialog(
      clipBehavior: Clip.antiAlias,
      children: [
        for (final themeMode in ThemeMode.values)
          _ThemeModeDialogOption(
            value: themeMode,
            onTap: () => onTapOption(themeMode),
          ),
      ],
    );
  }
}

class _ThemeModeDialogOption extends StatelessWidget {
  const _ThemeModeDialogOption({required this.value, required this.onTap});

  final ThemeMode value;
  final GestureTapCallback? onTap;

  @override
  Widget build(BuildContext context) {
    return ListTile(onTap: onTap, title: Text(value.label));
  }
}
