import { Api } from '@api'
import type { IBookedEvent, ICalendarState, IEvents, IOperators } from 'model'

import { serv_constants } from './Modules/serv_constants'
import { tools } from './Modules/tools'
import { defineStore } from 'pinia'
import { useUserStore } from '@store/UserStore'
import { useGlobalStore } from '@store/globalStore'
import { costanti } from '@costanti'

export const useCalendarStore = defineStore('CalendarStore', {
  state: (): ICalendarState => ({
    editable: false,
    eventlist: [],
    bookedevent: [],
    operators: [],
    internalpages: [],
    wheres: [],
    contribtype: [],
    // ---------------
    titlebarHeight: 0,
    locale: 'it-IT',
    maxDays: 1,
    fiveDayWorkWeek: false,
    shortMonthLabel: false,
    showDayOfYearLabel: false,
    shortWeekdayLabel: true,
    shortIntervalLabel: false,
    hour24Format: true,
    hideHeader: false,
    noScroll: false,
    showMonthLabel: false,
    showWorkWeeks: false,
    intervalRange: { min: 9, max: 23 },
    intervalRangeStep: 1,
    intervalHeight: 35, // 35
    resourceHeight: 80, // 60
    resourceWidth: 100,
    dayHeight: 150,
    enableThemes: false,
    theme: {},
  }),
  getters: {

    updatearrBookingEvent: (mystate: ICalendarState) => (id_bookedevent: any, arrBookings: IBookedEvent[]) => {
      // Rimuovi quelli precedenti
      mystate.bookedevent = mystate.bookedevent.filter((rec: IBookedEvent) => rec.id_bookedevent !== id_bookedevent)

      if (arrBookings) {
        // Aggiorna quelli appena arrivati:
        mystate.bookedevent = [...mystate.bookedevent, ...arrBookings]
      }

      console.log('BOOKEDEV: ', mystate.bookedevent)

    },

    findEventBooked: (mystate: ICalendarState) => (id: any, isconfirmed: boolean) => {
      const userStore = useUserStore()
      return mystate.bookedevent.find((bookedevent) => (bookedevent.id_bookedevent === id) && (bookedevent.userId === userStore.my._id) && ((isconfirmed && bookedevent.booked) || (!isconfirmed)))
    },

    findAllBookedByIdEvent: (mystate: ICalendarState) => (id_bookedevent: any) => {
      const arr = mystate.bookedevent.filter((rec: IBookedEvent) => (rec.id_bookedevent === id_bookedevent) && rec.booked)
      if (arr)
        return arr
      else
        return []
    },


    isPartecipero: (mystate: ICalendarState) => (id: any, table: string) => {
      const userStore = useUserStore()
      const isconfirmed = true
      const mybooked = mystate.bookedevent.find((bookedevent) => (bookedevent.id_bookedevent === id) && (bookedevent.userId === userStore.my._id) && ((isconfirmed && bookedevent.booked) || (!isconfirmed)))
      return !!mybooked
    },

    getNumParticipants: (mystate: ICalendarState) => (myevent: IEvents, showall: boolean, tipo = 0): number => {
      const userStore = useUserStore()
      const myarr = mystate.bookedevent.filter((bookedevent) => (bookedevent.id_bookedevent === myevent._id) && (bookedevent.booked) && (showall || (!showall && bookedevent.userId === userStore.my._id)) && (((tipo === tools.peopleWhere.participants) && bookedevent.numpeople > 0) || ((tipo === tools.peopleWhere.lunch && bookedevent.numpeopleLunch! > 0) || (tipo === tools.peopleWhere.dinner && bookedevent.numpeopleDinner! > 0) || (tipo === tools.peopleWhere.dinnerShared && bookedevent.numpeopleDinnerShared! > 0))))
      if (myarr.length > 0) {
        let ris = null
        if (tipo === tools.peopleWhere.participants) {
          ris = myarr.reduce((sum, bookedevent) => sum + bookedevent.numpeople, 0)
        } else if (tipo === tools.peopleWhere.lunch) {
          ris = myarr.reduce((sum, bookedevent) => sum + bookedevent.numpeopleLunch!, 0)
        } else if (tipo === tools.peopleWhere.dinner) {
          ris = myarr.reduce((sum, bookedevent) => sum + bookedevent.numpeopleDinner!, 0)
        } else if (tipo === tools.peopleWhere.dinnerShared) {
          ris = myarr.reduce((sum, bookedevent) => sum + bookedevent.numpeopleDinnerShared!, 0)
        }

        return ris ? ris : 0
      } else {
        return 0
      }
    },

    getEventsBookedByIdEvent: (mystate: ICalendarState) => (idevent: string, showall: boolean) => {
      const userStore = useUserStore()
      return mystate.bookedevent.filter((bookedevent) => (bookedevent.id_bookedevent === idevent) && (bookedevent.booked) && (showall || (!showall && bookedevent.userId === userStore.my._id)))
    },

    getWhereRec: (mystate: ICalendarState) => (wherecode: string) => {
      return mystate.wheres.find((mywhere) => mywhere.code === wherecode)

    },

    getContribtypeRec: (mystate: ICalendarState) => (id: string) => {
      const ctrec = mystate.contribtype.find((mycontr) => mycontr._id === id)
      return (ctrec)

    },

    getContribtypeById: (mystate: ICalendarState) => (id: string) => {
      const ctrec = mystate.contribtype.find((mycontr) => mycontr._id === id)
      return (ctrec) ? ctrec.label : ''

    },
    getColByContribType: (mystate: ICalendarState) => (id: string) => {
      const ctrec = mystate.contribtype.find((mycontr) => mycontr._id === id)
      return (ctrec && ctrec.color) ? ctrec.color : 'green'

    },
    getContribtypeRecByLabel: (mystate: ICalendarState) => (label: string) => {
      const ctrec = mystate.contribtype.find((mycontr) => mycontr.label === label)
      return (ctrec)

    },

  },
  actions: {

    getOperatorByUsername(username: string): IOperators | undefined {
      const ctrec = this.operators.find((rec: IOperators) => rec.username === username)
      return ctrec
    },

    getImgTeacherByUsername(username: string): string {
      if (username === '')
        return ''
      // Check if is this User!
      const myop = this.getOperatorByUsername(username)
      if (myop && !!myop.img && myop.img !== '' && myop.img !== 'undefined') {
        return myop.img
      } else {
        return ''
      }
    },

    getparambyevent(bookevent: IBookedEvent) {
      const userStore = useUserStore()
      return {
        _id: bookevent._id,
        id_bookedevent: bookevent.id_bookedevent,
        username: bookevent.username,
        infoevent: bookevent.infoevent,
        numpeople: bookevent.numpeople,
        numpeopleLunch: bookevent.numpeopleLunch,
        numpeopleDinner: bookevent.numpeopleDinner,
        numpeopleDinnerShared: bookevent.numpeopleDinnerShared,
        msgbooking: bookevent.msgbooking,
        datebooked: bookevent.datebooked,
        userId: userStore.my._id,
        booked: bookevent.booked,
        modified: bookevent.modified,
      }
    },

    async BookEvent(bookevent: IBookedEvent) {
      console.log('BookEvent', bookevent)

      const param = this.getparambyevent(bookevent)

      return Api.SendReq('/booking', 'POST', param)
        .then((res) => {
          if (res.status === 200) {
            console.log('datares', res.data)
            if (res.data.code === serv_constants.RIS_CODE_OK) {
              bookevent._id = res.data.id
              if (bookevent.modified) {

                const foundIndex = this.bookedevent.findIndex((x) => x.id_bookedevent === bookevent.id_bookedevent)
                if (foundIndex >= 0)
                  this.bookedevent[foundIndex] = bookevent

              } else {
                this.bookedevent.push(bookevent)
              }
              return true
            }
          }
          return false
        })
        .catch((error) => {
          console.error(error)
          return false
        })

    },

    async CancelEvent({ id }: { id: string }) {
      const globalStore = useGlobalStore()
      return globalStore.DeleteRec({ table: costanti.TABEVENTS, id })
    },

    intervalStart() {
      return this.intervalRange.min * (1 / this.intervalRangeStep)
    },

    intervalCount() {
      return (this.intervalRange.max - this.intervalRange.min) * (1 / this.intervalRangeStep)
    },


    async CancelBookingEvent({ ideventbook, notify }: { ideventbook: string, notify: string }) {
      console.log('CALSTORE: CancelBookingEvent', ideventbook, notify)

      return Api.SendReq('/booking/' + ideventbook + '/' + notify + '/' + tools.getEnv('VITE_APP_ID'), 'DELETE', null)
        .then((res) => {
          if (res.status === 200) {
            if (res.data.code === serv_constants.RIS_CODE_OK) {

              // Remove this record from my list
              this.bookedevent = this.bookedevent.filter((eventbooked) => (eventbooked._id !== ideventbook))

              return true
            }
          }
          return false

        })
        .catch((error) => {
          console.error(error)
          // userStore.mutations.setErrorCatch(error)
          return false
        })
    },

  },
})
