import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { useGlobalDataStore } from '@/stores/globalDataStore'
import { useToastStore } from '@/components/global/toastStore'
import axios from 'axios'

export const useDataStore = defineStore('dataStore', () => {
  const globalDataStore = useGlobalDataStore()
  const toastStore = useToastStore()

  const siteBase = ref<string>(import.meta.env.VITE_API_URL)
  const apiBase = ref<string>('api/')
  const panelData = reactive<Record<string, any>>({})
  const pageData = reactive<Record<string, any>>({})
  const panelDataChanged = ref<boolean>(false)
  const pageDataChanged = ref<boolean>(false)
  const pageDataUpdate = ref<boolean>(false)
  const isLoading = ref<boolean>(false)

  const dataGet = async (
    api: string,
    data: {
      data?: any
      params?: Record<string, any>
      headers?: Record<string, any>
      options?: Record<string, any>
      toast?: Record<string, any>
      full?: boolean
    } = {}
  ) => {
    try {
      const sendData: Record<string, any> = {}

      if (data.options !== undefined) Object.assign(sendData, data.options)
      if (data.data !== undefined) sendData.data = data.data
      if (data.params !== undefined) sendData.params = data.params
      if (data.headers !== undefined) sendData.headers = data.headers
      if (data.toast !== undefined) {
        toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout)
      }

      const response = await axios.get(apiBase.value + api, sendData)
      console.log('response --', api, response)

      if (data.full !== undefined && data.full) {
        return response
      } else {
        return response.data
      }
    } catch (error: any) {
      CheckApiError(error.response.status, error.response.data)
      console.error('Hata oluştu -:', error)
      return 'errorfalse'
    }
  }

  const dataPost = async (
    api: string,
    data: {
      data?: any
      params?: Record<string, any>
      headers?: Record<string, any>
      options?: Record<string, any>
      toast?: Record<string, any>
      full?: boolean
    } = {}
  ): Promise<any> => {
    try {
      const sendData: Record<string, any> = {}
      const postData = data.data || {}

      if (data.options !== undefined) Object.assign(sendData, data.options)
      if (data.params !== undefined) sendData.params = data.params
      if (data.headers !== undefined) sendData.headers = data.headers
      if (data.toast !== undefined) {
        toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout)
      }

      const response = await axios.post(apiBase.value + api, postData, sendData)

      if (data.full !== undefined && data.full) {
        return response
      } else {
        return response.data
      }
    } catch (error: any) {
      CheckApiError(error.response.status, error.response.data)

      console.error('Hata oluştu:', error)
      return Promise.resolve('errorfalse')
    }
  }

  const dataPut = async (
    api: string,
    data: {
      data?: any
      params?: Record<string, any>
      headers?: Record<string, any>
      options?: Record<string, any>
      toast?: Record<string, any>
      full?: boolean
    } = {}
  ): Promise<any> => {
    try {
      const sendData: Record<string, any> = {}
      const postData = data.data || {}

      if (data.options !== undefined) Object.assign(sendData, data.options)
      if (data.params !== undefined) sendData.params = data.params
      if (data.headers !== undefined) sendData.headers = data.headers
      if (data.toast !== undefined) {
        toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout)
      }

      const response = await axios.put(apiBase.value + api, postData, sendData)
      if (data.full !== undefined && data.full) {
        return response
      } else {
        return response.data
      }
    } catch (error: any) {
      CheckApiError(error.response.status, error.response.data)

      console.error('Hata oluştu:', error)
      return Promise.resolve('errorfalse')
    }
  }

  const dataDelete = async (
    api: string,
    data: {
      data?: object
      params?: Record<string, any>
      headers?: Record<string, any>
      options?: Record<string, any>
      toast?: Record<string, any>
      full?: boolean
    } = {}
  ): Promise<any> => {
    try {
      const sendData: Record<string, any> = {}

      if (data.options !== undefined) Object.assign(sendData, data.options)
      if (data.params !== undefined) sendData.params = data.params
      if (data.headers !== undefined) sendData.headers = data.headers
      if (data.toast !== undefined) {
        toastStore.AddToast(data.toast.toast, data.toast.type, data.toast.timeout)
      }

      const response = await axios.delete(apiBase.value + api, sendData)
      if (data.full !== undefined && data.full) {
        return response
      } else {
        return response.data
      }
    } catch (error: any) {
      CheckApiError(error.response.status, error.response.data)
      console.error('Hata oluştu:', error)
      return Promise.resolve('errorfalse')
    }
  }

  const CheckApiError = async (status: number, data: Record<string, any>) => {
    if (status === 400) {
      const errorKey = data['hata'] || data['errors']

      if (errorKey !== undefined) {
        errorKey.forEach((el: string) => {
          toastStore.AddToast(el, 'alert')
        })
      } else {
        toastStore.AddToast('Bir hata oluştu.', 'alert')
      }
    }
  }

  const GetCustomerTipList = async () => {
    if (globalDataStore.customerTips.length === 0) {
      let list = await dataGet('Auth/uyetipiList')

      if (list !== 'errorfalse') globalDataStore.customerTips = list
    }
  }

  const GetIlList = async () => {
    if (globalDataStore.ilList.length === 0) {
      let list = await dataGet('Il')

      if (list !== 'errorfalse') globalDataStore.ilList = list
    }
  }

  return {
    isLoading,
    siteBase,
    apiBase,
    panelData,
    pageData,
    panelDataChanged,
    pageDataChanged,
    pageDataUpdate,
    dataGet,
    dataPost,
    dataPut,
    dataDelete,
    GetCustomerTipList,
    GetIlList
  }
})
