// older code....
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/dashboard_latest/dashboard_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/enum_extensions.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/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/custom_badge.dart';
import 'package:test_sa/new_views/pages/land_page/requests_list_page.dart';
import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
import 'package:test_sa/models/new_models/dashboard_count.dart';

/// Helper class for Dashboard count data selection
/// This enables Selector to only rebuild when these specific properties change
class _DashboardCountData {
  final DashboardCount? dashboardCount;
  final bool isLoading;

  const _DashboardCountData({
    required this.dashboardCount,
    required this.isLoading,
  });

  @override
  bool operator ==(Object other) =>
      identical(this, other) || other is _DashboardCountData && runtimeType == other.runtimeType && dashboardCount == other.dashboardCount && isLoading == other.isLoading;

  @override
  int get hashCode => Object.hash(dashboardCount, isLoading);
}

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

  @override
  Widget build(BuildContext context) {
    return Selector<DashBoardProvider, _DashboardCountData>(
        selector: (_, provider) => _DashboardCountData(dashboardCount: provider.dashboardCount, isLoading: provider.isAllCountLoading),
        builder: (context, data, _) {
          List<Widget> listItems = [];
          if (context.userProvider.isNurse) {
            listItems = [
              requestType(data.dashboardCount?.data?.countOpen ?? 0, "dashboard/open_icon", context.translation.open, context, data.isLoading, RequestTypeEnum.open, AppColor.openColor),
              requestType(data.dashboardCount?.data?.countInprogress ?? 0, "dashboard/inprogress_icon", context.translation.inProgress, context, data.isLoading, RequestTypeEnum.inProgress,
                  AppColor.primary10),
              requestType(
                  data.dashboardCount?.data?.countAcknowledge ?? 0, "complete_request", "Acknowledgement", context, data.isLoading, RequestTypeEnum.pendingAcknowledgment, AppColor.overdueColor),
              requestType(data.dashboardCount?.data?.countCanceled ?? 0, "dashboard/unassigned", "Canceled", context, data.isLoading, RequestTypeEnum.canceled, AppColor.red30),
            ];
          } else {
            listItems = [
              requestType(
                  data.dashboardCount?.data?.countUnAssigned ?? 0, "dashboard/unassigned", "UnAssigned", context, data.isLoading, RequestTypeEnum.unassigned, AppColor.unassignedStatus(context)),
              requestType(data.dashboardCount?.data?.countOpen ?? 0, "dashboard/open_icon", context.translation.open, context, data.isLoading, RequestTypeEnum.open, AppColor.openColor),
              requestType(data.dashboardCount?.data?.countInprogress ?? 0, "dashboard/inprogress_icon", context.translation.inProgress, context, data.isLoading, RequestTypeEnum.inProgress,
                  AppColor.primary10),
              requestType(
                  data.dashboardCount?.data?.countComplete ?? 0, "complete_request", context.translation.completed, context, data.isLoading, RequestTypeEnum.completed, AppColor.completedColor),
              requestType(data.dashboardCount?.data?.countOverdue ?? 0, "overdue", context.translation.overdue, context, data.isLoading, RequestTypeEnum.overdue, AppColor.yellowIcon(context)),
              requestType(data.dashboardCount?.data?.countHighPriority ?? 0, "high_priority", context.translation.highPriority, context, data.isLoading, RequestTypeEnum.highPriority, AppColor.red30),
            ];
          }

          return GridView(
            padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: context.isTablet() ? 5 : 4, childAspectRatio: 85 / 85, crossAxisSpacing: 8, mainAxisSpacing: 8),
            children: listItems,
          );
        });
  }

  Widget requestType(int value, String icon, String title, BuildContext context, bool isLoading, RequestTypeEnum requestType, Color iconColor) {
    return CustomBadge(
      value: isLoading ? 0 : value,
      positionR: -4,
      positionT: -4,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.end,
        spacing: 0,
        children: [
          (icon ?? "").toSvgAsset(height: 26, width: 26, color: iconColor).toShimmer(isShow: isLoading, context: context).center.expanded,
          Text(
            title,
            textAlign: TextAlign.center,
            style: AppTextStyles.textStyle9.copyWith(color: AppColor.headingTextColor(context)),
          ).toShimmer(isShow: isLoading, context: context),
        ],
      ).toShadowContainer(context, borderRadius: 16, paddingObject: const EdgeInsets.symmetric(horizontal: 12, vertical: 12)),
    )
        .onPress(isLoading
            ? null
            : () async {
                await Navigator.push(context, MaterialPageRoute(builder: (context) => RequestsListPage(1, title)));
                Provider.of<DashBoardProvider>(context, listen: false).refreshDashboard(userType: Provider.of<SettingProvider>(context, listen: false).user!.type!, context: context);
              })
        .paddingOnly(end: 4, top: 4);
  }
}
