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

// our "constructor"
const create = (baseURL = 'https://tia.eksad.com/tia-reporting-dev/public/') => {
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const 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',
    },
    // 10 second timeout...
    timeout: 10000
  })

  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')

  //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)

  // 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')

  // 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 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)

  //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.get(`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_report/get_all_type_report')
  const getReportType = () => api.get('report/get_all_report')
  const searchReportItems = (body) => api.post('/item_report/search_item_report', body)
  const createReportItems = (body) => api.post('/item_report/create_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 = () => api.get('item_report/get_item_report_hierarki')

  //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)

  //Template
  const downloadTemplate = (fileName, fileType) => api.get(`attachment/download_file?fileName=${fileName}&&fileType=${fileType}`)
  // ------
  // 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,
    searchAM,
    createAM,
    updateAM,
    getUser,
    getDetailUser,
    searchUser,
    createUser,
    updateUser,
    deleteUser,
    downloadTemplate,
    checkUploadUnitBisnis,
    uploadUnitBisnis,
    changePassword,
    getPerusahaanHierarki,
    checkUploadUser,
    uploadUser,
    getReportItems,
    searchReportItems,
    createReportItems,
    getInputType,
    getReportType,
    checkUploadReportItems,
    uploadReportItems,
    getAllParameter,
    getAllGroup,
    getParameterByGroup,
    getDetailParameter,
    updateParameter,
    createParameter,
    getPerusahaanActive,
    getRoleActive,
    checkUploadParameter,
    uploadParameter,
    getItemReportHierarki,
    getMenuByRole
  }
}

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