// import 'package:flutter/material.dart';
// // import 'package:test_sa/pie_chart/pie_chart.dart';
// import 'package:provider/provider.dart';
// import 'package:syncfusion_flutter_charts/charts.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;
//
//   AllRequestsProvider _provider;
//
//   @override
//   Widget build(BuildContext context) {
//     _userProvider = Provider.of<UserProvider>(context);
//     bool isCurrentUserNotEngineer = (_userProvider.user.type != UsersTypes.engineer);
//     return Consumer<AllRequestsProvider>(
//       builder: (context, snapshot, _) {
//         if (_provider == null) {
//           _provider = snapshot;
//           if (isCurrentUserNotEngineer) {
//             _provider.getOpenRequests(reset: true);
//           }
//           _provider.getInProgressRequests(reset:true);
//           _provider.getCompletedRequests(reset:true);
//         }
//
//         int total = (snapshot.completedRequests?.total?.count ?? 0) + (snapshot.inProgressRequests?.total?.count ?? 0) + (isCurrentUserNotEngineer ? (snapshot.openRequests?.total?.count ?? 0) : 0);
//
//         final List<ChartData> chartData = [
//           ChartData('Completed', snapshot.completedRequests?.total?.count?.toDouble() ?? 0.0, AppColor.greenStatus(context)),
//           ChartData('In Progress', snapshot.inProgressRequests?.total?.count?.toDouble() ?? 0.0, AppColor.yellowStatus(context)),
//         ];
//
//         if (isCurrentUserNotEngineer) {
//           chartData.insert(1, ChartData('Open', snapshot.openRequests?.total?.count?.toDouble() ?? 0.0, AppColor.blueStatus(context)));
//         }
//
//         return Column(
//           children: [
//             AspectRatio(
//               aspectRatio: 398 / 237,
//               child: Stack(
//                 alignment: Alignment.center,
//                 children: [
//                   SfCircularChart(
//                       tooltipBehavior: TooltipBehavior(
//                         enable: true,
//                         // elevation: 2,
//                         color: context.isDark ? Color(0xFF111427) : Color(0xffF7F9FB),
//                         textStyle: context.isDark ? null : TextStyle(color: Colors.black87, fontSize: 12),
//                       ),
//                       legend: Legend(
//                           //isVisible: true,
//                           ),
//                       series: <CircularSeries>[
//                         DoughnutSeries<ChartData, String>(
//                           dataSource: chartData,
//                           radius: "110%",
//                           innerRadius: "60%",
//                           pointColorMapper: (ChartData data, _) => data.color,
//                           xValueMapper: (ChartData data, _) => data.x,
//                           yValueMapper: (ChartData data, _) => data.y,
//                           dataLabelMapper: (ChartData data, _) => data.x,
//                           dataLabelSettings: DataLabelSettings(
//                             isVisible: true,
//                             textStyle: TextStyle(color: context.isDark ? Colors.white : Colors.black87, fontSize: 12),
//                             labelPosition: ChartDataLabelPosition.outside,
//                             //useSeriesColor: true,
//                           ),
//                         )
//                       ]).toShimmer(isShow: snapshot.isOpenLoading || snapshot.isInProgressLoading || snapshot.isCompleteLoading, radius: 300).paddingAll(12).toShadowContainer(context),
//                   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),
//                       ),
//                     ],
//                   ),
//                 ],
//               ),
//             ).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,
//           ],
//         );
//       },
//     );
//   }
// }
//
// class ChartData {
//   ChartData(this.x, this.y, this.color);
//
//   final String x;
//   final double y;
//   final Color color;
// }
//
//

import 'package:flutter/material.dart';
import 'package:pie_chart/pie_chart.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/dashboard_latest/dashboard_provider.dart';
import 'package:test_sa/extensions/context_extension.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 '../../controllers/providers/api/user_provider.dart';
import '../../models/enums/user_types.dart';

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

  late UserProvider _userProvider;

  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    bool isCurrentUserIsNurse = (_userProvider.user?.type == UsersTypes.normal_user);
    return Consumer<DashBoardProvider>(
      builder: (context, snapshot, _) {
        int total = 0;
        List<ChartData> chartData = [];
        if (isCurrentUserIsNurse) {
          total = (snapshot.dashboardCount?.data?.countComplete ?? 0) + (snapshot.dashboardCount?.data?.countInprogress ?? 0) + (snapshot.dashboardCount?.data?.countOpen ?? 0);
          chartData = [
            ChartData('Acknowledged', snapshot.dashboardCount?.data?.countComplete?.toDouble() ?? 0.0, AppColor.green70),
            ChartData('Open', snapshot.dashboardCount?.data?.countOpen?.toDouble() ?? 0.0, AppColor.blueStatus(context)),
            ChartData('In Progress', snapshot.dashboardCount?.data?.countInprogress?.toDouble() ?? 0.0, AppColor.orange70),
          ];
        } else {
          total = (snapshot.dashboardCount?.data?.countComplete ?? 0) + (snapshot.dashboardCount?.data?.countInprogress ?? 0) + (snapshot.dashboardCount?.data?.countOpen ?? 0);
          chartData = [
            ChartData('Completed', snapshot.dashboardCount?.data?.countComplete?.toDouble() ?? 0.0, AppColor.green70),
            ChartData('Open', snapshot.dashboardCount?.data?.countOpen?.toDouble() ?? 0.0, AppColor.blueStatus(context)),
            ChartData('In Progress', snapshot.dashboardCount?.data?.countInprogress?.toDouble() ?? 0.0, AppColor.orange70),
          ];
        }
        //
        // int total =
        //     (snapshot.dashboardCount?.data?.countComplete ?? 0) + (snapshot.inProgressRequests?.total?.count ?? 0) + (isCurrentUserNotEngineer ? (snapshot.openRequests?.total?.count ?? 0) : 0);
        //
        // final List<ChartData> chartData = [
        //   ChartData('Completed', snapshot.completedRequests?.total?.count?.toDouble() ?? 0.0, AppColor.green70),
        //   // snapshot.openRequests?.total?.count!=0? ChartData('Open', snapshot.openRequests?.total?.count?.toDouble() ?? 0.0, AppColor.blueStatus(context)),
        //   ChartData('In Progress', snapshot.inProgressRequests?.total?.count?.toDouble() ?? 0.0, AppColor.orange70),
        // ];
        //
        // if (isCurrentUserIsNurse) {
        //   chartData.insert(1, ChartData('Open', snapshot.openRequests?.total?.count?.toDouble() ?? 0.0, AppColor.blueStatus(context)));
        // }

        return Column(
          children: [
            AspectRatio(
              aspectRatio: 398 / 237,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  PieChart(
                    dataMap: {for (var v in chartData) v.x: v.y},
                    animationDuration: const Duration(milliseconds: 800),
                    chartLegendSpacing: 32,
                    chartRadius: MediaQuery.of(context).size.width / 3.2,
                    colorList: chartData.map((element) => element.color).toList(),
                    initialAngleInDegree: 270,
                    chartType: ChartType.ring,
                    ringStrokeWidth: 32,
                    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(
                      showLegendsInRow: false,
                      legendPosition: LegendPosition.right,
                      showLegends: true,
                      legendTextStyle: TextStyle(
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    chartValuesOptions: const ChartValuesOptions(
                      showChartValueBackground: false,
                      showChartValues: false,
                      showChartValuesInPercentage: false,
                      showChartValuesOutside: false,
                      decimalPlaces: 1,
                    ),
                  ).toShimmer(isShow: snapshot.isAllCountLoading, radius: 250,context: context).paddingAll(0).toShadowContainer(context),
                ],
              ),
            ),
          ],
        ).paddingOnly(start: 16, end: 16);
      },
    );
  }
}

class ChartData {
  ChartData(this.x, this.y, this.color);

  final String x;
  final double y;
  final Color color;
}
