import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/dashboard/menu_entries.dart';
import 'package:mohem_flutter_app/provider/dashboard_provider_model.dart';
import 'package:mohem_flutter_app/ui/my_attendance/dynamic_screens/dynamic_listview_screen.dart';
import 'package:mohem_flutter_app/ui/my_attendance/services_menu_list_screen.dart';
import 'package:mohem_flutter_app/widgets/shimmer/dashboard_shimmer_widget.dart';
import 'package:provider/provider.dart';

class ServicesWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<String> namesT = [LocaleKeys.monthlyAttendance.tr(), LocaleKeys.workFromHome.tr(), LocaleKeys.ticketRequest.tr(), LocaleKeys.monthlyAttendance.tr()];
    List<String> iconT = [
      "assets/images/monthly_attendance.svg",
      "assets/images/monthly_attendance.svg",
      "assets/images/ticket_request.svg",
      "assets/images/ticket_request.svg",
      "assets/images/ticket_request.svg",
      "assets/images/ticket_request.svg",
      "assets/images/ticket_request.svg",
      "assets/images/ticket_request.svg"
    ];

    return Consumer<DashboardProviderModel>(
      builder: (context, data, child) {
        return data.isServicesMenusLoading
            ? whileLoading()
            : ListView.separated(
                padding: const EdgeInsets.only(top: 0),
                itemBuilder: (context, parentIndex) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [ 
                      data.homeMenus![parentIndex].menuEntry.prompt!.toSectionHeading().paddingOnly(left: 21, right: 21),
                      SizedBox(
                        height: 105 + 26,
                        child: ListView.separated(
                            shrinkWrap: true,
                            physics: const BouncingScrollPhysics(),
                            padding: const EdgeInsets.only(left: 21, right: 21, top: 13, bottom: 13),
                            scrollDirection: Axis.horizontal,
                            itemBuilder: (cxt, index) {
                              return AspectRatio(
                                aspectRatio: 105 / 105,
                                child: data.isServicesMenusLoading
                                    ? ServicesMenuShimmer()
                                    : Container(
                                        decoration: BoxDecoration(
                                          color: Colors.white,
                                          borderRadius: BorderRadius.circular(15),
                                          boxShadow: [
                                            BoxShadow(
                                              color: const Color(0xff000000).withOpacity(.05),
                                              blurRadius: 26,
                                              offset: const Offset(0, -3),
                                            ),
                                          ],
                                        ),
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            SvgPicture.asset(iconT[index]),
                                            Row(
                                              crossAxisAlignment: CrossAxisAlignment.end,
                                              children: [
                                                Expanded(
                                                  child: data.homeMenus![parentIndex].menuEntiesList[index].prompt!.toText10(isBold: true),
                                                ),
                                                RotatedBox(quarterTurns: AppState().isArabic(context) ? 2 : 4, child: SvgPicture.asset("assets/images/arrow_next.svg").paddingOnly(bottom: 4)),
                                              ],
                                            )
                                          ],
                                        ).paddingOnly(left: 10, right: 10, bottom: 10, top: 12),
                                      ).onPress(() {
                                        handleOnPress(context, data.homeMenus![parentIndex].menuEntiesList[index]);
                                      }),
                              );
                            },
                            separatorBuilder: (cxt, index) => 9.width,
                            itemCount: data.homeMenus![parentIndex].menuEntiesList.length),
                      ),
                    ],
                  );
                },
                separatorBuilder: (context, index) {
                  return 12.height;
                },
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: data.homeMenus!.length);
      },
    );
  }

  void handleOnPress(context, GetMenuEntriesList menuEntry) {
    var pro = Provider.of<DashboardProviderModel>(context, listen: false);
    if (menuEntry.requestType == "MONTHLY_ATTENDANCE") {
      Navigator.pushNamed(context, AppRoutes.monthlyAttendance);
      return;
    } else if (menuEntry.requestType == "VACATION_RULE") {
      Navigator.pushNamed(context, AppRoutes.vacationRule);
      return;
    } else if (menuEntry.menuName == "MBL_PERINFO_SS") {
      Navigator.of(context).pushNamed(AppRoutes.profile);
      return;
    }
    List<GetMenuEntriesList> menuList = pro.getMenuEntriesList?.where((element) => element.parentMenuName == menuEntry.menuName && (element.menuEntryType == "FUNCTION")).toList() ?? [];
    menuEntry.icon = "";
    if (menuList.isEmpty) {
      if (menuEntry.requestType == "EIT") {
        Navigator.pushNamed(context, AppRoutes.dynamicScreen, arguments: DynamicListViewParams(menuEntry.prompt!, menuEntry.functionName!));
      } else if (menuEntry.requestType == "PAYSLIP") {
        Navigator.pushNamed(context, AppRoutes.monthlyPaySlip);
      }
    } else {
      List<GetMenuEntriesList> _menuList =
          pro.getMenuEntriesList?.where((element) => element.parentMenuName == menuEntry.menuName && (element.menuEntryType == "FUNCTION" || element.menuEntryType == "MENU")).toList() ?? [];
      Navigator.pushNamed(context, AppRoutes.servicesMenuListScreen, arguments: ServicesMenuListScreenParams(menuEntry.prompt!, _menuList.isEmpty ? menuList : _menuList));
    }
    return;
  }

  String firstWord(String value) {
    return value.split(" ").length > 1 ? value.split(" ")[0] : "";
  }

  String lastWord(String value) {
    var parts = value.split(" ");
    if (parts.length == 1) {
      return value;
    } else {
      int i = value.indexOf(" ");
      return value.substring(i + 1).toCamelCase;
    }
  }

  Widget whileLoading() {
    return Column(
      children: [
        ServicesHeaderShimmer().paddingOnly(left: 21, right: 21, top: 21),
        SizedBox(
          height: 105 + 26,
          child: ListView.separated(
            shrinkWrap: true,
            physics: const BouncingScrollPhysics(),
            padding: const EdgeInsets.only(left: 21, right: 21, top: 13, bottom: 13),
            scrollDirection: Axis.horizontal,
            itemBuilder: (cxt, index) {
              return AspectRatio(
                aspectRatio: 105 / 105,
                child: ServicesMenuShimmer(),
              );
            },
            separatorBuilder: (cxt, index) => 9.width,
            itemCount: 4,
          ),
        ),
      ],
    );
  }
}
