import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/classes/colors.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/provider/dashboard_provider_model.dart';
import 'package:mohem_flutter_app/ui/landing/widget/app_drawer.dart';
import 'package:mohem_flutter_app/ui/landing/widget/menus_widget.dart';
import 'package:mohem_flutter_app/ui/landing/widget/services_widget.dart';
import 'package:mohem_flutter_app/widgets/circular_avatar.dart';
import 'package:mohem_flutter_app/widgets/shimmer/dashboard_shimmer_widget.dart';
import 'package:provider/provider.dart';

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

  @override
  _DashboardScreenState createState() {
    return _DashboardScreenState();
  }
}

class _DashboardScreenState extends State<DashboardScreen> {
  late DashboardProviderModel data;
  final GlobalKey<ScaffoldState> _scaffoldState = GlobalKey();
  @override
  void initState() {
    super.initState();
    data = Provider.of<DashboardProviderModel>(context, listen: false);
    data.fetchAttendanceTracking(context);
    data.fetchWorkListCounter(context);
    data.fetchMissingSwipe(context);
    data.fetchLeaveTicketBalance(context);
    data.fetchMenuEntries();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    List<String> namesD = ["Nostalgia Perfume Perfume", "Al Nafoura", "AlJadi", "Nostalgia Perfume"];
    final GlobalKey<ScaffoldState> _key = GlobalKey(); //
    return Scaffold(
      key: _scaffoldState,
      body: Column(
        children: [
          Row(
            children: [
              Builder(builder: (context) {
                return Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    CircularAvatar(
                      width: 34,
                      height: 34,
                      url: "https://cdn4.iconfinder.com/data/icons/professions-2-2/151/89-512.png",
                    ),
                    8.width,
                    SvgPicture.asset("assets/images/side_nav.svg"),
                  ],
                ).onPress(() {
                  _scaffoldState.currentState!.openDrawer();
                });
              }),
              Expanded(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    //AppLogo(),
                    8.width,
                    LocaleKeys.mohemm.tr().toText14()
                  ],
                ),
              ),
              SizedBox(
                width: 36,
                height: 36,
                child: Stack(
                  alignment: Alignment.centerLeft,
                  children: [
                    SvgPicture.asset("assets/images/announcements.svg"),
                    Positioned(
                      right: 0,
                      top: 0,
                      child: Container(
                        padding: const EdgeInsets.only(left: 5, right: 5),
                        decoration: BoxDecoration(color: MyColors.redColor, borderRadius: BorderRadius.circular(17)),
                        child: "3".toText12(color: Colors.white),
                      ),
                    )
                  ],
                ),
              ).onPress(() async {
                await Navigator.pushNamed(context, AppRoutes.announcements);

              })
            ],
          ).paddingOnly(left: 21, right: 21, top: 48, bottom: 7),
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      LocaleKeys.goodMorning.tr().toText14(color: MyColors.grey77Color),
                      "Mahmoud Shrouf".toText24(isBold: true),
                      16.height,
                      Row(
                        children: [
                          Expanded(
                            child: AspectRatio(
                              aspectRatio: 159 / 159,
                              child: Consumer<DashboardProviderModel>(
                                builder: (context, model, child) {
                                  return (model.isAttendanceTrackingLoading
                                          ? GetAttendanceTrackingShimmer()
                                          : Container(
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.circular(15),
                                                gradient: const LinearGradient(transform: GradientRotation(.46), begin: Alignment.topRight, end: Alignment.bottomLeft, colors: [
                                                  MyColors.gradiantEndColor,
                                                  MyColors.gradiantStartColor,
                                                ]),
                                              ),
                                              child: Stack(
                                                alignment: Alignment.center,
                                                children: [
                                                  if (model.isTimeRemainingInSeconds == 0) SvgPicture.asset("assets/images/thumb.svg"),
                                                  Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    children: [
                                                      Expanded(
                                                        child: Column(
                                                          mainAxisSize: MainAxisSize.min,
                                                          crossAxisAlignment: CrossAxisAlignment.start,
                                                          children: [
                                                            LocaleKeys.markAttendance.tr().toText14(color: Colors.white, isBold: true),
                                                            if (model.isTimeRemainingInSeconds == 0) "01-02-2022".toText12(color: Colors.white),
                                                            if (model.isTimeRemainingInSeconds != 0)
                                                              Column(
                                                                mainAxisSize: MainAxisSize.min,
                                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                                children: [
                                                                  9.height,
                                                                  CountdownTimer(
                                                                    endTime: model.endTime,
                                                                    onEnd: null,
                                                                    endWidget: "00:00:00".toText14(color: Colors.white, isBold: true),
                                                                    textStyle: TextStyle(color: Colors.white, fontSize: 14, letterSpacing: -0.48, fontWeight: FontWeight.bold),
                                                                  ),
                                                                  LocaleKeys.timeLeftToday.tr().toText12(color: Colors.white),
                                                                  9.height,
                                                                  ClipRRect(
                                                                    borderRadius: BorderRadius.all(
                                                                      Radius.circular(20),
                                                                    ),
                                                                    child: LinearProgressIndicator(
                                                                      value: model.progress,
                                                                      minHeight: 8,
                                                                      valueColor: const AlwaysStoppedAnimation<Color>(Colors.white),
                                                                      backgroundColor: const Color(0xff196D73),
                                                                    ),
                                                                  ),
                                                                ],
                                                              ),
                                                          ],
                                                        ).paddingOnly(top: 12, right: 15, left: 12),
                                                      ),
                                                      Row(
                                                        children: [
                                                          Expanded(
                                                            child: Column(
                                                              mainAxisSize: MainAxisSize.min,
                                                              crossAxisAlignment: CrossAxisAlignment.start,
                                                              children: [
                                                                LocaleKeys.checkIn.tr().toText12(color: Colors.white),
                                                                (model.attendanceTracking!.pSwipeIn == null ? "--:--" : model.attendanceTracking!.pSwipeIn)
                                                                    .toString()
                                                                    .toText14(color: Colors.white, isBold: true),
                                                                4.height,
                                                              ],
                                                            ).paddingOnly(left: 12),
                                                          ),
                                                          Container(
                                                            width: 45,
                                                            height: 45,
                                                            padding: const EdgeInsets.only(left: 14, right: 14),
                                                            decoration: const BoxDecoration(
                                                              color: Color(0xff259EA4),
                                                              borderRadius: BorderRadius.only(
                                                                bottomRight: Radius.circular(15),
                                                              ),
                                                            ),
                                                            child: SvgPicture.asset(model.isTimeRemainingInSeconds == 0 ? "assets/images/play.svg" : "assets/images/stop.svg"),
                                                          ),
                                                        ],
                                                      ),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ).onPress(() {
                                              Navigator.pushNamed(context, AppRoutes.todayAttendance);
                                            }))
                                      .animatedSwither();
                                },
                              ),
                            ),
                          ),
                          9.width,
                          Expanded(
                            child: MenusWidget(),
                          ),
                        ],
                      ),
                    ],
                  ).paddingOnly(left: 21, right: 21, top: 7),
                  ServicesWidget(),
                  8.height,
                  Container(
                    width: double.infinity,
                    padding: EdgeInsets.only(top: 31),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(topRight: Radius.circular(50), topLeft: Radius.circular(50)),
                      border: Border.all(color: MyColors.lightGreyEDColor, width: 1),
                    ),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  LocaleKeys.offers.tr().toText12(),
                                  Row(
                                    children: [
                                      LocaleKeys.discounts.tr().toText24(isBold: true),
                                      6.width,
                                      Container(
                                          padding: const EdgeInsets.only(left: 8, right: 8),
                                          decoration: BoxDecoration(
                                            color: MyColors.yellowColor,
                                            borderRadius: BorderRadius.circular(10),
                                          ),
                                          child: LocaleKeys.newString.tr().toText10(isBold: true)),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                            LocaleKeys.viewAllOffers.tr().toText12(isUnderLine: true),
                          ],
                        ).paddingOnly(left: 21, right: 21),
                        SizedBox(
                          height: 103 + 33,
                          child: ListView.separated(
                              shrinkWrap: true,
                              physics: const BouncingScrollPhysics(),
                              padding: const EdgeInsets.only(left: 21, right: 21, top: 13),
                              scrollDirection: Axis.horizontal,
                              itemBuilder: (cxt, index) {
                                return SizedBox(
                                  width: 73,
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                        width: 73,
                                        height: 73,
                                        decoration: BoxDecoration(
                                          borderRadius: const BorderRadius.all(
                                            Radius.circular(100),
                                          ),
                                          border: Border.all(color: MyColors.lightGreyEDColor, width: 1),
                                        ),
                                        child: ClipRRect(
                                          borderRadius: const BorderRadius.all(
                                            Radius.circular(50),
                                          ),
                                          child: Image.network(
                                            "https://play-lh.googleusercontent.com/NPo88ojmhah4HDiposucJmfQIop4z4xc8kqJK9ITO9o-yCab2zxIp7PPB_XPj2iUojo",
                                            fit: BoxFit.cover,
                                          ),
                                        ),
                                      ),
                                      4.height,
                                      Expanded(
                                        child: namesD[6 % (index + 1)].toText12(isCenter: true, maxLine: 2),
                                      ),
                                    ],
                                  ),
                                );
                              },
                              separatorBuilder: (cxt, index) => 8.width,
                              itemCount: 6),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
      drawer: SafeArea(
        child: AppDrawer(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Padding(
              padding: const EdgeInsets.all(4.0),
              child: SvgPicture.asset(
                "assets/icons/home.svg",
                width: 20,
                height: 20,
              ),
            ),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: const EdgeInsets.all(4.0),
              child: SvgPicture.asset(
                "assets/icons/create_req.svg",
                width: 20,
                height: 20,
              ),
            ),
            label: 'Create Request',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: const EdgeInsets.all(4.0),
              child: SvgPicture.asset(
                "assets/icons/work_list.svg",
                width: 20,
                height: 20,
              ),
            ),
            label: 'Work List',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: const EdgeInsets.all(4.0),
              child: SvgPicture.asset(
                "assets/icons/item_for_sale.svg",
                width: 20,
                height: 20,
              ),
            ),
            label: 'Items for Sale',
          ),
        ],
        currentIndex: 0,
        selectedLabelStyle: TextStyle(
          fontSize: 8,
          color: Color(0xff989898),
          fontWeight: FontWeight.w600,
        ),
        unselectedLabelStyle: TextStyle(
          fontSize: 8,
          color: Color(0xff989898),
          fontWeight: FontWeight.w600,
        ),
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Colors.black,
        backgroundColor: Color(0xffF8F8F8),
        onTap: (v) {},
      ),
    );
  }
}
