// a library to wrap and simplify api calls
import apisauce from 'apisauce'
import Constant from '../library/Constant'

// our "constructor"
const create = (type = "") => {
  let api;
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const baseURL = 'https://tia.eksad.com/tia-reporting-dev/public/'
  switch (type) {
    case '':
      api = apisauce.create({
        // base URL is read from the "constructor"
        baseURL,
        // here are some default headers
        headers: {
          'Cache-Control': 'no-cache',
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },
        // 60 second timeout...
        timeout: 60000
      })
      break;
    case 'UPLOAD':
      api = apisauce.create({
        // base URL is read from the "constructor"
        baseURL,
        // here are some default headers
        headers: {
          'Cache-Control': 'no-cache',
          Accept: 'application/json',
          'Content-Type': 'application/json',
        },
        // 40 second timeout...
        timeout: 100000
      })
      break;
    default:
      break;
  }

  api.addAsyncRequestTransform(request => async () => {
    var token
    try {
      const res = await localStorage.getItem(Constant.TOKEN)
      if (token != null) {
        token = res
        // alert(url)
        // api.setBaseURL(`${url}/api/`)
      } else {
        token = res
        // url = Constant.BASE_URL.MASTER + '/api/'
        // alert(url)
        // api.setBaseURL(`${url}/api/`)
      }
    } catch (error) {
      // console.tron.log(error)
    }
    // console.log(token)
    request.headers['token'] = token
    // console.tron.log(url)
  })

  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //
  const getRoot = () => api.get('')

  //Auth
  const login = (body) => api.post('auth/login', body)
  const resetPassword = (body) => api.post('auth/reset_password', body)
  const verification = (body) => api.post('email/reset_password', body)
  const isResetPassword = (userId) => api.post(`auth/is_reset_password/${userId}`)

  //Role
  const getRole = () => api.get('role/get_all_role')
  const getRoleActive = () => api.get('role/get_all_role_active')
  const getDetailRole = (roleId) => api.get(`role/get_role_by_id/${roleId}`)
  const searchRole = (body) => api.post('/role/search_role', body)
  const addRole = (body) => api.post('role/create_role', body)
  const editRole = (body) => api.post('role/update_role', body)
  const deleteRole = (roleId) => api.post(`role/delete_role/${roleId}`)

  //Menu
  const getMenu = () => api.get('menu/get_menu_hierarki')
  const getMenuByRole = () => api.get('menu/get_menu_hierarki_by_role')
  const getMenuByUser = () => api.get('menu/get_menu')
  const getPermission = (body) => api.post('permission/get_permission', body)

  //UNIT BISNIS
  const getUnitBisnis = () => api.get('business_unit/get_all_business_unit')
  const createUnitBisnis = (body) => api.post('/business_unit/create_business_unit', body)
  const updateUnitBisnis = (body) => api.post('/business_unit/update_business_unit', body)
  const searchUnitBisnis = (body) => api.post('/business_unit/search_business_unit', body)
  const checkUploadUnitBisnis = (body) => api.post('/business_unit/check_import', body)
  const uploadUnitBisnis = (body) => api.post('/business_unit/import_business_unit', body)
  const getUnitBisnisActive = () => api.get('business_unit/get_all_business_unit_active')
  const getDetailUnitBisnis = (id) => api.get(`business_unit/get_business_unit_by_id/${id}`)
  const deleteUnitBisnis = (id) => api.post(`business_unit/delete_business_unit/${id}`)

  // Perusahaan
  const getPerusahaan = () => api.get('company/get_all_company')
  const getPerusahaanActive = () => api.get('company/get_all_company_active')
  const createPerusahaan = (body) => api.post('/company/create_company', body)
  const updatePerusahaan = (body) => api.post('/company/update_company', body)
  const getPerusahaanHierarki = () => api.get('company/get_company_hierarki')
  const saveVisualisasiPerusahaan = (body) => api.post('company/save_visualization', body)
  const checkUploadPerusahaan = (body) => api.post('company/check_import', body)
  const getDetailPerusahaan = (id) => api.get(`company/get_company_by_id/${id}`)
  const uploadPerusahaan = (body) => api.post('company/import_company', body)
  const searchPerusahaan = (body) => api.post('company/search_company', body)
  const deletePerusahaan = (id) => api.post(`company/delete_company/${id}`)

  // APPROVAL MATRIX
  const getAM = () => api.get('approval_matrix/get_all_approval_matrix')
  const getApprovedByAM = () => api.get('approval_matrix/get_all_approver')
  const getTypeAM = () => api.get('approval_type/get_all_approval_type')
  const getOperatorAM = () => api.get('operator_type/get_all_operator_type')
  const getDetailAM = (id) => api.get(`approval_matrix/get_approval_matrix_by_id/${id}`)
  const searchAM = (body) => api.post('/approval_matrix/search_approval_matrix', body)
  const createAM = (body) => api.post('/approval_matrix/create_approval_matrix', body)
  const updateAM = (body) => api.post('/approval_matrix/update_approval_matrix', body)
  const updateVAM = (body) => api.post('/approval_matrix/visual_approval_matrix', body)
  const checkUploadAM = (body) => api.post('/approval_matrix/check_import_approval_matrix', body)
  const uploadAM = (body) => api.post('approval_matrix/import_approval_matrix', body)
  const deleteAM = (id) => api.post(`approval_matrix/delete_approval_matrix/${id}`)

  //User
  const getUser = () => api.get('user/get_all_user')
  const getDetailUser = (userId) => api.get(`user/get_user_by_id/${userId}`)
  const searchUser = (body) => api.post('user/search_user', body)
  const createUser = (body) => api.post('user/create_user', body)
  const updateUser = (body) => api.post('user/update_user', body)
  const deleteUser = (userId) => api.post(`user/delete_user/${userId}`)
  const changePassword = (body) => api.post('/user/change_password', body)
  const checkUploadUser = (body) => api.post('/user/check_import', body)
  const uploadUser = (body) => api.post('/user/import_user', body)

  //Report Items
  const getReportItems = () => api.get('item_report/get_all_item_report')
  const getInputType = () => api.get('type_item_report/get_all_type_item_report')
  const getReportType = () => api.get('report/get_all_report')
  const getDetailReportItems = (userId) => api.get(`item_report/get_item_report_by_id/${userId}`)
  const searchReportItems = (body) => api.post('/item_report/search_item_report', body)
  const createReportItems = (body) => api.post('/item_report/create_item_report', body)
  const updateReportItems = (body) => api.post('/item_report/update_item_report', body)
  const checkUploadReportItems = (body) => api.post('/item_report/check_import', body)
  const uploadReportItems = (body) => api.post('/item_report/import_item_report', body)
  const getItemReportHierarki = (body) => api.post('item_report/get_item_report_hierarki', body)
  const saveVisualisasiReport = (body) => api.post('item_report/save_visualization', body)
  const getReportParent = (body) => api.post('item_report/get_parent_item_report', body)
  const deleteReportItems = (id) => api.post(`item_report/delete_item_report/${id}`)
  const getAllSettingByType = (body) => api.post('setting/get_all_setting_by_type', body)
  const createAllItemReport = (body) => api.post('item_report/create_all_item_report', body)
  const deleteAllItemReport = (body) => api.post('/item_report/delete_all_item_report', body)

  //PARAMETER
  const getAllParameter = () => api.get('/setting/get_all_setting')
  const getDetailParameter = (id) => api.get(`setting/get_setting_by_id/${id}`)
  const getAllGroup = () => api.get('/setting_group/get_all_setting_group')
  const getParameterByGroup = (groupID) => api.get(`/setting_type/get_all_setting_type_by_group/${groupID}`)
  const createParameter = (body) => api.post('setting/create_setting', body)
  const updateParameter = (body) => api.post('setting/update_setting', body)
  const checkUploadParameter = (body) => api.post('setting/check_import', body)
  const uploadParameter = (body) => api.post('/setting/import_setting', body)
  const searchParameter = (body) => api.post('setting/search_setting', body)
  const deleteParameter = (id) => api.post(`setting/delete_setting/${id}`)

  //Transaction
  const getReportTypeBody = (body) => api.post('transaction/get_all_report', body)
  const getMasterBudgetAtt = (body) => api.post('transaction/get_report_attachment', body)
  const uploadAttachment = (body) => api.post('transaction/upload_attachment', body)
  const getRevision = (body) => api.post('transaction/get_revision', body)
  const getPeriodeTransaction = () => api.get('transaction/get_periode')
  const deleteAttachment = (id) => api.post(`transaction/delete_attachment/${id}`)
  const getDetailReportMB = (body) => api.post('/transaction/master_budget/get_report_hierarki', body)
  const getLastestUpdateMB = (body) => api.post('/transaction/master_budget/get_latest_update', body)
  const createSubmitReport = (body) => api.post('transaction/master_budget/create_submission_report', body)
  const getSubmission = (body) => api.post('transaction/get_submission_id', body)
  const checkUploadMB = (body) => api.post('transaction/master_budget/check_import', body)
  const uploadMasterBudget = (body) => api.post('transaction/master_budget/import_master_budget', body)
  const validateSubmitReport = (body) => api.post('transaction/master_budget/validate_save', body)
  const getMonthTransaction = () => api.get('transaction/get_default_month')
  const countingFormula = (body) => api.post('transaction/counting_formula', body)
  const submitMasterBudget = (body) => api.post('transaction/master_budget/submit_master_budget', body)
  const checkIsSubmit = (body) => api.post('transaction/master_budget/is_can_submit', body)
  const checkApprover = () => api.get('transaction/master_budget/is_approver')
  const approvalSubmission = (body) => api.post('transaction/master_budget/approval_submission', body)

  const getIdDeleteFromExcel = (body) => api.post('transaction/master_budget/delete_from_excel', body)
  const getOpetratingIndID = (body) => api.post('transaction/get_operating_indicator_id', body)
  const getAllOperatingInd = (body) => api.post('transaction/get_all_operating_indicator_report', body)
  const getOperatingIndDetail = (body) => api.post('transaction/operating_indicator/get_operating_indicator_report_hierarki', body)
  const createOpetaingInd = (body) => api.post('transaction/operating_indicator/create_submission_report', body)
  const checkUploadOperatingInd = (body) => api.post('transaction/operating_indicator/check_import', body)
  const uploadOperatingInd = (body) => api.post('transaction/operating_indicator/import_operating_indicator', body)
  const getDashboard = (body) => api.get('transaction/get_dashboard')
  const historyApproval = (body) => api.post('transaction/master_budget/history_approval', body)

  //Template
  const downloadTemplate = (fileName, fileType) => api.get(`attachment/download_file?fileName=${fileName}&&fileType=${fileType}`)

  // UPLOAD FOTO
  const uploadFoto = (body) => api.post('attachment/upload_foto', body)

  // MANAGEMENT DOCUMENT
  // const getDocumentCategory = (body) => api.post('setting/get_all_setting_document_category', body)
  const getDocumentCategory = () => api.get('setting/get_all_setting_document_category')
  const getAllDocument = (body) => api.post('document/get_all_document', body)
  const uploadDocument = (body) => api.post('document/upload_document', body)
  const updateDocument = (body) => api.post('document/update_document', body)
  const downloadDocument = (body) => api.post('document/download_document', body)
  const getPerusahaanUserActive = () => api.get('company/get_all_user_company_active')
  const getDetailDocument = (id) => api.get(`document/get_document_by_id/${id}`)
  const deleteDocument = (id) => api.post(`document/delete_document/${id}`)

  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    getRoot,
    login,
    verification,
    resetPassword,
    isResetPassword,
    getRole,
    getDetailRole,
    searchRole,
    addRole,
    editRole,
    deleteRole,
    getMenu,
    getUnitBisnis,
    createUnitBisnis,
    updateUnitBisnis,
    searchUnitBisnis,
    getPerusahaan,
    createPerusahaan,
    updatePerusahaan,
    getAM,
    getApprovedByAM,
    getTypeAM,
    getOperatorAM,
    getDetailAM,
    searchAM,
    createAM,
    updateAM,
    updateVAM,
    checkUploadAM,
    uploadAM,
    deleteAM,
    getUser,
    getDetailUser,
    searchUser,
    createUser,
    updateUser,
    deleteUser,
    downloadTemplate,
    checkUploadUnitBisnis,
    uploadUnitBisnis,
    changePassword,
    getPerusahaanHierarki,
    checkUploadUser,
    uploadUser,
    getReportItems,
    searchReportItems,
    createReportItems,
    updateReportItems,
    getDetailReportItems,
    getInputType,
    getReportType,
    checkUploadReportItems,
    uploadReportItems,
    getAllParameter,
    getAllGroup,
    getParameterByGroup,
    getDetailParameter,
    updateParameter,
    createParameter,
    getPerusahaanActive,
    getRoleActive,
    checkUploadParameter,
    uploadParameter,
    getItemReportHierarki,
    getMenuByRole,
    saveVisualisasiReport,
    saveVisualisasiPerusahaan,
    getReportParent,
    searchParameter,
    checkUploadPerusahaan,
    getDetailPerusahaan,
    uploadPerusahaan,
    searchPerusahaan,
    getUnitBisnisActive,
    getMenuByUser,
    getDetailUnitBisnis,
    uploadFoto,
    getReportTypeBody,
    getPermission,
    getMasterBudgetAtt,
    uploadAttachment,
    getPeriodeTransaction,
    getMonthTransaction,
    getRevision,
    deleteAttachment,
    getDetailReportMB,
    deleteUnitBisnis,
    deleteParameter,
    deletePerusahaan,
    deleteReportItems,
    getDocumentCategory,
    getAllDocument,
    uploadDocument,
    updateDocument,
    downloadDocument,
    getPerusahaanUserActive,
    getDetailDocument,
    deleteDocument,
    createSubmitReport,
    getSubmission,
    checkUploadMB,
    getAllOperatingInd,
    getOperatingIndDetail,
    createOpetaingInd,
    uploadMasterBudget,
    getAllSettingByType,
    getOpetratingIndID,
    createAllItemReport,
    deleteAllItemReport,
    validateSubmitReport,
    checkUploadOperatingInd,
    uploadOperatingInd,
    getLastestUpdateMB,
    countingFormula,
    submitMasterBudget,
    checkIsSubmit,
    getIdDeleteFromExcel,
    getDashboard,
    historyApproval,
    checkApprover,
    approvalSubmission
  }
}

// let's return back our create method as the default.
export default {
  create
}