import { createRequest, applicantCreateUpdateRequest, paymentGatewayRESTCall, createRequestFileUpload, applicantCreateRequest, createRequestPayment, createRequestFreezeUnFreeze, createRequestCourtFreezeUnFreeze } from './helper';
import Config from '../../config/appConstants';
import Utils from '../../utils';
import { store } from '../../redux'; //GET

export async function getAPI(uri, params) {
  return await createRequest('GET', uri, 1, '', params).then(response => {
    return response.data;
  }).catch(function (error) {
    return false;
  });
} //PUT_POST

export async function sendAPI(type, uri, payload, params) {
  if (type === "PUT" || type === "POST") {
    // if (type === "POST") {
    //     if(payload.hasOwnProperty("RelatedParties")){
    //         payload.RelatedParties.map((cur, i)=> {
    //             if(store.getState().proxyUserDetails.hasOwnProperty("ProxyLoginDetails")){
    //                 if(cur.hasOwnProperty("OwnerDetails")){
    //                     payload.RelatedParties[i].OwnerDetails["ProxyGeneratedID"] = store.getState().proxyUserDetails.hasOwnProperty("ProxyLoginDetails") ? store.getState().proxyUserDetails.ProxyLoginDetails.ProxyGeneratedID: "";
    //                 }else if(cur.hasOwnProperty("BeneficiaryDetails")){
    //                     payload.RelatedParties[i].BeneficiaryDetails["ProxyGeneratedID"] = store.getState().proxyUserDetails.hasOwnProperty("ProxyLoginDetails") ? store.getState().proxyUserDetails.ProxyLoginDetails.ProxyGeneratedID: "";
    //                 }else if(cur.hasOwnProperty("ProcessingPartyDetails")){
    //                     payload.RelatedParties[i].ProcessingPartyDetails["ProxyGeneratedID"] = store.getState().proxyUserDetails.hasOwnProperty("ProxyLoginDetails") ? store.getState().proxyUserDetails.ProxyLoginDetails.ProxyGeneratedID: "";
    //                 }
    //             }
    //         });
    //     }
    // }
    if (payload.hasOwnProperty("UpdateApplicationDetails") && payload.UpdateApplicationDetails.hasOwnProperty("ApplicationDetails") && payload.UpdateApplicationDetails.ApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.UpdateApplicationDetails.ApplicationDetails.ApplicationData["ActionUserID"] = Utils.ActionUserDetails().ActionUserID;
      payload.UpdateApplicationDetails.ApplicationDetails.ApplicationData.UpdatedBy = Utils.CreatedUserDetails().CreatedUserID;
    } else if (payload.hasOwnProperty("UpdateApplicationDetails") && payload.UpdateApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.UpdateApplicationDetails.ApplicationData.UpdatedBy = Utils.CreatedUserDetails().CreatedUserID;
      payload.UpdateApplicationDetails.ApplicationData["ActionUserID"] = Utils.ActionUserDetails().ActionUserID;
    } else if (payload.hasOwnProperty("ApplicationDetails") && payload.ApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.ApplicationDetails.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;
      payload.ApplicationDetails.ApplicationData.ActionUserID = Utils.ActionUserDetails().ActionUserID;
    } else if (payload.hasOwnProperty("ApplicationData")) {
      payload.ApplicationData.ActionUserID = Utils.ActionUserDetails().ActionUserID;
      payload.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;
    }
  } // payload.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;


  return await applicantCreateUpdateRequest(type, uri, 1, payload, params).then(function (response) {
    return response.data;
  }).catch(function (error) {
    return false;
  });
} //PKI Login - Auth API Call

export async function createAuthAPIRequest(type, uri, payload, params) {
  return await createRequest(type, uri, 0, payload, params).then(function (response) {
    return response.data;
  }).catch(function (error) {
    if (error.response && error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
} //PUT_POST using createRequest

export async function createAPIRequest(type, uri, payload, params) {
  if (type === "PUT" || type === "POST") {
    if (payload.hasOwnProperty("UpdateApplicationDetails") && payload.UpdateApplicationDetails.hasOwnProperty("ApplicationDetails") && payload.UpdateApplicationDetails.ApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.UpdateApplicationDetails.ApplicationDetails.ApplicationData["ActionUserID"] = Utils.ActionUserDetails().ActionUserID;
      payload.UpdateApplicationDetails.ApplicationDetails.ApplicationData.UpdatedBy = Utils.CreatedUserDetails().CreatedUserID;
    } else if (payload.hasOwnProperty("UpdateApplicationDetails") && payload.UpdateApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.UpdateApplicationDetails.ApplicationData.UpdatedBy = Utils.CreatedUserDetails().CreatedUserID;
      payload.UpdateApplicationDetails.ApplicationData["ActionUserID"] = Utils.ActionUserDetails().ActionUserID;
    } else if (payload.hasOwnProperty("ApplicationDetails") && payload.ApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.ApplicationDetails.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;
      payload.ApplicationDetails.ApplicationData.ActionUserID = Utils.ActionUserDetails().ActionUserID;
    } else if (payload.hasOwnProperty("ApplicationData")) {
      payload.ApplicationData.ActionUserID = Utils.ActionUserDetails().ActionUserID;
      payload.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;
    }
  } else if (type === "GET") {
    if (uri === Config.GET_QUERY_RE_OBJECT) {
      if (store.getState().proxyUserDetails.hasOwnProperty("ProxyLoginDetails") && store.getState().proxyUserDetails.ProxyLoginDetails.IsSpecific === "Yes") {
        params.REObjectID = store.getState().proxyUserDetails.ProxyLoginDetails.REObjectID;
      }
    }
  }

  return await createRequest(type, uri, 1, payload, params).then(function (response) {
    return response.data;
  }).catch(function (error) {
    if (error.response && error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
} //Create Request File Upload

export async function createAPIRequestFileUpload(type, uri, payload, encMode) {
  return await createRequestFileUpload(type, uri, 1, payload, encMode).then(function (response) {
    return response.data;
  }).catch(function (error) {
    console.log(error.response.data);

    if (error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
} //Create and Update Application Endpoint

export async function applicationCreateUpdateAPIRequest(type, uri, payload, params) {
  if (type === "PUT" || type === "POST") {
    if (payload.hasOwnProperty("UpdateApplicationDetails") && payload.UpdateApplicationDetails.hasOwnProperty("ApplicationDetails") && payload.UpdateApplicationDetails.ApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.UpdateApplicationDetails.ApplicationDetails.ApplicationData["ActionUserID"] = Utils.ActionUserDetails().ActionUserID;
      payload.UpdateApplicationDetails.ApplicationDetails.ApplicationData.UpdatedBy = Utils.CreatedUserDetails().CreatedUserID;
    } else if (payload.hasOwnProperty("UpdateApplicationDetails") && payload.UpdateApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.UpdateApplicationDetails.ApplicationData.UpdatedBy = Utils.CreatedUserDetails().CreatedUserID;
      payload.UpdateApplicationDetails.ApplicationData["ActionUserID"] = Utils.ActionUserDetails().ActionUserID;
    } else if (payload.hasOwnProperty("ApplicationDetails") && payload.ApplicationDetails.hasOwnProperty("ApplicationData")) {
      payload.ApplicationDetails.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;
      payload.ApplicationDetails.ApplicationData.ActionUserID = Utils.ActionUserDetails().ActionUserID;
    } else if (payload.hasOwnProperty("ApplicationData")) {
      payload.ApplicationData.ActionUserID = Utils.ActionUserDetails().ActionUserID;
      payload.ApplicationData.CreatedBy = Utils.CreatedUserDetails().CreatedUserID;
    }
  }

  return await applicantCreateRequest(type, uri, 1, payload, params).then(function (response) {
    return response.data;
  }).catch(function (error) {
    console.log(error.response.data);

    if (error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
}
export async function paymentGatewayGetAPI(uri, params, sessionID) {
  return await paymentGatewayRESTCall('GET', uri, 1, params, sessionID).then(function (response) {
    return response.data;
  }).catch(function (error) {
    if (error.response.data.code === "404") return false;
  });
}
export async function createPaymentAPIRequest(type, uri, payload, params) {
  return await createRequestPayment(type, uri, 1, payload, params).then(function (response) {
    return response.data;
  }).catch(function (error) {
    console.log(error.response.data);

    if (error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
}
export async function KrookiValidation(params) {
  return await createRequest('GET', Config.KROOKI_VALIDATION, 1, '', params).then(response => {
    return response.data;
  }).catch(function (error) {
    return false;
  });
}
export async function GetLandInvolvedParty(params) {
  return await createRequest('GET', Config.GETLAND_INVOLVEDPARTYID + "/residential", 1, '', params).then(response => {
    return response.data;
  }).catch(function (error) {
    return false;
  });
}
export async function getInvolvedParty(params) {
  return await createRequest('GET', Config.GET_INVOVLED_PARTY, 1, '', params).then(response => {
    return response.data;
  }).catch(function (error) {
    return false;
  });
}
export async function createAPIRequestFreezeUnFreeze(type, uri, payload, params, InvolvedPartyID) {
  return await createRequestFreezeUnFreeze(type, uri, 1, payload, params, InvolvedPartyID).then(function (response) {
    return response.data;
  }).catch(function (error) {
    if (error.response && error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
}
export async function createAPIRequestCourtFreezeUnFreeze(type, uri, payload, params, InvolvedPartyID) {
  return await createRequestCourtFreezeUnFreeze(type, uri, 1, payload, params, InvolvedPartyID).then(function (response) {
    return response.data;
  }).catch(function (error) {
    if (error.response && error.response.data.code === "404" && error.response.data.details === "Proxy Number Expired") {
      return error.response.data.details;
    } else {
      return false;
    }
  });
}