import axios from 'axios'
import url from '@/plugins/routes'

const state = {
  month: [],
  allUsers: []
}

const mutations = {
  setAllUsers: (state, users) => {
    state.allUsers = []
    state.allUsers = users
    for (let i = 0; i < state.allUsers.length; i++) {
      state.allUsers[i].fullName =
        state.allUsers[i].firstname + ' ' + state.allUsers[i].lastname
    }
  },
  createMonth: (state, date) => {
    let month = []
    let id = 0
    const year = date.getFullYear()
    const mon = date.getMonth()
    let a = new Date(year, mon, 0, 13, 1)
    console.log('createMonth', a)
    let days = a.getDate()
    let startDate = 1
    for (let intDay = 1; intDay <= days; intDay++) {
      if (new Date(year, mon, intDay).getDay() === 3) {
        startDate = intDay
        break
      }
    }
    let week = { id: id, days: {} }
    for (let intDay = startDate; intDay <= days; intDay++) {
      let currentDate = new Date(year, mon, intDay, 13, 1)
      console.log('currentDate', currentDate)

      switch (currentDate.getDay()) {
        case 1:
          if (week.days.monday) {
            week.startDate = week.days.monday.date
          } else if (week.days.tuesday) {
            week.startDate = week.days.tuesday.date
          } else if (week.days.wednesday) {
            week.startDate = week.days.wednesday.date
          } else if (week.days.thursday) {
            week.startDate = week.days.thursday.date
          } else if (week.days.friday) {
            week.startDate = week.days.friday.date
          } else if (week.days.satturday) {
            week.startDate = week.days.satturday.date
          } else if (week.days.sunday) {
            week.startDate = week.days.sunday.date
          }

          if (week.days.sunday) {
            week.endDate = week.days.sunday.date
          } else if (week.days.satturday) {
            week.endDate = week.days.satturday.date
          } else if (week.days.friday) {
            week.endDate = week.days.friday.date
          } else if (week.days.thursday) {
            week.endDate = week.days.thursday.date
          } else if (week.days.wednesday) {
            week.endDate = week.days.wednesday.date
          } else if (week.days.tuesday) {
            week.endDate = week.days.tuesday.date
          } else if (week.days.monday) {
            week.endDate = week.days.monday.date
          }

          month.push(week)
          id++
          week = { id: id, days: {} }
          week.days.monday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Montag',
            worker: []
          }
          break
        case 2:
          week.days.tuesday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Dienstag',
            worker: []
          }
          break
        case 3:
          week.days.wednesday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Mittwoch',
            worker: []
          }
          break
        case 4:
          week.days.thursday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Donnerstag',
            worker: []
          }
          break
        case 5:
          week.days.friday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Freitag',
            worker: []
          }
          break
        case 6:
          week.days.satturday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Samstag',
            worker: []
          }
          break
        case 0:
          week.days.sunday = {
            id: currentDate.getDay(),
            date: currentDate,
            name: 'Sontag',
            worker: []
          }
          break
      }
    }
    if (week.days.monday) {
      week.startDate = week.days.monday.date
    } else if (week.days.tuesday) {
      week.startDate = week.days.tuesday.date
    } else if (week.days.wednesday) {
      week.startDate = week.days.wednesday.date
    } else if (week.days.thursday) {
      week.startDate = week.days.thursday.date
    } else if (week.days.friday) {
      week.startDate = week.days.friday.date
    } else if (week.days.satturday) {
      week.startDate = week.days.satturday.date
    } else if (week.days.sunday) {
      week.startDate = week.days.sunday.date
    }

    if (week.days.sunday) {
      week.endDate = week.days.sunday.date
    } else if (week.days.satturday) {
      week.endDate = week.days.satturday.date
    } else if (week.days.friday) {
      week.endDate = week.days.friday.date
    } else if (week.days.thursday) {
      week.endDate = week.days.thursday.date
    } else if (week.days.wednesday) {
      week.endDate = week.days.wednesday.date
    } else if (week.days.tuesday) {
      week.endDate = week.days.tuesday.date
    } else if (week.days.monday) {
      week.endDate = week.days.monday.date
    }
    month.push(week)
    state.month = month
    console.log(state.month)
  },
  setWorker: (state, data) => {
    console.log('hier ist die mutation', data)
    for (let week in state.month) {
      console.log('week', week)
      for (let day in state.month[week].days) {
        console.log(state.month[week].days[day], data.day)
        if (state.month[week].days[day].date - data.day.date === 0) {
          state.month[week].days[day].worker.push(data.worker)
          console.log('sm', state.month)
          return
        }
      }
    }
  }
}
const actions = {
  createMonth({ commit }, date) {
    commit('createMonth', date)
  },
  async getAllUsers({ commit, rootState, dispatch }) {
    try {
      const response = await axios.post(
        url.searchUser,
        { searchString: '' },
        { headers: { Token: rootState.login.user.accessToken } }
      )
      commit('setAllUsers', response.data)
    } catch (e) {
      if (e.response)
        if (e.response.data === 401) dispatch('logout', null, { root: true })
    }
  },
  setWorker({ commit }, data) {
    console.log('hier bin ich', data)
    commit('setWorker', data)
  }
}
const getters = {
  month: state => {
    return state.month
  },
  allUsers: state => {
    return state.allUsers
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}