import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.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/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/itg_forms_models/wf_history_model.dart';
import 'package:mohem_flutter_app/ui/work_list/sheets/delegate_sheet.dart';
import 'package:mohem_flutter_app/ui/work_list/sheets/selected_itg_item_sheet.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheet.dart';
import 'package:mohem_flutter_app/widgets/circular_avatar.dart';

class ApprovalLevelfragment extends StatelessWidget {
  List<WFHistory> wFHistory;
  VoidCallback voidCallback;

  ApprovalLevelfragment({Key? key, this.wFHistory = const <WFHistory>[], required this.voidCallback}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (wFHistory.isNotEmpty) {
      //TODO: This COde is Written For Future. If Needed Enable to Filter The Empty Action Requests.
      // wFHistory = wFHistory.where((item) => item.action != null && item.action!.isNotEmpty).toList();
      wFHistory = wFHistory.reversed.toList();
    }
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: ListView.separated(
        itemCount: wFHistory.length,
        padding: const EdgeInsets.all(21),
        itemBuilder: (context, index) {
          return showItem(context, wFHistory[index], index);
        },
        separatorBuilder: (BuildContext context, int index) {
          return 12.height;
        },
      ),
    );
  }

  Widget showItem(BuildContext context, WFHistory history, int index) {
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: const Color(0xff000000).withOpacity(.05),
            blurRadius: 26,
            offset: const Offset(0, -3),
          ),
        ],
      ),
      clipBehavior: Clip.antiAlias,
      child: Stack(
        clipBehavior: Clip.antiAlias,
        children: [
          Positioned(
            left: -20,
            top: -10,
            child: Transform.rotate(
              angle: 15,
              child: Container(
                width: 50,
                height: 30,
                color: getStatusColor(history.action!),
              ),
            ),
          ),
          Column(
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CircularAvatar(url: null, isImageBase64: false, height: 34, width: 34),
                  9.width,
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        history.name!.toText16(),
                        if ((history.notes ?? "").isNotEmpty) history.notes!.toText12(color: MyColors.grey57Color),
                        4.height,
                        Row(
                          children: [
                            history.action!.toText10(
                              color: getStatusColor(history.action!),
                            ),
                            8.width,
                            if (history.date!.isNotEmpty) history.date!.toText12(color: MyColors.lightTextColor),
                          ],
                        ),
                        10.height,
                        getActionDuration(index).toText11(maxLine: 1, color: const Color(0xff1FA269))
                      ],
                    ),
                  )
                ],
              ).paddingOnly(top: 19, left: 16, right: 16, bottom: 12),
              Container(width: double.infinity, height: 1, color: MyColors.lightGreyEFColor),
              Row(
                children: [
                  LocaleKeys.request_info.tr().toText12(color: MyColors.grey67Color).center.paddingOnly(top: 6, bottom: 6).onPress(() {
                    showMyBottomSheet(
                      context,
                      callBackFunc: voidCallback,
                      child: SelectedItgItemSheet(
                        LocaleKeys.comments.tr(),
                        wfHistory: history,
                        apiMode: 'RequestInformation',
                      ),
                    );
                  }).expanded,
                  Container(width: 1, height: 30, color: MyColors.lightGreyEFColor),
                  LocaleKeys.delegate.tr().toText12(color: MyColors.gradiantEndColor).center.paddingOnly(top: 6, bottom: 6).onPress(() {
                    if (history.employeeID == AppState().memberInformationList?.eMPLOYEENUMBER) {
                      showMyBottomSheet(context,
                          callBackFunc: voidCallback,
                          child: DelegateSheet(
                            title: LocaleKeys.delegate.tr(),
                            apiMode: "Delegate",
                            notificationID: null,
                            actionHistoryList: null,
                            wFHistory: wFHistory,
                            callBackFunc: voidCallback,
                          ));
                      return;
                    }
                    showMyBottomSheet(
                      context,
                      callBackFunc: voidCallback,
                      child: SelectedItgItemSheet(
                        LocaleKeys.comments.tr(),
                        wfHistory: history,
                        apiMode: 'Delegate',
                      ),
                    );
                  }).expanded,
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  String getActionDuration(int index) {
    if (wFHistory[index].action!.toLowerCase() == "submit" || wFHistory[index].action!.toLowerCase() == "submitted") {
      return "";
      // DateTime dateTimeFrom = DateUtil.convertSimpleStringDateToDate(wFHistory[index].date!);
      // Duration duration = DateTime.now().difference(dateTimeFrom);
      // return "Action duration: " + DateUtil.formatDuration(duration);
    } else if (wFHistory[index].employeeID == AppState().memberInformationList?.eMPLOYEENUMBER) {
      if (wFHistory[index + 1].date!.isEmpty || wFHistory[index + 1].date! == "") {
        return "";
      } else {
        DateTime dateTimeFrom = DateUtil.convertSimpleStringDateToDate(wFHistory[index + 1].date!, isITG: true);
        Duration duration = DateTime.now().difference(dateTimeFrom);
        return "Action duration: " + DateUtil.formatDuration(duration);
      }
    } else {
      if (wFHistory[index].date!.isEmpty || wFHistory[index].action!.toLowerCase() == "NO ACTION" || wFHistory[index + 1].date! == "") {
        return "";
      } else {
        DateTime dateTimeTo = DateUtil.convertSimpleStringDateToDate(wFHistory[index].date!, isITG: true);
        DateTime dateTimeFrom = DateUtil.convertSimpleStringDateToDate(wFHistory[index + 1].date!, isITG: true);
        Duration duration = dateTimeTo.difference(dateTimeFrom);
        return "Action duration: " + DateUtil.formatDuration(duration);
      }
    }
  }

  Color getStatusColor(String code) {
    if (code.toLowerCase() == "submit" || code.toLowerCase() == "submitted") {
      return MyColors.grey3AColor;
    } else if (code.toLowerCase() == "pending") {
      return MyColors.yellowColor;
    } else if (code.toLowerCase() == "not doable" || code.toLowerCase() == "rejected") {
      return MyColors.redColor;
    } else if (code.toLowerCase() == "approved" || code.toLowerCase() == "auto-approve" || code.toLowerCase() == "auto-approved" || code.toLowerCase() == "doable" || code.toLowerCase() == "answer") {
      return MyColors.greenColor;
    } else if (code.toLowerCase() == "requested information" || code.toLowerCase() == "request information" || code.toLowerCase() == "assign" || code.toLowerCase() == "reassign") {
      return MyColors.orange;
    } else {
      return MyColors.whiteColor;
    }
  }
}
