/* eslint-disable @typescript-eslint/no-explicit-any */
import { getFormDataHeader, getHeaderInfo } from '@/rtk/helpers/tokenCreator'
import { removeTokens } from '@/rtk/localStorage'
import { notifications } from '@mantine/notifications'
import axios from 'axios'
const handleResponse = (response: any) => {
  if (response.status === 401) {
    removeTokens()
    window.location.href = '/login'
  }
  if (response.data.status !== 'OK') {
    return response.data
  }
  return response
}

const handleResponseDownloadFile = (response: any) => {
  if (response.status === 401) {
    removeTokens()
    window.location.href = '/login'
  }
  if (response.status === 200) {
    return response
  }
  return response
}

/**
 *
 * @param url
 * @param body
 * @param otherHeader options
 * @returns
 */
export const post = async (url: string, body: any, otherHeader?: any) => {
  const header = await getHeaderInfo()
  try {
    if (otherHeader) {
      const resp = await axios.post(url, body, otherHeader)
      return handleResponse(resp)
    } else {
      const resp = await axios.post(url, body, header)
      return handleResponse(resp)
    }
  } catch (err: any) {
    if (err.response.status === 422) {
      const errorMess = err.response.data.message
      notifications.show({
        title: 'Error',
        message: errorMess,
        color: 'red',
      })
    }
    return handleResponse(err.response)
  }
}

/**
 *
 * @param url
 * @param params
 * @returns
 */
export const get = async (url: any, params: any = {}) => {
  const header = await getHeaderInfo()
  try {
    const resp = await axios.get(url, { ...header, params })
    return handleResponse(resp)
  } catch (err: any) {
    console.log(err)
    if (err.response.status === 422) {
      const errorMess = err.response.data.message
      notifications.show({
        title: 'Error',
        message: errorMess,
        color: 'red',
      })
    }
    throw handleResponse(err.response)
  }
}

export const getDownloadFile = async (url: any, params: any = {}) => {
  const header = await getHeaderInfo()

  try {
    const resp = await axios.get(url, {
      ...header,
      params,
      responseType: 'blob',
    })
    return handleResponseDownloadFile(resp)
  } catch (err: any) {
    console.log(err)
    if (err.response.status === 422) {
      const errorMess = err.response.data.message
      notifications.show({
        title: 'Error',
        message: errorMess,
        color: 'red',
      })
    }
    throw handleResponseDownloadFile(err.response)
  }
}

export const put = async (url: any, body: any) => {
  const header = await getHeaderInfo()

  try {
    const resp = await axios.put(url, body, header)

    return handleResponse(resp)
  } catch (err: any) {
    throw handleResponse(err.response)
  }
}

export const patch = async (url: any, body: any) => {
  const header = await getHeaderInfo()

  try {
    const resp = await axios.patch(url, body, header)

    return handleResponse(resp)
  } catch (err: any) {
    throw handleResponse(err.response)
  }
}

export const deleteApi = async (url: any) => {
  const header = await getHeaderInfo()

  try {
    const resp = await axios.delete(url, header)

    return handleResponse(resp)
  } catch (err: any) {
    throw handleResponse(err.response)
  }
}

export const postImage = async (url: string, body: any, method: any) => {
  const header = await getFormDataHeader()
  const formData = new FormData()
  formData.append('file', body)
  try {
    if (method === 'post') {
      const resp = await axios.post(url, formData, header)
      return handleResponse(resp)
    } else {
      const resp = await axios.put(url, formData, header)
      return handleResponse(resp)
    }
  } catch (err: any) {
    throw handleResponse(err.response)
  }
}

export const exportFile = async (
  url: string,
  params: any = {},
  fileName: string,
) => {
  const header = await getHeaderInfo()

  try {
    const response = await axios.get(url, {
      ...header,
      params,
      responseType: 'blob',
    })

    if (response.status === 200) {
      const blob = new Blob([response.data])
      const downloadUrl = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = downloadUrl
      link.download = fileName

      document.body.appendChild(link)
      link.click()
      window.URL.revokeObjectURL(downloadUrl)
      document.body.removeChild(link)

      // notifications.show({
      //   title: 'Success',
      //   message: 'Export successfully',
      //   color: 'green',
      // })

      return true
    }

    throw new Error('Export failed')
  } catch (error: any) {
    notifications.show({
      title: 'Error',
      message: error.message || 'Export failed',
      color: 'red',
    })
    throw error
  }
}
