import { defineStore } from 'pinia'

interface UiState {
  loading: boolean
  searchData: string
  currentPage: number
  passwordToken: string
  dealData: string
  userType: number
  filteredCategoriesId: number[]
  globalForm: Record<string, unknown>
  newNotification: boolean
  imgUrl: string
  exporting: boolean
  // Dialog states
  assignDialog: boolean
  jobDialog: boolean
  thankYouDialog: boolean
  privacyDialog: boolean
  confirmDialog: boolean
  compareDialog: boolean
  editRequestDialog: boolean
  cancelRequestDialog: boolean
  authRequireDialog: boolean
  addUser: boolean
  mapDialog: boolean
}

export const useUiStore = defineStore('ui', {
  state: (): UiState => ({
    loading: false,
    searchData: '',
    currentPage: 1,
    passwordToken: '',
    dealData: '/',
    userType: 2,
    filteredCategoriesId: [1, 2],
    globalForm: {},
    newNotification: false,
    imgUrl: '',
    exporting: false,
    assignDialog: false,
    jobDialog: false,
    thankYouDialog: false,
    privacyDialog: false,
    confirmDialog: false,
    compareDialog: false,
    editRequestDialog: false,
    cancelRequestDialog: false,
    authRequireDialog: false,
    addUser: false,
    mapDialog: false,
  }),

  actions: {
    setSearch(data: string) {
      this.searchData = data
    },

    startLoading() {
      this.loading = true
    },

    stopLoading() {
      this.loading = false
    },

    setDealData(path: string) {
      this.dealData = path
    },

    setPasswordToken(token: string) {
      this.passwordToken = token
    },

    setGlobalForm(formData: Record<string, unknown>) {
      this.globalForm = { ...formData }
    },

    setUserType(type: number) {
      this.userType = type
    },

    setFilteredCategories(ids: number[]) {
      this.filteredCategoriesId = ids
    },

    setImgUrl(url: string) {
      this.imgUrl = url
    },

    setExporting(val: boolean) {
      this.exporting = val
    },

    toggleNotification() {
      this.newNotification = !this.newNotification
    },

    toggleDialog(name: keyof Pick<UiState,
      'assignDialog' | 'jobDialog' | 'thankYouDialog' | 'privacyDialog' |
      'confirmDialog' | 'compareDialog' | 'editRequestDialog' |
      'cancelRequestDialog' | 'authRequireDialog' | 'addUser' | 'mapDialog'
    >) {
      this[name] = !this[name]
    },

    resetPage() {
      this.currentPage = 1
    },

    nextPage() {
      this.currentPage++
    },

    prevPage() {
      this.currentPage--
    },
  },
})
