import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:fluttericon/font_awesome_icons.dart';
import 'package:fluttericon/rpg_awesome_icons.dart';
import 'package:fluttericon/typicons_icons.dart';
import 'package:unit2/screens/unit2/homepage.dart/components/dashboard.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/utils/text_container.dart';
import '../../../bloc/user/user_bloc.dart';
import '../../../model/login_data/user_info/role.dart';
import 'components/empty_module.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({Key? key}) : super(key: key);

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  List<Module> roles = [
    Module(name: 'UniT2 module operations', roles: []),
    Module(name: 'DocSms module operations', roles: []),
    Module(name: "RPAss module operations",roles:[] )
  ];
  int? userId;
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return Future.value(true);
      },
      child: BlocBuilder<UserBloc, UserState>(builder: (context, state) {
        if (state is UserLoggedIn) {
          userId = state.userData!.user!.login!.user!.id;
          for (var element in roles) {
            element.roles.clear();
          }
          for (var role in state.userData!.user!.login!.user!.roles!) {
            Role? getRole = role;
            for (var module in role!.modules!) {
              if (module!.name!.toLowerCase() == 'unit2') {
                IconData iconData = iconGenerator(getRole!.name!);
                Roles newRole = Roles(role: getRole, icon: iconData);
                roles[0].roles.add(newRole);
              }
              if (module.name!.toLowerCase() == 'document management') {
                IconData iconData = iconGenerator(getRole!.name!);
                Roles newRole = Roles(role: getRole, icon: iconData);
                roles[1].roles.add(newRole);
              }  if (module.name!.toLowerCase() == 'rpass') {
                IconData iconData = iconGenerator(getRole!.name!);
                Roles newRole = Roles(role: getRole, icon: iconData);
                roles[2].roles.add(newRole);
              }
            }
          }
          return Scaffold(
            appBar: AppBar(
          backgroundColor: primary,
          leading: IconButton(
            onPressed: () {
              ZoomDrawer.of(context)!.toggle();
            },
            icon: const Icon(
              Icons.menu,
              color: Colors.white,
            ),
          ),
          centerTitle: true,
          title: const Text(
            unit2ModuleScreen,
            style: TextStyle(
              fontSize: 18.0,
              color: Colors.white,
            ),
          ),
            ),
            body: state.userData!.user!.login!.user!.roles!.isNotEmpty
            ? DashBoard(
              userId: userId!,
                roles: roles,
              )
            : const NoModule(),
          );
        }
        return Container();
      }),
    );
  }

  IconData iconGenerator(String roleName) {
    IconData? iconData;
    switch (roleName.toLowerCase()) {
      case 'qr code scanner':
        iconData = FontAwesome.qrcode;
        break;
      case 'security guard':
        iconData = FontAwesome5.user_shield;
        break;
      case 'establishment point-person':
        iconData = FontAwesome.building_filled;
        break;
      case 'registration in-charge':
        iconData = FontAwesome.user_plus;
        break;
      case 'process server':
        iconData = Typicons.doc_text;
        break;
      case 'field surveyor':
        iconData = RpgAwesome.telescope;
    }
    return iconData!;
  }
}

class Module {
  final String name;
  final List<Roles> roles;
  Module({required this.name, required this.roles});
}

class Roles {
  final IconData icon;
  final Role role;
  Roles({required this.role, required this.icon});
}
