import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/settings/bloc/settings_bloc.dart';
import 'package:hl_lieferservice/feature/settings/bloc/settings_event.dart';
import 'package:hl_lieferservice/feature/settings/bloc/settings_state.dart';
import 'package:hl_lieferservice/feature/settings/model/settings.dart';

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

  @override
  State<StatefulWidget> createState() => _SettingsPage();
}

class _SettingsPage extends State<SettingsPage> {
  void _logout() {}

  void _changePassword() {}

  Widget _scanSettings() {
    return BlocBuilder<SettingsBloc, SettingsState>(
      builder: (context, state) {
        final currentState = state;

        if (currentState is AppSettingsLoaded) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.all(20),
                child: Text(
                  "Scaneinstellungen",
                  style: Theme.of(context).textTheme.headlineSmall,
                ),
              ),

              ListTile(
                title: const Text("Hardware-Scanner"),
                subtitle: const Text(
                  "Schaltet die Kamera beim Scannen aus und nutzt den Hardware-Scanner",
                ),
                trailing: Switch(
                  value: currentState.settings.useHardwareScanner,
                  onChanged: (value) {
                    Settings newSettings = currentState.settings.copyWith();
                    newSettings.useHardwareScanner = value;

                    context.read<SettingsBloc>().add(
                      UpdateSettings(settings: newSettings),
                    );
                  },
                ),
                tileColor: Theme.of(context).colorScheme.onSecondary,
              ),
            ],
          );
        }

        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.all(20),
              child: Text(
                "Scaneinstellungen",
                style: Theme.of(context).textTheme.headlineSmall,
              ),
            ),

            Card(
              color: Theme.of(context).colorScheme.onSecondary,
              child: Padding(
                padding: const EdgeInsets.all(20),
                child: Center(
                  child: Text("Fehler beim Lesen der Scan-Einstellungen"),
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          _scanSettings(),
          Padding(
            padding: const EdgeInsets.all(20),
            child: Text(
              "Kontoeinstellungen",
              style: Theme.of(context).textTheme.headlineSmall,
            ),
          ),

          ListTile(
            title: const Text("Passwort öndern"),
            trailing: Padding(
              padding: const EdgeInsets.all(2),
              child: IconButton(
                onPressed: _changePassword,
                icon: FilledButton(
                  onPressed: _changePassword,
                  child: const Text("Ändern"),
                ),
              ),
            ),
            tileColor: Theme.of(context).colorScheme.onSecondary,
          ),

          ListTile(
            title: const Text("Ausloggen"),
            trailing: IconButton(
              onPressed: _logout,
              icon: Icon(Icons.logout, color: Colors.redAccent),
            ),
            tileColor: Theme.of(context).colorScheme.onSecondary,
          ),
        ],
      ),
      appBar: AppBar(
        title: Text(
          "Einstellungen",
          style: Theme.of(context).textTheme.headlineMedium,
        ),
      ),
    );
  }
}
