import { defineStore } from 'pinia'
import { useDataStore } from '@/stores/dataStore'
import { usePiyangoStore } from '../stores/piyangoStore'
import { usePiyangoKatilimciStore } from '../stores/piyangoKatilimciStore'
import { usePiyangoKatilimciValidationStore } from '../validation/piyangoKatilimciValidationStore'
import { useUsersStore } from '@/stores/usersStore'

export const usePiyangoKatilimciService = defineStore('piyangoKatilimciService', () => {
  const dataStore = useDataStore()
  const piyangoStore = usePiyangoStore()
  const piyangoKatilimciStore = usePiyangoKatilimciStore()
  const piyangoKatilimciValidationStore = usePiyangoKatilimciValidationStore()
  const usersStore = useUsersStore()

  const SaveKatilimciUser = async () => {
    if (piyangoKatilimciValidationStore.UserFormCheck()) {
      let form: any
      piyangoKatilimciStore.piyangoKatilimciUserFormData.cekilisHakkiAdedi = Math.max(
        Number(piyangoKatilimciStore.piyangoKatilimciUserFormData.cekilisHakkiAdedi) || 1,
        1
      )

      if (!piyangoKatilimciStore.isPiyangoKatilimciUserUpdate) {
        delete piyangoKatilimciStore.piyangoKatilimciUserFormData.id
        delete piyangoKatilimciStore.piyangoKatilimciUserFormData.cekilis
        form = await dataStore.dataPost('Katilimci', {
          data: piyangoKatilimciStore.piyangoKatilimciUserFormData
        })
      } else {
        form = await dataStore.dataPut(
          'Katilimci/' + piyangoKatilimciStore.piyangoKatilimciUserFormData.id,
          {
            data: piyangoKatilimciStore.piyangoKatilimciUserFormData
          }
        )
      }

      if (form !== 'errorfalse') {
        piyangoKatilimciStore.refreshPiyangoKatilimciList = true
        piyangoKatilimciStore.katilimciUserPanel = false
      }

      if (form !== 'errorfalse') {
        piyangoKatilimciStore.ResetFormData()
      }
    } else {
      piyangoKatilimciValidationStore.isUserFormValid = true
    }
  }

  const CreateOnlineDraw = async () => {
    if (usersStore.isPanelUser) {
      let dt = await dataStore.dataGet(
        'Cekilis/CekilisDuzenle/' + piyangoStore.selectedLottery
      )
    }
  }

  const KatilimciData = async (id: number): Promise<Record<string, any>> => {
    let dt = await dataStore.dataGet('Katilimci/' + id)

    if (dt !== 'errorfalse') {
      return dt
    } else return {}
  }

  const KatilimciFileUpload = async () => {
    if (piyangoKatilimciValidationStore.FileFormCheck()) {
      const formData = new FormData()
      formData.append('excelFile', piyangoKatilimciStore.piyangoKatilimciFileFormData.excelFile)
      let dt: any

      dt = await dataStore.dataPost(
        'Katilimci/ExcelleYukle/' + piyangoStore.selectedLottery,
        {
          data: formData,
          headers: { 'Content-Type': 'multipart/form-data' }
        }
      )
      if (dt !== 'errorfalse') {
        piyangoKatilimciStore.refreshPiyangoKatilimciList = true
        piyangoKatilimciStore.katilimciFilePanel = false
      }
    } else {
      piyangoKatilimciValidationStore.isFileFormValid = true
    }
  }

  const GetUploadJobs = async (cekilisId: number): Promise<Record<string, any>[]> => {
    const dt = await dataStore.dataGet(`ExcelUploadJob/my-jobs?cekilisId=${cekilisId}`)
    if (dt !== 'errorfalse' && Array.isArray(dt)) {
      return dt
    }
    return []
  }

  const GetUploadJobDetail = async (guid: string): Promise<Record<string, any> | null> => {
    const dt = await dataStore.dataGet(`ExcelUploadJob/${guid}`)
    if (dt !== 'errorfalse') {
      return dt
    }
    return null
  }

  const GetUploadJobStatus = async (guid: string): Promise<Record<string, any> | null> => {
    const dt = await dataStore.dataGet(`ExcelUploadJob/status/${guid}`)
    if (dt !== 'errorfalse') {
      return dt
    }
    return null
  }

  const SubscribeToJob = async (guid: string, connectionId: string): Promise<boolean> => {
    const result = await dataStore.dataPost(
      `ExcelUploadJob/subscribe?guid=${guid}&connectionId=${encodeURIComponent(connectionId)}`,
      {}
    )
    return result !== 'errorfalse'
  }

  return {
    SaveKatilimciUser,
    CreateOnlineDraw,
    KatilimciData,
    KatilimciFileUpload,
    GetUploadJobs,
    GetUploadJobDetail,
    GetUploadJobStatus,
    SubscribeToJob
  }
})
