import 'package:mohem_flutter_app/api/api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:mohem_flutter_app/models/dashboard/get_attendance_tracking_list_model.dart';
import 'package:mohem_flutter_app/models/dashboard/menu_entries.dart';
import 'package:mohem_flutter_app/models/generic_response_model.dart';
import 'package:mohem_flutter_app/models/get_day_hours_type_details_list_model.dart';
import 'package:mohem_flutter_app/models/get_time_card_summary_list_model.dart';
import 'package:mohem_flutter_app/models/my_team/get_employee_subordinates_list.dart';
import 'package:mohem_flutter_app/models/my_team/get_subordinates_leaves_total_vacations_list_model.dart';
import 'package:mohem_flutter_app/models/worklist/get_favorite_replacements_model.dart';

class MyTeamApiClient {
  static final MyTeamApiClient _instance = MyTeamApiClient._internal();

  MyTeamApiClient._internal();

  factory MyTeamApiClient() => _instance;

  Future<List<GetEmployeeSubordinatesList>> getEmployeeSubordinates(String searchEmpEmail, String searchEmpName, String searchEmpNo) async {
    String url = "${ApiConsts.erpRest}GET_EMPLOYEE_SUBORDINATES";
    Map<String, dynamic> postParams = {
      "P_PAGE_LIMIT": 50,
      "P_PAGE_NUM": 1,
      "P_SEARCH_EMAIL_ADDRESS": searchEmpEmail,
      "P_SEARCH_EMPLOYEE_DISPLAY_NAME": searchEmpName,
      "P_SEARCH_EMPLOYEE_NUMBER": searchEmpNo,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getEmployeeSubordinatesList ?? [];
    }, url, postParams);
  }

  Future<GetTimeCardSummaryList?> getTimeCardSummary(String month, int year, String? empID) async {
    String url = "${ApiConsts.erpRest}GET_TIME_CARD_SUMMARY";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "M",
      "P_SELECTED_RESP_ID": -999,
      "SearchMonth": month,
      "SearchYear": year,
    };

    postParams.addAll(AppState().postParamsJson);
    postParams['P_SELECTED_EMPLOYEE_NUMBER'] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return (responseData.getTimeCardSummaryList?.length ?? 0) > 0 ? responseData.getTimeCardSummaryList!.first : null;
    }, url, postParams);
  }

  Future<List<GetDayHoursTypeDetailsList>> getDayHoursTypeDetails(String month, int year, String? empID) async {
    String url = "${ApiConsts.erpRest}GET_DAY_HOURS_TYPE_DETAILS";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "M",
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
      "P_SELECTED_RESP_ID": -999,
      "SearchMonth": month,
      "SearchYear": year,
    };
    postParams.addAll(AppState().postParamsJson);
    postParams['P_SELECTED_EMPLOYEE_NUMBER'] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      print(responseData.getDayHoursTypeDetailsList!.length);
      return responseData.getDayHoursTypeDetailsList ?? [];
    }, url, postParams);
  }

  Future<GetAttendanceTracking?> getAttendanceTracking(String? empID) async {
    String url = "${ApiConsts.erpRest}GET_Attendance_Tracking";
    Map<String, dynamic> postParams = {};
    postParams.addAll(AppState().postParamsJson);
    postParams['P_SELECTED_EMPLOYEE_NUMBER'] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      print(responseData.getAttendanceTrackingList);
      return responseData.getAttendanceTrackingList;
    }, url, postParams);
  }

  Future<List<GetEmployeeSubordinatesList>> employeeSubordinates(String searchEmpEmail, String searchEmpName, String searchEmpNo, String? empID) async {
    String url = "${ApiConsts.erpRest}GET_EMPLOYEE_SUBORDINATES";
    Map<String, dynamic> postParams = {
      "P_PAGE_LIMIT": 50,
      "P_PAGE_NUM": 1,
      "P_SEARCH_EMAIL_ADDRESS": searchEmpEmail,
      "P_SEARCH_EMPLOYEE_DISPLAY_NAME": searchEmpName,
      "P_SEARCH_EMPLOYEE_NUMBER": searchEmpNo,
    };
    postParams.addAll(AppState().postParamsJson);
    postParams['P_SELECTED_EMPLOYEE_NUMBER'] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getEmployeeSubordinatesList ?? [];
    }, url, postParams);
  }

  Future<List<GetMenuEntriesList>> employeeSubordinatesRequest(String? empID) async {
    String url = "${ApiConsts.erpRest}GET_MENU_ENTRIES";
    Map<String, dynamic> postParams = {"P_MENU_TYPE": "M", "P_SELECTED_RESP_ID": -999};
    postParams.addAll(AppState().postParamsJson);
    postParams['P_SELECTED_EMPLOYEE_NUMBER'] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getMenuEntriesList ?? [];
    }, url, postParams);
  }

  Future<List<GetFavoriteReplacements>?> getFavoriteReplacement() async {
    String url = "${ApiConsts.erpRest}Mohemm_GetFavoriteReplacements";
    Map<String, dynamic> postParams = {};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.mohemmGetFavoriteReplacementsList;
    }, url, postParams);
  }

  Future<GenericResponseModel> changeFavoriteReplacements({required String email, required String employeName, required String image, required String userName, bool isFav = false}) async {
    String url = "${ApiConsts.erpRest}Mohemm_ChangeFavoriteReplacements";
    Map<String, dynamic> postParamsObj = {
      "EMAIL_ADDRESS": email,
      "EMPLOYEE_DISPLAY_NAME": employeName,
      "EMPLOYEE_IMAGE": image,
      "IsFavorite": isFav,
      "USER_NAME": userName,
    };
    Map<String, dynamic> postParams = {
      "Mohemm_ChangeReplacementsInputList": [postParamsObj],
      //postParams["Mohemm_ChangeReplacementsInputList"] = list;
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<List<GetSubordinatesLeavesTotalVacationsList>> getSubordinatesLeavesList(String dateFrom, String dateTo) async {
    String url = "${ApiConsts.erpRest}GET_SUBORDINATES_LEAVES_TOTAL_VACATIONS";
    Map<String, dynamic> postParams = {
      "P_DATE_FROM": dateFrom,
      "P_DATE_TO": dateTo,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getSubordinatesLeavesTotalVacationsList ?? [];
    }, url, postParams);
  }
}
