import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/dashboard_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/date_uitl.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/dashboard/get_accrual_balances_list_model.dart';
import 'package:mohem_flutter_app/provider/dashboard_provider_model.dart';
import 'package:pie_chart/pie_chart.dart';
import 'package:provider/provider.dart';

class PieChartModel {
  String title;
  String titleAppend;
  double value;
  Color color;

  PieChartModel(this.title, this.value, this.color, {this.titleAppend = ""});

  Map<String, double> get chartMapValue => {title: value};

  double get parsedValue => value > 0 ? value : value + 0.1;
}

class BalancesDashboardWidget extends StatefulWidget {
  final String title;
  final List<PieChartModel> chartModelList;
  final bool isLeaveBalance;

  const BalancesDashboardWidget(this.title, this.isLeaveBalance, {Key? key, this.chartModelList = const []}) : super(key: key);

  @override
  _BalancesDashboardWidgetState createState() {
    return _BalancesDashboardWidgetState();
  }
}

class _BalancesDashboardWidgetState extends State<BalancesDashboardWidget> {
  List<PieChartModel> chartModelList = [];
  late DateTime accrualDateTime;
  GetAccrualBalancesList? leaveBalanceAccrual;
  List<GetAccrualBalancesList>? ticketBalanceAccrualList;

  @override
  void initState() {
    super.initState();
    accrualDateTime = DateTime.now();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void changeAccrualDate() async {
    try {
      Utils.showLoading(context);
      List<GetAccrualBalancesList> accrualList = await DashboardApiClient().getAccrualBalances(DateFormat("MM/dd/yyyy").format(accrualDateTime));
      if (accrualList.isNotEmpty) {
        if (widget.isLeaveBalance) {
          leaveBalanceAccrual = accrualList[0];
          chartModelList = [
            PieChartModel(LocaleKeys.currentBalance.tr(), leaveBalanceAccrual?.accrualNetEntitlement ?? 0, MyColors.textMixColor, titleAppend: ""),
            PieChartModel(LocaleKeys.usedBalance.tr(), leaveBalanceAccrual?.accrualUsedEntitlement?.toDouble() ?? 0, MyColors.backgroundBlackColor, titleAppend: ""),
          ];
        } else {
          chartModelList = [
            PieChartModel(LocaleKeys.adult.tr(), accrualList[1].accrualNetEntitlement ?? 0, MyColors.textMixColor, titleAppend: ""),
            PieChartModel(LocaleKeys.child.tr(), accrualList[2].accrualNetEntitlement?.toDouble() ?? 0, MyColors.backgroundBlackColor, titleAppend: ""),
            PieChartModel(LocaleKeys.infants.tr(), accrualList[3].accrualNetEntitlement?.toDouble() ?? 0, MyColors.pinkColor, titleAppend: ""),
          ];
        }
      }
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    if (leaveBalanceAccrual == null && widget.isLeaveBalance) {
      leaveBalanceAccrual = Provider.of<DashboardProviderModel>(context, listen: false).leaveBalanceAccrual;
      chartModelList = [
        PieChartModel(LocaleKeys.currentBalance.tr(), leaveBalanceAccrual?.accrualNetEntitlement ?? 0, MyColors.textMixColor, titleAppend: ""),
        PieChartModel(LocaleKeys.usedBalance.tr(), leaveBalanceAccrual?.accrualUsedEntitlement?.toDouble() ?? 0, MyColors.backgroundBlackColor, titleAppend: ""),
      ];
    }

    if (ticketBalanceAccrualList == null && !widget.isLeaveBalance) {
      ticketBalanceAccrualList = Provider.of<DashboardProviderModel>(context, listen: false).accrualList ?? [];
      if (ticketBalanceAccrualList!.isNotEmpty) {
        chartModelList = [
          PieChartModel(LocaleKeys.adult.tr(), ticketBalanceAccrualList![1].accrualNetEntitlement ?? 0, MyColors.textMixColor, titleAppend: ""),
          PieChartModel(LocaleKeys.child.tr(), ticketBalanceAccrualList![2].accrualNetEntitlement?.toDouble() ?? 0, MyColors.backgroundBlackColor, titleAppend: ""),
          PieChartModel(LocaleKeys.infants.tr(), ticketBalanceAccrualList![3].accrualNetEntitlement?.toDouble() ?? 0, MyColors.pinkColor, titleAppend: ""),
        ];
      }
    }

    return Column(
      children: [
        Row(
          children: [
            widget.title.toText20().expanded,
            Row(
              children: [
                const Icon(Icons.calendar_month_rounded, color: MyColors.darkIconColor, size: 16),
                5.width,
                DateUtil.formatDateToDate(accrualDateTime, AppState().isArabic(context)).toText13(isUnderLine: true),
                8.width
              ],
            ).onPress(() async {
              DateTime selectedDate = await Utils.selectDate(context, accrualDateTime);
              if (selectedDate != accrualDateTime) {
                accrualDateTime = selectedDate;
                changeAccrualDate();
              }
            }),
          ],
        ),
        8.height,
        Row(
          children: [
            ListView.separated(
                    shrinkWrap: true,
                    padding: const EdgeInsets.only(top: 8, bottom: 8),
                    physics: const NeverScrollableScrollPhysics(),
                    itemBuilder: (cxt, index) {
                      return chartLegend(chartModelList[index], isFirst: index == 0);
                    },
                    separatorBuilder: (cxt, index) => 18.height,
                    itemCount: chartModelList.length)
                .expanded,
            getChart(chartModelList)
          ],
        )
      ],
    ).paddingOnly(top: 19, bottom: 11, right: 6, left: 14).objectContainerView(disablePadding: true, radius: 10);
  }

  Widget chartLegend(PieChartModel chartModel, {bool isFirst = false}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: 12,
          height: 12,
          decoration: BoxDecoration(color: chartModel.color, shape: BoxShape.circle),
        ).paddingOnly(top: 2),
        9.width,
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            RichText(
              text: TextSpan(
                text: chartModel.title,
                style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: isFirst ? MyColors.textMixColor : MyColors.backgroundBlackColor),
                children: [
                  TextSpan(
                    text: " " + chartModel.titleAppend,
                    style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: isFirst ? MyColors.textMixColor : MyColors.backgroundBlackColor),
                  ),
                ],
              ),
            ),
            chartModel.value.toString().toText14(),
          ],
        ).expanded,
      ],
    );
  }

  Widget getChart(List<PieChartModel> chartModelList) {
    List<Color> _colorList = chartModelList.map((e) => e.color).toList();
    return PieChart(
      dataMap: {for (var e in chartModelList) e.title: e.parsedValue},
      animationDuration: const Duration(milliseconds: 800),
      chartRadius: MediaQuery.of(context).size.width / 3.2,
      colorList: _colorList,
      emptyColor: MyColors.lightGreyEAColor,
      initialAngleInDegree: 270,
      legendOptions: const LegendOptions(
        showLegendsInRow: false,
        showLegends: false,
      ),
      chartValuesOptions: const ChartValuesOptions(
        showChartValueBackground: false,
        showChartValues: true,
        showChartValuesInPercentage: true,
        showChartValuesOutside: false,
        decimalPlaces: 0,
        chartValueStyle: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 10,
          letterSpacing: -0.64,
          color: MyColors.white,
        ),
      ),
    );
  }
}
