import { Injectable } from "@angular/core";
import { AuthenticationService } from "src/app/hmg-common/services/authentication/authentication.service";
import { ConnectorService } from "src/app/hmg-common/services/connector/connector.service";
import { Observable } from "rxjs";
import { WorkListBodyRequest } from "../models/NotificationBodyReq";
import { EITNotificatonBodyResponse } from "../models/EITNotificationBodyRes";
import { AbsenceNotificatonBodyResponse } from "../models/AbsenceNotificationBodyRes";
import { WorkListButtonRequest } from "../models/NotificationButtonReq";
import { NotificatonButtonResponse } from "../models/NotificationButtonRes";
import { WorklistsubmitterInfoResponse } from "../models/WorklistsubmitterInfoRes";
import { WorkListSubmitterInfoRequest } from "../models/WorklistsubmitterInfoReq";
import { WorkListActionRequest } from "../models/NotificationActionReq";
import { PONotificatonBodyResponse } from "../models/PONotificationBodyRes";
import { MONotificatonBodyResponse } from "../models/MONotificationBodyRes";
import { PRNotificatonBodyResponse } from "../models/PRNotificationBodyRes";
import { StaticInjector } from "@angular/core/src/di/injector";
import { POItemHistoryRes } from "../models/POItemHistoryRes";
import { MOItemHistoryRes } from "../models/MOItemHistoryRes";
import { QuotationAnalysisResponse } from '../models/quotationAnalysisRes';
import { Request } from 'src/app/hmg-common/services/models/request';
import { LoginModel } from '../../authentication/models/LoginModel';
import { itgRequest } from '../models/itgFormDetailsRequest';
import { UserItemTypes } from "../models/userItemTypeRequest";

@Injectable()
export class WorklistMainService {
  public static getEITNotificationBody = "Services/ERP.svc/REST/GET_EIT_NOTIFICATION_BODY";
  public static getAbsenceNotificationBody = "Services/ERP.svc/REST/GET_ABSENCE_NOTIFICATION_BODY";
  public static getPoNotificationBody = "Services/ERP.svc/REST/GET_PO_NOTIFICATION_BODY";
  public static getMONotificationBody = "Services/ERP.svc/REST/GET_MO_NOTIFICATION_BODY";
  public static getPRNotificationBody = "Services/ERP.svc/REST/GET_PR_NOTIFICATION_BODY";
  public static getICNotificationBody = "Services/ERP.svc/REST/GET_ITEM_CREATION_NTF_BODY";
  public static getBasicDetailNotificationBody = "Services/ERP.svc/REST/GET_BASIC_DET_NTF_BODY";
  public static getPhoneNotificationBody = "Services/ERP.svc/REST/GET_PHONES_NOTIFICATION_BODY";
  public static getTerminationNotificationBody = "Services/ERP.svc/REST/GET_TERM_NOTIFICATION_BODY";
  public static getContactNotificationBody = "Services/ERP.svc/REST/GET_CONTACT_NOTIFICATION_BODY";
  public static getStampMSNotificationBody = "Services/ERP.svc/REST/GET_STAMP_MS_NOTIFICATION_BODY";
  public static getStampNSNotificationBody = "Services/ERP.svc/REST/GET_STAMP_NS_NOTIFICATION_BODY";
  public static getAddressNotificationBody = "Services/ERP.svc/REST/GET_ADDRESS_NOTIFICATION_BODY";
  public static getPOItemHistory = "Services/ERP.svc/REST/GET_PO_ITEM_HISTORY";
  public static getMOItemHistory = "Services/ERP.svc/REST/GET_MO_ITEM_HISTORY";
  public static getNotificationButtons = "Services/ERP.svc/REST/GET_NOTIFICATION_BUTTONS";
  public static getUserInfo = "Services/ERP.svc/REST/Get_UserInformation";
  public static getNotificationAction = "Services/ERP.svc/REST/NOTIFICATION_ACTIONS";
  public static getNotificationRespondAtt = "Services/ERP.svc/REST/NOTIFICATION_GET_RESPOND_ATTRIBUTES";
  public static getQutationAnalysisUrl = "Services/ERP.svc/REST/GET_QUOTATION_ANALYSIS";
  public static getITGFormTaskCount = 'Services/COCWS.svc/REST/ITGGetTaskCountRequestType';
  public static getITGFormTaskDetails = 'Services/COCWS.svc/REST/ITGFormsPendingTasks';
  public static getITGFormDetails = 'Services/COCWS.svc/REST/ITGGetFormDetials';
  public static getWorklistNotificationURL = 'Services/ERP.svc/REST/GET_USER_ITEM_TYPES';
  public static updateWorklistNotificationURL = 'Services/ERP.svc/REST/UPDATE_USER_ITEM_TYPES';



  constructor(
    public api: ConnectorService,
    public authService: AuthenticationService
  ) { }

  public getEITNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<EITNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getEITNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getAbsencesNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<AbsenceNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getAbsenceNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getMONotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<MONotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getMONotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getPRNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PRNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getPRNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getBasicDetailNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PRNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getBasicDetailNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getStampMSNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PRNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getStampMSNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getStampNSNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PRNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getStampNSNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getAddressNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PRNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getAddressNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getPONotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PONotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getPoNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getPhoneNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getPhoneNotificationBody,
      request,
      onError,
      errorLabel
    );
  }


  public getTerminationNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getTerminationNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getContactNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getContactNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getPOItemHistory(
    POItemHistoryReq: any,
    onError?: any,
    errorLabel?: string
  ): Observable<POItemHistoryRes> {
    const request = POItemHistoryReq;
     this.authService.authenticateRequest(request);
   // this.authService.setPublicFields(request);

    return this.api.post(
      WorklistMainService.getPOItemHistory,
      request,
      onError,
      errorLabel
    );
  }

  public getMOItemHistory(
    MOItemHistoryReq: any,
    onError?: any,
    errorLabel?: string
  ): Observable<MOItemHistoryRes> {
    const request = MOItemHistoryReq;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getMOItemHistory,
      request,
      onError,
      errorLabel
    );
  }

  public getICNotificationBody(
    WorkListBodyRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<PRNotificatonBodyResponse> {
    const request = WorkListBodyRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getICNotificationBody,
      request,
      onError,
      errorLabel
    );
  }

  public getQutationAnalysis(
    qutationAnalysis: any,
    onError?: any,
    errorLabel?: string
  ): Observable<QuotationAnalysisResponse> {
    const request = qutationAnalysis;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getQutationAnalysisUrl,
      request,
      onError,
      errorLabel
    );
  }

  public getNotificationButtons(
    WorkListButtonRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<NotificatonButtonResponse> {
    const request = WorkListButtonRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getNotificationButtons,
      request,
      onError,
      errorLabel
    );
  }

  public getSubmitterInfo(
    worklistsubmitterInfoObj: any,
    onError?: any,
    errorLabel?: string
  ): Observable<WorklistsubmitterInfoResponse> {
    const request = worklistsubmitterInfoObj;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getUserInfo,
      request,
      onError,
      errorLabel
    );
  }

  public actionButton(
    WorkListActionRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    const request = WorkListActionRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getNotificationAction,
      request,
      onError,
      errorLabel
    );
  }

  public notificationResponseAttr(
    notifiResAttRequest: any,
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    const request = notifiResAttRequest;
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getNotificationRespondAtt,
      request,
      onError,
      errorLabel
    );
  }

  public getITGCount( onError?: any, errorLabel?: string, isPostNoLoad = false): Observable<any> {
    const request = new Request();
    this.authService.authenticateRequest(request);
    if (isPostNoLoad) {
      return this.api.postNoLoad(
        WorklistMainService.getITGFormTaskCount,
        request,
        onError,
        errorLabel
      );
    } else {
      return this.api.post(
        WorklistMainService.getITGFormTaskCount,
        request,
        onError,
        errorLabel
      );
    }
  }

  public getITGFormDetails(request: itgRequest, onError?: any, errorLabel?: string): Observable<any> {
    this.authService.authenticateRequest(request);
    request.EmployeeNumber = request.P_USER_NAME;
    console.log(request);
    return this.api.post(
      WorklistMainService.getITGFormDetails,
      request,
      onError,
      errorLabel
    );
  }



  public getITGDetails( onError?: any, errorLabel?: string, isPostNoLoad = false): Observable<any> {
    const request = new Request();
    console.log(request);
    this.authService.authenticateRequest(request);
    if (isPostNoLoad) {
      return this.api.postNoLoad(
        WorklistMainService.getITGFormTaskDetails,
        request,
        onError,
        errorLabel
      );
    } else {
      return this.api.post(
        WorklistMainService.getITGFormTaskDetails,
        request,
        onError,
        errorLabel
      );
    }
  }


  public getITGActionRequest(itgrequest:itgRequest,actionURL:string, onError?: any, errorLabel?: string): Observable<any> {
    this.authService.authenticateRequest(itgrequest);
    itgrequest.EmployeeNumber = itgrequest.P_USER_NAME;
    console.log(itgrequest);
    console.log(actionURL);
    return this.api.postItg(
      actionURL,
      itgrequest,
      onError,
      errorLabel
    );
  }


  public getWorklistNotifications(
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    const request = new Request();
    this.authService.authenticateRequest(request);
    return this.api.post(
      WorklistMainService.getWorklistNotificationURL,
      request,
      onError,
      errorLabel
    );
  }

  public updateWorklistNotifications(
    requestData: any,
    onError?: any,
    errorLabel?: string
  ): Observable<any> {
    this.authService.authenticateRequest(requestData);
    return this.api.post(
      WorklistMainService.updateWorklistNotificationURL,
      requestData,
      onError,
      errorLabel
    );
  }

}
