import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/app_strings/app_asset.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/dashboard_latest/dashboard_provider.dart';
import 'package:test_sa/dashboard_latest/widgets/request_category_list.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/new_models/dashboard_detail.dart';
import 'package:test_sa/new_views/common_widgets/tab_button.dart';
import 'package:test_sa/service_request_latest/views/components/weekly_calendar_fragment.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

class RequestCategoryFragment extends StatelessWidget {
  const RequestCategoryFragment({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<DashBoardProvider>(builder: (context, dashboardProvider, child) {
      bool isNurse = (Provider.of<UserProvider>(context, listen: false).user?.type) == UsersTypes.normal_user;

      return Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
        //TODO uncommit this to get the calendar card..
        !isNurse && dashboardProvider.tabs.isNotEmpty && dashboardProvider.tabs[dashboardProvider.currentListIndex].tag == 0
            ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  context.translation.allWorkOrder.heading4(context),
                  // Row(
                  //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  //   children: [
                  //     context.translation.allWorkOrder.heading4(context),
                  //     GestureDetector(
                  //         onTap: () {
                  //           dashboardProvider.isShowDashBoardCalendar = !dashboardProvider.isShowDashBoardCalendar;
                  //         },
                  //         child: dashboardProvider.isShowDashBoardCalendar ? AppAsset.listIcon.toSvgAsset() : AppAsset.calendarIcon.toSvgAsset())
                  //   ],
                  // ),
                  dashboardProvider.isShowDashBoardCalendar
                      ? Padding(
                          padding: EdgeInsets.only(top: 12.toScreenHeight),
                          child: const WeeklyCalendarFragment(),
                        )
                      : const SizedBox(),
                ],
              ).paddingOnly(start: 14, end: 12)
            : context.translation.allWorkOrder.heading4(context).paddingOnly(start: 16, end: 16),
        getTabs(
          requestsProvider: dashboardProvider,
          context: context,
          userType: Provider.of<UserProvider>(context, listen: false).user?.type!,
        ),
        dashboardProvider.isDetailLoading
            ? Column(
                mainAxisSize: MainAxisSize.max,
                children: List.generate(3, (index) {
                  return Padding(
                    padding: EdgeInsets.symmetric(vertical: 10.toScreenHeight, horizontal: 0),
                    child: const SizedBox().toRequestShimmer(context, dashboardProvider.isDetailLoading),
                  );
                })).paddingOnly(start: 16, end: 16, top: 16)
            : (dashboardProvider.requestDetailList==null||dashboardProvider.requestDetailList?.data?.isEmpty==true)
                ? const NoDataFound().paddingOnly(top: 80).center
                : RequestCategoryList(dashboardProvider.requestDetailList?.data ?? <Data>[], dashboardProvider.isDetailLoading, dashboardProvider.requestDetailList?.totalRows ?? 0),
      ]);
    });
  }

  Widget getTabs({required BuildContext context, required DashBoardProvider requestsProvider, required UsersTypes? userType}) {
    List<CategoryTabs> tabs = CategoryTabs.getTabs(userType: userType ?? UsersTypes.normal_user, context: context);
    return SizedBox(
      height: 44 + 16,
      child: ListView.separated(
          scrollDirection: Axis.horizontal,
          separatorBuilder: (cxt, index) => 12.width,
          padding: const EdgeInsets.only(top: 6 + 16, left: 16, right: 16),
          itemBuilder: (cxt, index) {
            return TabButton(
              label: tabs[index].label,
              loading: requestsProvider.isDetailLoading,
              isSelected: requestsProvider.currentListIndex == index,
              count: requestsProvider.requestDetailList != null ? requestsProvider.requestDetailList!.totalRows ?? 0 : 0,
              onPressed: () async {
                requestsProvider.currentListIndex = index;
                requestsProvider.resetRequestDataList();
                requestsProvider.getRequestDetail(usersType: userType ?? UsersTypes.normal_user, showLoader: true, status: tabs[index].tag, tabId: tabs[index].id);
              },
            );
          },
          itemCount: tabs.length),
    );
  }
}
