import 'package:flutter/material.dart';
import 'package:pie_chart/pie_chart.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/all_requests_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/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/pages/land_page/widgets/request_item_view_list.dart';

import '../../../../controllers/providers/api/user_provider.dart';
import '../../../../models/enums/user_types.dart';

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

  UserProvider _userProvider;

  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    bool isCurrentUserNotEngineer = (_userProvider.user.type != UsersTypes.engineer);
    return Consumer<AllRequestsProvider>(
      builder: (context, snapshot, _) {
        Map<String, double> statuses = {
          "Completed": snapshot.completedRequests?.requestsDetails?.length?.toDouble() ?? 0.0,
          "In Progress": snapshot.inProgressRequests?.requestsDetails?.length?.toDouble() ?? 0.0,
          // "Open": snapshot.openRequests?.requestsDetails?.length?.toDouble() ?? 0.0,
        };
        if (isCurrentUserNotEngineer) {
          statuses["Open"] = snapshot.openRequests?.requestsDetails?.length?.toDouble() ?? 0.0;
        }
        int total = (snapshot.completedRequests?.requestsDetails?.length ?? 0) +
            (snapshot.inProgressRequests?.requestsDetails?.length ?? 0) +
            (isCurrentUserNotEngineer ? (snapshot.openRequests?.requestsDetails?.length ?? 0) : 0);

        return Column(
          children: [
            AspectRatio(
              aspectRatio: 398 / 237,
              child: PieChart(
                dataMap: statuses,
                animationDuration: const Duration(milliseconds: 500),
                chartRadius: 190.toScreenWidth,
                colorList: [
                  AppColor.greenStatus(context),
                  AppColor.yellowStatus(context),
                  AppColor.blueStatus(context),
                ],
                initialAngleInDegree: 270,
                chartType: ChartType.ring,
                ringStrokeWidth: 40.toScreenWidth,
                sideTextStyle: AppTextStyles.heading6.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
                centerWidget: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(
                      context.translation.total,
                      style: AppTextStyles.heading5.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
                    ),
                    Text(
                      "$total",
                      style: AppTextStyles.heading6.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
                    ),
                  ],
                ),
                legendOptions: const LegendOptions(showLegends: false),
                chartValuesOptions: ChartValuesOptions(
                  chartValueBackgroundColor: Theme.of(context).scaffoldBackgroundColor,
                  showChartValuesInPercentage: true,
                  chartValueStyle: AppTextStyles.bodyText2.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral60),
                  showChartValuesOutside: true,
                ),
              ).toShimmer(isShow: snapshot.isOpenLoading || snapshot.isInProgressLoading || snapshot.isCompleteLoading, radius: 300).paddingAll(12).toShadowContainer(context),
            ).paddingOnly(start: 16, end: 16, bottom: 16),
            DefaultTabController(
              length: isCurrentUserNotEngineer ? 3 : 2,
              child: Column(
                children: <Widget>[
                  Container(
                    margin: const EdgeInsets.only(left: 16, right: 16),
                    decoration: BoxDecoration(color: context.isDark ? AppColor.neutral50 : AppColor.neutral30, borderRadius: BorderRadius.circular(16)),
                    child: TabBar(
                      //controller: _tabController,
                      padding: EdgeInsets.zero,
                      labelColor: context.isDark ? AppColor.neutral30 : AppColor.neutral60,
                      unselectedLabelColor: context.isDark ? AppColor.neutral10 : AppColor.neutral20,
                      unselectedLabelStyle: AppTextStyles.bodyText,
                      labelStyle: AppTextStyles.bodyText,
                      indicatorPadding: const EdgeInsets.all(4),
                      indicator: BoxDecoration(color: context.isDark ? AppColor.neutral60 : Theme.of(context).cardColor, borderRadius: BorderRadius.circular(13)),
                      onTap: (index) {
                        // setState(() {});
                      },
                      tabs: [
                        if (isCurrentUserNotEngineer) Tab(text: context.translation.open, height: 57.toScreenHeight),
                        Tab(text: context.translation.inProgress, height: 57.toScreenHeight),
                        Tab(text: context.translation.completed, height: 57.toScreenHeight),
                      ],
                    ),
                  ),
                  8.height,
                  TabBarView(
                    children: [
                      if (isCurrentUserNotEngineer) RequestItemViewList(snapshot.openRequests?.requestsDetails ?? [], snapshot.isOpenLoading),
                      RequestItemViewList(snapshot.inProgressRequests?.requestsDetails ?? [], snapshot.isInProgressLoading),
                      RequestItemViewList(snapshot.completedRequests?.requestsDetails ?? [], snapshot.isCompleteLoading),
                    ],
                  ).expanded,
                ],
              ),
            ).expanded,
          ],
        );
      },
    );
  }
//
// Widget requestItemView(List<RequestsDetails> list, bool isLoading) {
//   return ListView.separated(
//       padding: const EdgeInsets.all(16),
//       itemBuilder: (cxt, index) {
//         if (isLoading) return SizedBox().toRequestShimmer(cxt, isLoading);
//         bool isServiceRequest = list[index].nameOfType == "ServiceRequest";
//         bool isGasRefill = list[index].nameOfType == "GasRefill";
//         bool isAssetTransfer = list[index].nameOfType == "AssetTransfer";
//         bool isPPMs = list[index].nameOfType == "PPMs";
//
//         return isServiceRequest
//             ? ServiceRequestItemView(list[index])
//             : isGasRefill
//                 ? GasRefillItemView(list[index])
//                 : isPPMs
//                     ? PpmItemView(list[index])
//                     : isAssetTransfer
//                         ? AssetItemView(list[index])
//                         : Container(
//                             height: 100,
//                             width: double.infinity,
//                             color: Colors.grey,
//                           );
//       },
//       separatorBuilder: (cxt, index) => 8.height,
//       itemCount: isLoading ? 6 : list.length);
// }
}
