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/api/all_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/notifications_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.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/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_floating_action_button.dart';
import 'package:test_sa/new_views/pages/land_page/requests_list_page.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // todo check here, nurse can add request not engineer
      floatingActionButton: const AppFloatingActionButton(),
      body: Consumer<AllRequestsProvider>(
        builder: (context, snapshot, _) => RefreshIndicator(
          onRefresh: () {
            snapshot.getRequests();
            Provider.of<NotificationsProvider>(context, listen: false).getSystemNotifications(user: Provider.of<UserProvider>(context, listen: false).user, resetProvider: true);
            return Future.delayed(const Duration(microseconds: 250));
          },
          child: GridView(
            padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 191 / 237, crossAxisSpacing: 16, mainAxisSpacing: 16),
            children: [
              gridItem(
                snapshot.highPriorityRequests?.total?.count,
                "high_priority",
                context.translation.highPriority,
                context,
                snapshot.isHighPriorityLoading,
                0,
                context.isDark ? AppColor.redStatus(context) : AppColor.red50,
              ),
              gridItem(snapshot.overdueRequests?.total?.count, "overdue", context.translation.overdue, context, snapshot.isOverdueLoading, 1, AppColor.yellowStatus(context)),
              gridItem(snapshot.openRequests?.total?.count, "new_request", ApiManager.instance.user.type == UsersTypes.engineer ? context.translation.inProgress : context.translation.newR, context,
                  snapshot.isOpenLoading, 2, AppColor.primary40),
              gridItem(snapshot.completedRequests?.total?.count, "complete_request", context.translation.completed, context, snapshot.isCompleteLoading, 3, AppColor.greenStatus(context)),
            ],
          ),
        ),
      ),
    );
  }

  Widget gridItem(int value, String icon, String title, BuildContext context, bool isLoading, int index, Color iconColor) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              value?.toString() ?? "-",
              style: AppTextStyles.heading1.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50, height: 1),
            ).toShimmer(isShow: isLoading).expanded,
            8.width,
            (icon ?? "").toSvgAsset(height: 48, width: 48, color: iconColor)?.toShimmer(isShow: isLoading),
          ],
        ).expanded,
        Text(
          "$title\n${context.translation.requests}",
          style: AppTextStyles.heading5.copyWith(
            color: context.isDark ? AppColor.neutral30 : AppColor.neutral50,
          ),
        ).toShimmer(isShow: isLoading),
        8.height,
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              context.translation.viewDetails,
              style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.primary40 : AppColor.primary50),
            ),
            4.width,
            Icon(
              Icons.arrow_forward,
              color: context.isDark ? AppColor.primary40 : AppColor.primary50,
              size: 14,
            )
          ],
        ).toShimmer(isShow: isLoading),
      ],
    ).toShadowContainer(context).onPress(isLoading
        ? null
        : () async {
            await Navigator.push(context, MaterialPageRoute(builder: (context) => RequestsListPage(index)));
            Provider.of<AllRequestsProvider>(context, listen: false).getRequests();
          });
  }
}
