// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../core/models/models.dart';
import '../../core/services/services.dart';
import '../themes.dart';
import '../../mvvm/viewmodels/login_view_model.dart';
import 'widgets_components.dart';

class AppDrawer extends StatelessWidget {
  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: () async {
              Provider.of<LoginViewModel>(context, listen: false).logOut();
              Navigator.of(context).pushNamedAndRemoveUntil(
                'login',
                (route) => false,
              );
            },
            child: const Text('Aceptar'),
          ),
        ],
      ),
    );
    return shouldPop;
  }

  @override
  Widget build(BuildContext context) {
    BaseService baseService = BaseService();
    String prefijoVersion = baseService.prefijoVersion();
    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: const Column(
              children: [
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Image(
                    image: AssetImage('assets/logo-BN.png'),
                    height: 150,
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            ),
          ),
          // HEADER
          Expanded(
            child: ListView(
              children: [
                ListTile(
                  leading: circulo(const Icon(Icons.restaurant_menu)),
                  title: const Text('Pedidos'),
                  onTap: () => {},
                ),
                ListTile(
                  leading: const Icon(Icons.logout),
                  title: const Text('Cerrar sesión'),
                  onTap: () {
                    _showExitConfirmationDialog(context);
                  },
                ),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 10),
            child: Align(
              alignment: Alignment.bottomCenter,
              child: Text(
                '$prefijoVersion.1.25.02.11',
                style: const TextStyle(fontWeight: FontWeight.w300),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
