import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/worklist/worklist_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/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/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/itg_forms_models/wf_history_model.dart';
import 'package:mohem_flutter_app/models/worklist/get_favorite_replacements_model.dart';
import 'package:mohem_flutter_app/models/worklist/replacement_list_model.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/circular_avatar.dart';
import 'package:mohem_flutter_app/widgets/input_widget.dart';

class SelectedItgItemSheet extends StatelessWidget {
  String title, apiMode;
  int? notificationID;
  WFHistory wfHistory;
  GetFavoriteReplacements? favoriteReplacements;
  ReplacementList? replacementList;

  SelectedItgItemSheet(this.title, {required this.apiMode, this.notificationID, required this.wfHistory, this.favoriteReplacements, this.replacementList});

  TextEditingController username = TextEditingController();
  String comment = "";

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: MediaQuery.of(context).size.height - 80,
      child: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Padding(
                padding: EdgeInsets.all(21),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    title.toText24(isBold: true),
                    21.height,
                    if (wfHistory != null) showItem(wfHistory.name, false),
                    if (favoriteReplacements != null) showItem(favoriteReplacements!.employeeDisplayName, true),
                    if (replacementList != null) showItem(replacementList!.employeeDisplayName, replacementList!.isFavorite),
                    14.height,
                    InputWidget(
                      "Enter a note",
                      "This is simple note",
                      username,
                      isBackgroundEnable: true,
                      lines: 5,
                      onChange: (v) {
                        comment = v;
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
          Container(
            width: double.infinity,
            height: 1,
            color: MyColors.borderE3Color,
          ),
          Row(
            children: [
              Expanded(
                child: DefaultButton(
                  LocaleKeys.cancel.tr(),
                  () {
                    Navigator.pop(context);
                  },
                  textColor: Colors.black,
                  colors: [
                    Color(0xffE6E6E6),
                    Color(0xffE6E6E6),
                  ],
                ),
              ),
              16.width,
              Expanded(
                child: DefaultButton(
                  LocaleKeys.submit.tr(),
                  () {
                    if (comment.trim() != "") {
                      String? email = "", userId = "";
                      if (wfHistory != null) {
                        email = wfHistory.email;
                        userId = wfHistory.employeeID;
                      } else if (favoriteReplacements != null) {
                        email = favoriteReplacements!.emailAddress;
                        userId = favoriteReplacements!.userName;
                      } else {
                        email = replacementList!.emailAddress;
                        userId = replacementList!.userName;
                      }
                      performNetworkCall(context, email: email ?? "", userId: userId ?? "");
                    } else {
                      Utils.showToast("Please enter comments");
                    }
                  },
                  colors: const [
                    Color(0xff32D892),
                    Color(0xff1AB170),
                  ],
                ),
              ),
            ],
          ).insideContainer
        ],
      ),
    );
  }

  Future<void> performNetworkCall(BuildContext context, {String? email, String? userId}) async {
    Utils.showLoading(context);
    try {
      var requestDetails = AppState().requestAllList![AppState().itgWorkListIndex!];
      if (apiMode == "Delegate") {
        await WorkListApiClient()
            .delegateITGRequest(requestDetails.requestType!, requestDetails.iD!, requestDetails.itemID!, AppState().memberInformationList?.eMPLOYEENUMBER ?? "", wfHistory.employeeID!, comment);
      } else if (apiMode == "RequestInformation") {
        await WorkListApiClient()
            .informationITGRequest(requestDetails.requestType!, requestDetails.iD!, requestDetails.itemID!, AppState().memberInformationList?.eMPLOYEENUMBER ?? "", wfHistory.employeeID!, comment);
      } else if (apiMode == "Answer") {
        await WorkListApiClient()
            .answerITGRequest(requestDetails.requestType!, requestDetails.iD!, requestDetails.itemID!, AppState().memberInformationList?.eMPLOYEENUMBER ?? "", wfHistory.employeeID!, comment);
      }
      Utils.hideLoading(context);
      Navigator.pop(context, "delegate_reload");
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  Widget showItem(String? name, bool? isFav) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        color: Color(0xffF7F7F7),
        border: Border.all(
          color: Color(0xffefefef),
          width: 1,
        ),
      ),
      padding: EdgeInsets.only(top: 16, bottom: 16, left: 21, right: 21),
      child: Row(
        children: [
          CircularAvatar(
            height: 30,
            width: 30,
          ),
          16.width,
          Expanded(
            child: (name ?? "").toText12(),
          ),
          Icon(
            Icons.star,
            size: 12,
            color: (isFav ?? false) ? MyColors.yellowColor : MyColors.borderColor,
          ),
        ],
      ),
    );
  }
}
