import { defineStore } from 'pinia'
import { ref, reactive, computed } from 'vue'
import { useUsersStore } from '@/stores/usersStore'
import { useGlobalStore } from '@/stores/globalStore'

export const usePiyangoStore = defineStore('piyangoStore', () => {
  const usersStore = useUsersStore()
  const globalStore = useGlobalStore()

  const isNew = ref<boolean>(true)
  const lotteryId = ref<number | null>(null)
  const selectedLottery = ref<number | null>(null)
  const lotteryApprove = ref<number | null>(null)
  const lotteryDrawState = ref<boolean>(false)
  const lotteryDrawDate = ref<string | Date>('')
  const lotteryIlanTarihi = ref<string | Date | null>('')
  const lotteryFirstApproveDate = ref<string | Date | null>('')
  const lotteryCreationDate = ref<string | Date>('')
  const lotteryStateDate = ref<string | Date>('')
  const lotteryPurposeId = ref<number | null>(null)
  const lotteryBasvuruBedeliStatus = ref<boolean | null>(null)
  const lotteryIzinBedeliStatus = ref<boolean | null>(null)
  const subCustomerType = ref<number | null>(null)
  const customerApproved = ref<boolean>(false)
  const lotteryYetkiRefresh = ref<boolean>(false)

  const lotteryStatusTypes = ref<Record<string, any>[]>([
    { name: 'Bekliyor', class: 'waiting' },
    { name: 'Kapsam Dışı', class: 'waiting' },
    { name: 'Onaylandı', class: 'ok' },
    { name: 'İzin Verildi', class: 'ok' },
    { name: 'Kampanya Olumlu Bitti', class: 'ok' },
    { name: 'Katılımcı Listesi Yüklendi', class: 'next' },
    { name: 'Bilgi/Belge Eksik/Yanlış', class: 'waiting' },
    { name: 'Başvuru İşleme Alındı', class: 'waiting' },
    { name: 'Düzenleme Bekliyor', class: 'waiting' },
    { name: 'Üzerinde Çalışılıyor', class: 'waiting' },
    { name: 'Ön Kayıt Kabul Edildi', class: 'waiting' },
    { name: 'Yardım Amaçlı Piyango Son Onay', class: 'waiting' },
    { name: 'İnceleme Bekleniyor', class: 'beforeok' },
    { name: 'Yeni Piyango', class: 'new' },
    { name: 'Başvuru Reddedildi', class: 'alert' },
    { name: 'Başvuru Uygun Görülmedi', class: 'alert' },
    { name: 'İzin Öncesi İptal', class: 'alert' },
    { name: 'İzin Sonrası İptal', class: 'alert' },
    { name: 'Ceza Verildi', class: 'alert' },
    { name: 'Değişiklik Talep Ediliyor', class: 'change' }
  ])

  const baseLotteryData = reactive<Record<string, any>>({
    amac:'',
    amacpiyangoId: null,
    duzenleyenId: null,
    baslik: '',
    baglisirketId: null,
    cekilisSartveSekilleri: '',
    kapsamIl: [] as string[],
    kapsamIlce: [] as string[],
    baslangicTarihi: '',
    bitisTarihi: '',
    asilSonBasvuruTarihi: '',
    yedekSonBasvuruTarihi: '',
    cekilisTarihi: '',
    ilantarihi: '',
    ilantarihi2: '',
    ikramiyeTeslimTarihi: '',
    ilanEdilecekGazete: '',
    ilId: null,
    ilceId: null,
    adres: '',
    katilimListesi: '',
    katilimciListesiTeslimYontemiId: null,
    cekilisYontemiId: null,
    digerHususlar: ''
  })

  const lotteryData = reactive<Record<string, any>>({})
  const safeLotteryData = reactive<Record<string, any>>({})
  const duzenleyenData = reactive<Record<string, any>>({})
  const araciUyeData = reactive<Record<string, any>>({})
  const lotterySelectedProvience = ref<string>('')

  const UyeName = (data: Record<string, any>): string => {
    if (data.basvuruTipId === 18) return `${data.gercekAdi} ${data.gercekSoyadi}`
    else if (data.basvuruTipId === 21) return `${data.tuzelUnvan}`
    else if (data.basvuruTipId === 22) return `${data.dernekUnvan}`
    else if (data.basvuruTipId === 23) return `${data.sirketUnvan}`
    else if (data.basvuruTipId === 26) return `${data.kamuUnvan}`
    return ''
  }

  const duzenleyenLink = computed<string | null>(() => {
    if (lotteryData.baglisirketId === null && lotteryData.duzenleyenId !== null)
      return '/uyeler/detay/' + lotteryData.duzenleyenId
    else if (lotteryData.duzenleyenId !== null && lotteryData.baglisirketId !== null)
      return (
        '/uyeler/detay/' +
        araciUyeData.baglisirketId +
        '/yetkili-uye/detay/' +
        lotteryData.duzenleyenId
      )
    else return null
  })

  const araciLink = computed<string>(() => {
    return '/uyeler/detay/' + lotteryData.baglisirketId
  })

  const CheckForYardimAmacli = (): boolean => {
    return (
      usersStore.isVakifDernek ||
      (usersStore.isAraciFirma && subCustomerType.value === 22)
    )
  }

  const isLotteryForCharity = computed((): boolean => {
    return lotteryPurposeId.value === 2
  })

  const ResetLotteryData = () => {
    globalStore.ResetObject(lotteryData, baseLotteryData)
    Object.assign(lotteryData, baseLotteryData)
    lotteryApprove.value = null
    selectedLottery.value = null
    lotteryData.kapsamIl.splice(0,lotteryData.kapsamIl.length)
    lotteryData.kapsamIlce.splice(0,lotteryData.kapsamIlce.length)
  }

  const ResetLotteryContent = () => {
    Object.assign(lotteryData, baseLotteryData)
  }

  const LoterryStatusClass = (d: any): string => {
    let cls = lotteryStatusTypes.value.filter((v) => {
      return v.name === d
    })
    if (cls !== undefined && cls !== null && cls.length !== 0)
      return 'back-grad-' + cls[0].class
    else return 'back-grad-waiting'
  }

  const SetLotteryControlData = (data: Record<string, any>) => {
    lotteryApprove.value = data.onayDurum
    lotteryDrawDate.value = data.cekilisTarihi
    lotteryDrawState.value = data.cekilisDuzenlemeDurum
    lotteryStateDate.value = data.sonIslemTarihi
    lotteryPurposeId.value = data.piyangoAmacId
    lotteryFirstApproveDate.value = data.ilkIzinTarihi
    lotteryBasvuruBedeliStatus.value = data.basvuruBedeliDurum
    lotteryIzinBedeliStatus.value = data.izinBedeliDurum
  }
  return {
    isNew,
    lotteryId,
    baseLotteryData,
    selectedLottery,
    lotteryApprove,
    lotteryDrawState,
    lotteryDrawDate,
    lotteryIlanTarihi,
    lotteryFirstApproveDate,
    lotteryCreationDate,
    lotteryStateDate,
    lotteryBasvuruBedeliStatus,
    lotteryIzinBedeliStatus,
    subCustomerType,
    lotteryPurposeId,
    isLotteryForCharity,
    lotteryYetkiRefresh,
    lotteryData,
    safeLotteryData,
    duzenleyenData,
    araciUyeData,
    lotterySelectedProvience,
    customerApproved,
    duzenleyenLink,
    araciLink,
    UyeName,
    CheckForYardimAmacli,
    ResetLotteryData,
    ResetLotteryContent,
    LoterryStatusClass,
    SetLotteryControlData
  }
})
