import 'package:flutter/material.dart';

import 'package:get/get.dart';

import 'package:f0ckapp/controller/authcontroller.dart';
import 'package:f0ckapp/controller/themecontroller.dart';
import 'package:f0ckapp/screens/login.dart';
import 'package:f0ckapp/screens/settings.dart';
import 'package:f0ckapp/utils/appversion.dart';

class EndDrawer extends StatelessWidget {
  const EndDrawer({super.key});

  void _showMsg(String message, {String title = ''}) {
    Get
      ..closeAllSnackbars()
      ..snackbar(message, title, snackPosition: SnackPosition.BOTTOM);
  }

  @override
  Widget build(BuildContext context) {
    final ThemeController themeController = Get.find();
    final AuthController authController = Get.find();

    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          Obx(() {
            if (authController.token.value != null &&
                authController.user.value?.avatarUrl != null) {
              return DrawerHeader(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: NetworkImage(authController.user.value!.avatarUrl!),
                    fit: BoxFit.cover,
                    alignment: Alignment.topCenter,
                  ),
                ),
                child: null,
              );
            } else {
              return DrawerHeader(
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/menu.webp'),
                    fit: BoxFit.cover,
                    alignment: Alignment.topCenter,
                  ),
                ),
                child: null,
              );
            }
          }),
          Obx(() {
            if (authController.token.value != null) {
              return Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    if (authController.user.value?.username != null)
                      Padding(
                        padding: const EdgeInsets.symmetric(vertical: 8.0),
                        child: Text(
                          'Hamlo ${authController.user.value?.username}',
                          style: const TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                    ElevatedButton(
                      onPressed: () async {
                        await authController.logout();
                        _showMsg('Erfolgreich ausgeloggt.');
                      },
                      child: const Text('Logout'),
                    ),
                  ],
                ),
              );
            } else {
              return Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    const Text(
                      'Du bist nicht eingeloggt.',
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 10),
                    ElevatedButton(
                      onPressed: () {
                        Navigator.pop(context);
                        Get.to(() => LoginScreen());
                      },
                      child: const Text('Login'),
                    ),
                  ],
                ),
              );
            }
          }),
          ExpansionTile(
            title: const Text('Theme'),
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16.0),
                child: Obx(() {
                  return Column(
                    children: themeController.themeMap.entries.map((entry) {
                      final String themeName = entry.key;
                      final ThemeData themeData = entry.value;
                      final bool isSelected =
                          themeController.currentTheme.value == themeData;
                      return ListTile(
                        title: Text(themeName),
                        selected: isSelected,
                        selectedTileColor: Colors.blue.withValues(alpha: 0.2),
                        onTap: () async {
                          await themeController.updateTheme(themeName);
                        },
                      );
                    }).toList(),
                  );
                }),
              ),
            ],
          ),
          ListTile(
            title: const Text('Settings'),
            onTap: () {
              Navigator.pop(context);
              Get.bottomSheet(SettingsPage());
            },
          ),
          ListTile(
            title: Text('v${AppVersion.version}'),
            onTap: () {
              Navigator.pop(context);
              _showMsg('jooong lass das, hier ist nichts');
            },
          ),
        ],
      ),
    );
  }
}
