import { defineStore } from 'pinia'
import { ref, reactive, computed } from 'vue'
import { usePiyangoStore } from './piyangoStore'
import { useUsersStore } from '@/stores/usersStore'
import { useValidationStore } from '@/stores/validationStore'
import { usePiyangoYardimAmacliValidationStore } from '../validation/piyangoYardimAmacliValidationStore'

export const usePiyangoYardimAmacliStore = defineStore('piyangoYardimAmacliStore', () => {
  const usersStore = useUsersStore()
  const piyangoStore = usePiyangoStore()
  const validationStore = useValidationStore()
  const piyangoYardimAmacliValidationStore = usePiyangoYardimAmacliValidationStore()

  const yardimAmacliRakamlar = ref<Record<string, any>[]>([
    { title: 'Üç Rakam (000-999)', prefix: 'uc', min: 1, max: 999, maxl: 3 },
    { title: 'Dort Rakam (0000-9999)', prefix: 'dort', min: 1000, max: 9999, maxl: 4 },
    { title: 'Beş Rakam (00000-99999)', prefix: 'bes', min: 10000, max: 99999, maxl: 5 },
    {
      title: 'Altı Rakam (000000-999999)',
      prefix: 'alti',
      min: 100000,
      max: 999999,
      maxl: 6
    }
  ])

  const piyangoYardimAmacliSafeFormData = reactive<Record<string, any>>({
    duzenleyenId: usersStore.userId,
    cekilsiId: piyangoStore.selectedLottery,
    basimevi: '',
    basimeviAdresi: '',
    birimSatisFiyati: null,
    altiRakamAdet: null,
    altiRakamBaslangic: null,
    altiRakamBitis: null,
    besRakamAdet: null,
    besRakamBaslangic: null,
    besRakamBitis: null,
    dortRakamAdet: null,
    dortRakamBaslangic: null,
    dortRakamBitis: null,
    ucRakamAdet: null,
    ucRakamBaslangic: null,
    ucRakamBitis: null
  })

  const piyangoYardimAmacliFormData = reactive<Record<string, any>>({})
  const piyangoYardimAmacliServerFormData = reactive<Record<string, any>>({})
  const loaded = ref<boolean>(false)
  const isNew = ref<boolean>(false)

  const ResetFormData = () => {
    loaded.value = false
    if (isNew.value)
      Object.assign(piyangoYardimAmacliFormData, piyangoYardimAmacliSafeFormData)
    else Object.assign(piyangoYardimAmacliFormData, piyangoYardimAmacliServerFormData)
    piyangoYardimAmacliValidationStore.formChanged = false
    setTimeout(() => {
      loaded.value = true
    }, 30)
  }

  const EmptyFormData = () => {
    Object.assign(piyangoYardimAmacliFormData, piyangoYardimAmacliSafeFormData)
  }

  const CreateBiletTable = () => {
    for (let i = 0; i < yardimAmacliRakamlar.value.length; i++) {
      piyangoYardimAmacliFormData[
        yardimAmacliRakamlar.value[i].prefix + 'RakamBaslangic'
      ] = null
      piyangoYardimAmacliFormData[yardimAmacliRakamlar.value[i].prefix + 'RakamBitis'] =
        null
      piyangoYardimAmacliFormData[yardimAmacliRakamlar.value[i].prefix + 'RakamAdet'] =
        null
    }
  }

  const CalculateToplamBilet = (data: Record<string, any>) => {
    if (
      !validationStore.checkEmpty(
        piyangoYardimAmacliFormData[data.prefix + 'RakamBaslangic']
      ) &&
      validationStore.checkEmpty(piyangoYardimAmacliFormData[data.prefix + 'RakamBitis'])
    ) {
      piyangoYardimAmacliValidationStore.invalidTexts[data.prefix + 'RakamBitis'] =
        'Bitiş numarasını girmelisiniz.'
    } else {
      delete piyangoYardimAmacliValidationStore.invalidTexts[data.prefix + 'RakamBitis']
    }

    if (
      validationStore.checkEmpty(
        piyangoYardimAmacliFormData[data.prefix + 'RakamBaslangic']
      ) &&
      !validationStore.checkEmpty(piyangoYardimAmacliFormData[data.prefix + 'RakamBitis'])
    ) {
      piyangoYardimAmacliValidationStore.invalidTexts[data.prefix + 'RakamBaslangic'] =
        'Başlangıç numarasını girmelisiniz.'
    } else {
      delete piyangoYardimAmacliValidationStore.invalidTexts[
        data.prefix + 'RakamBaslangic'
      ]
    }

    if (
      !validationStore.checkEmpty(
        piyangoYardimAmacliFormData[data.prefix + 'RakamBaslangic']
      ) &&
      !validationStore.checkEmpty(piyangoYardimAmacliFormData[data.prefix + 'RakamBitis'])
    ) {
      if (
        parseInt(piyangoYardimAmacliFormData[data.prefix + 'RakamBaslangic']) <
        parseInt(piyangoYardimAmacliFormData[data.prefix + 'RakamBitis'])
      ) {
        delete piyangoYardimAmacliValidationStore.invalidTexts[data.prefix + 'RakamBitis']
        piyangoYardimAmacliFormData[data.prefix + 'RakamAdet'] =
          piyangoYardimAmacliFormData[data.prefix + 'RakamBitis'] -
          piyangoYardimAmacliFormData[data.prefix + 'RakamBaslangic'] +
          1
      } else {
        piyangoYardimAmacliFormData[data.prefix + 'RakamAdet'] = ''
        piyangoYardimAmacliValidationStore.invalidTexts[data.prefix + 'RakamBitis'] =
          'Bitiş numarasını başlangıç numarasından büyük olmalıdır.'
      }
    } else {
      piyangoYardimAmacliFormData[data.prefix + 'RakamAdet'] = ''
    }

    piyangoYardimAmacliValidationStore.formChanged = true
  }

  const toplamBilet = computed(() => {
    var toplam:number = 0
    yardimAmacliRakamlar.value.forEach((rakam) => {
      if(piyangoYardimAmacliFormData[rakam.prefix + 'RakamAdet'] !== null){
        toplam += Number(piyangoYardimAmacliFormData[rakam.prefix + 'RakamAdet'])
      }
    })
    return toplam
  })

  return {
    yardimAmacliRakamlar,
    piyangoYardimAmacliSafeFormData,
    piyangoYardimAmacliFormData,
    piyangoYardimAmacliServerFormData,
    loaded,
    isNew,
    toplamBilet,
    ResetFormData,
    EmptyFormData,
    CreateBiletTable,
    CalculateToplamBilet
  }
})
