import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:generp/Notifiers/Counter.dart';
import 'package:generp/Notifiers/HomeScreenNotifier.dart';
import 'package:generp/Notifiers/LogoutNotifier.dart';
import 'package:generp/Notifiers/ProfileNotifier.dart';
import 'package:generp/screens/LoginScreen.dart';
import 'package:generp/screens/WebERPScreen.dart';
import 'package:generp/screens/WebWhizzdomScreen.dart';
import 'package:generp/screens/serviceEngineer/serviceEngineerDashboard.dart';
import 'package:provider/provider.dart';

import '../Utils/app_colors.dart';
import 'AttendanceScreen.dart';
import 'inventory/InventoryScreen.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Timer? timer;

  @override
  void initState() {
    // TODO: implement initState
    var prov = Provider.of<HomescreenNotifier>(context, listen: false);
    var prof_prov = Provider.of<ProfileNotifer>(context, listen: false);
    Future.microtask(() {
      prov.DashboardApiFunction(context);
    });
    Future.delayed(Duration(milliseconds: 600), () {
      prof_prov.ProfileApiFunction(prov, context);
      prof_prov.VersionApiFunction();

    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer2<HomescreenNotifier, ProfileNotifer>(
      builder: (context, homescreen, profile, child) {
        return Scaffold(
          backgroundColor: AppColors.scaffold_bg_color,

          body: Container(
            child: Column(
              children: [
                Expanded(
                  flex: 3,
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 10),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.vertical(
                        bottom: Radius.circular(30),
                      ),
                    ),
                    child: Container(
                      margin: EdgeInsets.only(
                        bottom: 15,
                        left: 15,
                        right: 15,
                        top: 30,
                      ),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(30),
                        gradient: LinearGradient(
                          colors: [AppColors.profile_card_gradient1, AppColors.profile_card_gradient2],
                        ),
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                            flex: 2,
                            child: SizedBox(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,

                                children: [
                                  CircleAvatar(
                                    radius: 32.5, // 65 / 2
                                    backgroundImage: NetworkImage(profile.profileImage),
                                    backgroundColor: Colors.transparent,
                                  ),

                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Container(
                                        width: 10,
                                        height: 10,
                                        decoration: BoxDecoration(
                                          shape: BoxShape.circle,
                                          color: Colors.green,
                                        ),
                                      ),
                                      SizedBox(width: 5),
                                      Text(
                                        "${homescreen.onlineStatus}",
                                        style: TextStyle(
                                          fontSize: 14,
                                          color: Colors.black,
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 3,
                            child: SizedBox(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    "${profile.employeeName}",
                                    style: TextStyle(
                                      color: AppColors.app_blue,
                                      fontSize: 16,
                                    ),
                                  ),
                                  Text(
                                    "${profile.designation}",
                                    style: TextStyle(
                                      color: AppColors.semi_black,
                                      fontSize: 14,
                                    ),
                                  ),
                                  Text(
                                    "${profile.employeeeID}",
                                    style: TextStyle(
                                      color: AppColors.semi_black,
                                      fontSize: 14,
                                    ),
                                  ),
                                  InkResponse(
                                    onTap: () {
                                      _showProfileBottomSheet(context);
                                    },
                                    child: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.start,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: [
                                        Text(
                                          "View Profile",
                                          style: TextStyle(
                                            color: AppColors.app_blue,
                                            fontSize: 16,
                                          ),
                                        ),
                                        SizedBox(width: 5),
                                        SizedBox(
                                          width: 15,
                                          height: 15,
                                          child: Center(
                                            child: SvgPicture.asset(
                                              "assets/svg/next_button.svg",
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                Expanded(
                  flex: 8,
                  child: Container(
                    padding: EdgeInsets.only(
                      left: 20,
                      right: 20,
                      top: 0,
                      bottom: 10,
                    ),
                    child: GridView.builder(
                      itemCount: 8,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10,
                      ),
                      itemBuilder: (context, index) {
                        var roles = homescreen.roleStatus;
                        final names = [
                          "Attendance",
                          "ERP",
                          "Gen Tracker",
                          "Service Engineer",
                          "Nearby",
                          "Inventory",
                          "Whizzdom",
                          "CRM",
                        ];
                        return InkResponse(
                          onTap: () {
                            switch (names[index]) {
                              case "Attendance":
                                Navigator.push(context,MaterialPageRoute(builder: (context) => AttendanceScreen()));
                                break;
                              case "ERP":
                                Navigator.push(context,MaterialPageRoute(builder: (context) => WebErpScreen(erp_url: homescreen.webPageUrl)));
                                break;
                              case "Gen Tracker":
                                // Navigator.push(context, MaterialPageRoute(builder: (context)=>GenTrackerDashboard()));
                                break;
                              case "Service Engineer":
                                Navigator.push(context, MaterialPageRoute(builder: (context)=>Serviceengineerdashboard()));
                                break;
                              case "Nearby":
                                // Navigator.push(context, MaterialPageRoute(builder: (context)=>NearByGeneratorsScreen()));
                                break;
                              case "Inventory":
                                Navigator.push(context, MaterialPageRoute(builder: (context)=>InventoryScreen()));
                                break;
                              case "Whizzdom":
                                Navigator.push(context, MaterialPageRoute(builder: (context)=>WebWhizzdomScreen(
                                    whizzdom_url:
                                    homescreen.whizzdomPageUrl
                                )));
                                break;
                              case "CRM":
                                // Navigator.push(context, MaterialPageRoute(builder: (context)=>CRMScreen()));
                                break;
                              default:
                                print("111");
                                break;
                            }
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(30),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                SvgPicture.asset(
                                  "assets/svg/home_icons_${index + 1}.svg",
                                ),
                                Text(names[index]),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Future<void> _showProfileBottomSheet(BuildContext context) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,

      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Container(
                margin: EdgeInsets.only(
                  bottom: 15,
                  left: 15,
                  right: 15,
                  top: 30,
                ),

                child: Consumer3<
                  HomescreenNotifier,
                  ProfileNotifer,
                  LogoutNotifier
                >(
                  builder: (context, home, profile, logout, child) {
                    return SingleChildScrollView(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Padding(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 5.0,
                            ),
                            child: Row(
                              children: [
                                SizedBox(
                                  height: 35,
                                  child: Image.asset(
                                    "assets/images/gen_horiz_logo.png",
                                  ),
                                ),
                                Spacer(),
                                SizedBox(
                                  height: 25,
                                  width: 25,
                                  child: SvgPicture.asset(
                                    "assets/svg/scanner.svg",
                                  ),
                                ),
                              ],
                            ),
                          ),
                          SizedBox(height: 15),
                          Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(30),
                              gradient: LinearGradient(
                                colors: [AppColors.profile_card_gradient1, AppColors.profile_card_gradient2],
                              ),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Padding(
                                  padding: EdgeInsets.all(10),
                                  child: CircleAvatar(
                                    radius: 55,
                                    foregroundImage: NetworkImage(
                                      profile.profileImage,
                                    ),
                                  ),
                                ),
                                Text(
                                  "${profile.employeeName}",
                                  style: TextStyle(
                                    color: AppColors.app_blue,
                                    fontSize: 16,
                                  ),
                                ),
                                Text(
                                  "${profile.employeeEmail}",
                                  style: TextStyle(
                                    color: AppColors.semi_black,
                                    fontSize: 14,
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.symmetric(horizontal: 20),
                                  alignment: Alignment.center,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Expanded(
                                        flex: 1,
                                        child: ListView.builder(
                                          shrinkWrap: true,
                                          itemCount: 5,
                                          physics:
                                              NeverScrollableScrollPhysics(),
                                          itemBuilder: (context, index) {
                                            final textHeadings = [
                                              "Company",
                                              "Branch",
                                              "Designation",
                                              "Employee ID",
                                              "Mobile Number",
                                            ];
                                            return SizedBox(
                                              height: 40,
                                              child: Align(
                                                alignment: Alignment.centerLeft,
                                                child: Text(
                                                  "${textHeadings[index]}",

                                                  textAlign: TextAlign.left,
                                                  style: TextStyle(
                                                    fontSize: 14,
                                                    color: AppColors.app_blue,
                                                  ),
                                                ),
                                              ),
                                            );
                                          },
                                        ),
                                      ),
                                      Expanded(
                                        flex: 1,
                                        child: ListView.builder(
                                          shrinkWrap: true,
                                          itemCount: 5,
                                          physics:
                                              NeverScrollableScrollPhysics(),
                                          itemBuilder: (context, index) {
                                            final textHeadings = [
                                              profile.company,
                                              profile.branch,
                                              profile.designation,
                                              profile.employeeeID,
                                              profile.mobileNUmber,
                                            ];
                                            return SizedBox(
                                              height: 40,
                                              child: Align(
                                                alignment: Alignment.centerLeft,
                                                child: Text(
                                                  "${textHeadings[index].toString()}",
                                                  textAlign: TextAlign.left,
                                                  style: TextStyle(
                                                    fontSize: 14,
                                                    color: AppColors.semi_black,
                                                  ),
                                                ),
                                              ),
                                            );
                                          },
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              // LoginApiFunction();

                              var f = FocusScope.of(context);

                              if (!f.hasPrimaryFocus) {
                                f.unfocus();
                              }
                              logout.LogoutApiFunction(home, context);
                              //  Navigator.push(context,MaterialPageRoute(builder: (context)=>Profile()));
                            },
                            child: Container(
                              alignment: Alignment.center,
                              height: 45,
                              margin: EdgeInsets.only(
                                left: 5.0,
                                right: 5.0,
                                top: 5.0,
                                bottom: 5.0,
                              ),
                              decoration: BoxDecoration(
                                color: AppColors.app_blue, //1487C9
                                borderRadius: BorderRadius.circular(30.0),
                              ),
                              child: Center(
                                child: Text(
                                  "Logout",
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),

                          Text(
                            profile.releaseNotes,
                            style: TextStyle(
                              fontSize: 14,
                              color: AppColors.grey_thick,
                            ),
                          ),
                        ],
                      ),
                    );
                  },
                ),
              ),
            );
          },
        );
      },
    );
  }
}
