// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sis_flutter/data/session/session_storage.dart';
import 'package:sis_flutter/services/usuario_session_service.dart';
import 'package:sis_flutter/views/home/home_screen.dart';
import '../models/usuario_model.dart';
import '../themes/themes.dart';
import '../viewmodels/administracion_view_model.dart';
import '../viewmodels/login_view_model.dart';
import '../views/main/main_screen.dart';
import 'widgets_components.dart';

class AppDrawer extends StatelessWidget {
  Usuario? usuario;
  AppDrawer({super.key});

  Future<bool> _showExitConfirmationDialog(BuildContext context) async {
    bool shouldPop = false;
    await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        surfaceTintColor: AppTheme.secondary,
        title: const Text('¿Cerrar sesión?'),
        content: const Text('¿Estás seguro de que quieres cerrar la sesión?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text('Cancelar', style: TextStyle(color: Colors.red)),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              Navigator.pop(context);
              Provider.of<LoginViewModel>(context, listen: false).logOut();
              Navigator.of(context)
                  .pushNamedAndRemoveUntil(MainScreen.route, (route) => false);
            },
            child: const Text('Aceptar'),
          ),
        ],
      ),
    );
    return shouldPop;
  }

  Future<Usuario?> _loadUsuario() async {
    final usuario = await getUsuarioFromSessionStorage();
    return usuario;
  }

  @override
  Widget build(BuildContext context) {
    String? nombre = Provider.of<LoginViewModel>(context).name.toString();
    String? correo = Provider.of<LoginViewModel>(context).email.toString();
    _loadUsuario().then((value) => usuario = value);

    final avm = Provider.of<AdministracionViewModel>(context);
    List<String> permisos = avm.lospermisos;
    return Drawer(
      surfaceTintColor: Colors.white,
      backgroundColor: Colors.white,
      child: Column(
        children: [
          Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: AppTheme.primary,
            ),
            padding: EdgeInsets.only(
              top: MediaQuery.of(context).padding.top,
            ),
            child: Column(
              children: [
                const Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Image(
                    image: AssetImage('assets/edesarrollos_logo.png'),
                    height: 200,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                Text(
                  usuario?.name ?? nombre.toString(),
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
                Text(
                  correo.toString(),
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(
                  height: 10,
                ),
              ],
            ),
          ),
          //HEADER
          Expanded(
              child: ListView(padding: EdgeInsets.zero, children: [
            ListTile(
              leading: circulo(const Icon(Icons.home_filled)),
              title: const Text('Inicio'),
              onTap: () {
                Navigator.pop(context);
                Navigator.of(context).pushNamed(HomeScreen.route);
              },
            ),
            ListTile(
              leading: const Icon(Icons.logout),
              title: const Text('Cerrar sesión'),
              onTap: () {
                _showExitConfirmationDialog(context);
              },
            ),
          ])),
          const Padding(
              padding: EdgeInsets.only(bottom: 10),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Text(
                  'v1.24.10.29+1',
                  style: TextStyle(fontWeight: FontWeight.w300),
                ),
              ))
        ],
      ),
    );
  }
}
