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/generic_response_model.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_department_sections.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_project_departments.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_projects.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_section_topics.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_ticket_details.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_ticket_transactions.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_ticket_types.dart';
import 'package:mohem_flutter_app/models/mowadhafhi/get_tickets_list.dart';

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

  MowadhafhiApiClient._internal();

  factory MowadhafhiApiClient() => _instance;

  Future<List<GetTicketsByEmployeeList>> getTicketsByEmployee() async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetTicketsByEmployee";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgPageSize": 10, "ItgPageNo": 1};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getTicketsByEmployeeList ?? [];
    }, url, postParams);
  }

  Future<List<GetTicketDetailsByEmployee>> getTicketDetailsByEmployee(String? itgTicketID) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetTicketDetails";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgTicketId": itgTicketID};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getTicketDetailsByEmployee ?? [];
    }, url, postParams);
  }

  Future<List<GetTicketTransactions>> getTicketTransactions(String? itgTicketID) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetTicketTransaction";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgTicketId": itgTicketID};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getTicketTransactions ?? [];
    }, url, postParams);
  }

  Future<List<GetTicketTypes>> getTicketTypes() async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetTicketTypes";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getTicketTypes ?? [];
    }, url, postParams);
  }

  Future<List<GetMowadhafhiProjects>> getProjects() async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetProjects";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgProjectCode": AppState().memberInformationList?.pAYROLLCODE};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getMowadhafhiProjects ?? [];
    }, url, postParams);
  }

  Future<List<GetProjectDepartments>> getProjectDepartments(int projectID) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetProjectDepartments";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgProjectId": projectID};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getProjectDepartments ?? [];
    }, url, postParams);
  }

  Future<List<GetDepartmentSections>> getDepartmentSections(int? projectDepartmentID) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetDepartmentSections";
    Map<String, dynamic> postParams = {
      "EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "ItgDepartmentSectionId": projectDepartmentID,
      "ItgProjectDepartmentId": projectDepartmentID
    };

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getDepartmentSections ?? [];
    }, url, postParams);
  }

  Future<List<GetSectionTopics>> getSectionTopics(int? departmentSectionID) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_GetSectionTopics";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgDepartmentSectionId": departmentSectionID};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getSectionTopics ?? [];
    }, url, postParams);
  }

  Future<int?> submitRequest(int? departmentID, String description, int? projectID, String? sectionID, String? sectionTopicID, int? ticketTypeID, List<Map<String, dynamic>> attachmentList) async {
    String url = "${ApiConsts.cocRest}Mohemm_ITG_CreateTicketMobile";
    Map<String, dynamic> postParams = {
      "EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "ItgImageCollList": attachmentList,
      "channelId": 3,
      "departmentId": departmentID,
      "description": description,
      "employeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER,
      "projectId": projectID,
      "sectionId": sectionID,
      "sectionTopicId": sectionTopicID,
      "ticketStatus": "new",
      "ticketTypeId": ticketTypeID
    };

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.messageStatus;
    }, url, postParams);
  }
}
