import 'dart:io';

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

import '../../../../common/routing/routes.dart';
import '../../../../common/settings/app_settings.dart';
import '../../../../common/utils/linux_shutdown.dart';
import 'widgets/color_seed_button.dart';
import '../../business/wifi_controller.dart';

// dialog api:
// https://api.flutter.dev/flutter/material/Dialog-class.html

// inspired from:
// https://github.com/canonical/nm.dart/blob/main/example/wifi.dart

/// Show Settings Screen
class SettingsScreen extends ConsumerWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final wifiStream = (Platform.isLinux)
        ? ref.watch(wifiControllerProvider.notifier).wifiStream()
        : const Stream.empty();

    final useDarkMode = ref.watch(useDarkModeProvider);
    final useMaterial3 = ref.watch(useMaterial3Provider);
    final showPerformanceOverlay = ref.watch(showPerformanceOverlayProvider);

    return Scaffold(
      appBar: AppBar(
        title: const Text('Preferences'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            ListTile(
              leading: const Icon(Icons.wifi),
              title: const Text('Internet'),
              subtitle: StreamBuilder(
                stream: wifiStream,
                builder: (context, snapshot) {
                  if (!Platform.isLinux) {
                    return const Text("Only supported on Linux");
                  }
                  if (snapshot.hasData) {
                    if (snapshot.data?.$1.activeConnection?.id != null) {
                      return Text(snapshot.data!.$1.activeConnection!.id);
                    }
                  }
                  if (snapshot.hasError) return Text(snapshot.error.toString());
                  return const Text("Not connected");
                },
              ),
              onTap: () => context.pushNamed(RoutesSettings.wifi.name),
            ),
            const Divider(),
            ListTile(
              onTap: () => ref.read(useDarkModeProvider.notifier).toggle(),
              leading: const Icon(Icons.dark_mode),
              title: const Text('Dark theme'),
              subtitle: const Text('Switch between the light and dark theme.'),
              trailing: Switch(
                value: useDarkMode,
                onChanged: (_) =>
                    ref.read(useDarkModeProvider.notifier).toggle(),
              ),
            ),
            ListTile(
              onTap: () => ref.read(useMaterial3Provider.notifier).toggle(),
              leading: const Icon(Icons.filter_3),
              title: const Text('Use Material 3 theme'),
              subtitle: const Text(
                  'Switch between the Material 3 and Material 2 theme.'),
              trailing: Switch(
                value: useMaterial3,
                onChanged: (_) =>
                    ref.read(useMaterial3Provider.notifier).toggle(),
              ),
            ),
            ListTile(
              onTap: () =>
                  ref.read(showPerformanceOverlayProvider.notifier).toggle(),
              leading: const Icon(Icons.speed),
              title: const Text('Show Performance Overlay'),
              subtitle: const Text(
                  'Show the performance overlay at the top of the screen.'),
              trailing: Switch(
                value: showPerformanceOverlay,
                onChanged: (_) =>
                    ref.read(showPerformanceOverlayProvider.notifier).toggle(),
              ),
            ),
            const ColorSeedButton(),
            const Divider(),
            const SizedBox(
              height: 4,
            ),
            if (Platform.isLinux)
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () async => await linuxShutdown(restart: true),
                    child: const Text('Reboot'),
                  ),
                  const SizedBox(
                    width: 24,
                    height: 48,
                  ),
                  ElevatedButton(
                    onPressed: () async => await linuxShutdown(),
                    child: const Text('Shutdown'),
                  ),
                ],
              )
          ],
        ),
      ),
    );
  }
}
