import 'package:easy_localization/src/public_ext.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/get_action_history_list_model.dart';
import 'package:mohem_flutter_app/ui/work_list/sheets/delegate_sheet.dart';
import 'package:mohem_flutter_app/ui/work_list/sheets/selected_item_sheet.dart';
import 'package:mohem_flutter_app/widgets/bottom_sheet.dart';
import 'package:mohem_flutter_app/widgets/circular_avatar.dart';

class ActionsFragment extends StatelessWidget {
  int? notificationID;
  List<GetActionHistoryList> actionHistoryList;
  VoidCallback voidCallback;

  ActionsFragment(this.notificationID, this.actionHistoryList, {Key? key, required this.voidCallback}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: ListView.separated(
        itemCount: actionHistoryList.length,
        padding: EdgeInsets.all(21),
        itemBuilder: (context, index) {
          return showItem(context, actionHistoryList[index], index);
        },
        separatorBuilder: (BuildContext context, int index) {
          return 12.height;
        },
      ),
    );
  }

  Widget showItem(BuildContext context, GetActionHistoryList actionHistory, 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(actionHistory.aCTIONCODE!),
              ),
            ),
          ),
          Column(
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  actionHistory.eMPLOYEEIMAGE != null
                      ? CircularAvatar(url: actionHistory.eMPLOYEEIMAGE ?? "", isImageBase64: true, height: 34, width: 34)
                      : CircularAvatar(url: "https://cdn4.iconfinder.com/data/icons/professions-2-2/151/89-512.png", isImageBase64: false, height: 34, width: 34),
                  9.width,
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        actionHistory.nAME!.toText16(),
                        if ((actionHistory.nOTE ?? "").isNotEmpty) "Note: ${actionHistory.nOTE!}".toText12(color: MyColors.grey57Color),
                        4.height,
                        Row(
                          children: [
                            actionHistory.aCTION!.toText10(color: getStatusColor(actionHistory.aCTIONCODE!)),
                            8.width,
                            if (actionHistory.nOTIFICATIONDATE!.isNotEmpty)
                              DateUtil.formatDateToDate(DateUtil.convertSimpleStringDateToDateddMMyyyy(actionHistory.nOTIFICATIONDATE!), false).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: SelectedItemSheet(LocaleKeys.request_info.tr(), apiMode: "REQUEST_INFO", notificationID: notificationID, actionHistoryList: actionHistory),
                    );
                  }).expanded,
                  Container(width: 1, height: 30, color: MyColors.lightGreyEFColor),
                  LocaleKeys.delegate.tr().toText12(color: MyColors.gradiantEndColor).center.paddingOnly(top: 6, bottom: 6).onPress(() {
                    if (actionHistory.uSERNAME == AppState().memberInformationList?.eMPLOYEENUMBER) {
                      showMyBottomSheet(context,
                          callBackFunc: voidCallback,
                          child: DelegateSheet(
                            title: LocaleKeys.delegate.tr(),
                            apiMode: "DELEGATE",
                            notificationID: notificationID,
                            actionHistoryList: actionHistoryList,
                            callBackFunc: voidCallback,
                          ));
                      return;
                    }
                    showMyBottomSheet(
                      context,
                      callBackFunc: voidCallback,
                      child: SelectedItemSheet(
                        LocaleKeys.comments.tr(),
                        apiMode: "DELEGATE",
                        actionHistoryList: actionHistory,
                        notificationID: notificationID,
                      ),
                    );
                  }).expanded,
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  String getActionDuration(int index) {
    if (actionHistoryList[index].aCTIONCODE == "SUBMIT") {
      return "";
    } else if (actionHistoryList[index].aCTIONCODE == "PENDING") {
      if (actionHistoryList[++index].nOTIFICATIONDATE!.isEmpty) {
        return "";
      }
      DateTime dateTimeFrom = DateUtil.convertSimpleStringDateToDate(actionHistoryList[++index].nOTIFICATIONDATE!);
      Duration duration = DateTime.now().difference(dateTimeFrom);
      return "Action duration: " + DateUtil.formatDuration(duration);
    } else {
      DateTime dateTimeTo = DateUtil.convertSimpleStringDateToDate(actionHistoryList[index].nOTIFICATIONDATE!);
      DateTime dateTimeFrom = DateUtil.convertSimpleStringDateToDate(actionHistoryList[++index].nOTIFICATIONDATE!);
      Duration duration = dateTimeTo.difference(dateTimeFrom);
      return "Action duration: " + DateUtil.formatDuration(duration);
    }
  }

  Color getStatusColor(String code) {
    if (code == "SUBMIT") {
      return const Color(0xff2E303A);
    } else if (code == "REJECTED") {
      return MyColors.redColor;
    } else if (code == "REJECT") {
      return MyColors.redColor;
    } else if (code == "PENDING") {
      return MyColors.orange;
    } else if (code == "APPROVED" || code == "APPROVE" || code == "ANSWER_INFO") {
      return const Color(0xff1FA269);
    } else if (code == "REQUEST_INFO" || code == "FORWARD") {
      return const Color(0xff2E303A);
    } else if (code != "SUBMIT" && code != "REJECT" && code != "PENDING") {
      return MyColors.orange;
    } else {
      return const Color(0xff2E303A);
    }
  }
}
