/* eslint-disable no-unused-expressions */
/* eslint-disable eqeqeq */
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
/* eslint-disable */
import axios from 'axios'
import _, { min } from 'lodash'

export default {
    namespaced: true,
    state: {
        weeks: [],
        status: undefined,
        filter: {
            season: undefined,
            status: undefined,
            region: undefined,
            resort: undefined,
            bedrooms: undefined,
            date: undefined,
            ddate: undefined,
            minPrice: undefined,
            maxPrice: undefined,
            keyword: undefined,
        },
        prevFilter: undefined,
        difFilter: undefined,
        searchParams: {
            region: undefined,
            resort: undefined,
            regionObj: undefined,
        },
    },
    mutations: {
        onLoading(state) {
            state.status = 'Loading...'
        },
        onError(state) {
            state.status = 'Error occurred getting data.'
        },
        onClearFilter(state, filter) {
            state.filter[filter] = undefined
        },
        setWeeks(state, weeks) {
            state.weeks = weeks
            state.status = ''
        },
        addWeek(state, week) {
            state.weeks.push(week)
        },
        applyResort(state, resort) {
            state.filter.resort = resort
        },
    },
    getters: {
        filteredWeeks: (state) => {
            let weekList = state.weeks
            const { filter } = state
            if (filter) {
                if (filter.keyword) {
                    const list = []
                    for (const i in weekList) {
                        const item = weekList[i]
                        for (const r in item) {
                            const innerItem = item[r]
                            if (
                                innerItem &&
                                JSON.stringify(innerItem)
                                .toLowerCase()
                                .includes(filter.keyword.toLowerCase())
                            ) {
                                list.push(item)
                                break
                            }
                        }
                    }
                    weekList = list
                }
                if (filter.region) {
                    weekList = _.filter(
                        weekList,
                        (x) => x.region && x.region.regionCode === filter.region.regionCode,
                    )
                }
                if (filter.resort) {
                    weekList = _.filter(
                        weekList,
                        (x) =>
                        x.resort.resortCode &&
                        x.resort.resortCode === filter.resort.resortCode,
                    )
                }
                if (filter.bedrooms) {
                    if (filter.bedrooms === 'Studio') {
                        weekList = _.filter(
                            weekList,
                            (x) => (x.bedrooms && x.bedrooms === '0') || !x.bedrooms,
                        )
                    } else {
                        weekList = _.filter(
                            weekList,
                            (x) => x.bedrooms && x.bedrooms === filter.bedrooms,
                        )
                    }
                }
                if (filter.date) {
                    const minDate = new Date(filter.date)
                    minDate.setDate(minDate.getDate() - 1)
                    weekList = _.filter(
                        weekList,
                        (x) => new Date(x.arrivalDate) >= minDate,
                    )
                }
                if (filter.ddate) {
                    const maxDate = new Date(filter.ddate)
                    weekList = _.filter(
                        weekList,
                        (x) => new Date(x.departureDate) <= maxDate,
                    )
                }
                if (filter.minPrice && filter.minPrice !== 0) {
                    weekList = _.filter(
                        weekList,
                        (x) => x.sellPrice && x.sellPrice >= filter.minPrice,
                    )
                }
                if (filter.maxPrice && filter.maxPrice !== 0) {
                    weekList = _.filter(
                        weekList,
                        (x) => x.sellPrice && x.sellPrice <= filter.maxPrice,
                    )
                }
                if (filter.season) {
                    weekList = _.filter(weekList, (x) => x.season === filter.season.name)
                }
            }
            return _.sortBy(_.sortBy(weekList, 'arrivalDate'), 'resort.resortName')
        },
        getRegions(state, getters, rootState, rootGetters) {
            return rootGetters['timeshare/getRegionCode']('KK')
        },
        weekById(state) {
            return (resortCode, unitNumber) => {
                const w = state.weeks.find(
                    (week) =>
                    week.resort !== undefined &&
                    week.resort.resortCode === resortCode &&
                    week.unitNumber === unitNumber,
                )
                console.log(resortCode, unitNumber)

                if (w) {
                    return w
                }
            }
        },
    },
    actions: {
        applyResortFilter({ commit }, resortCode) {
            commit('applyResort', {
                resortCode,
            })
        },
        async getByResortCode({ commit }, resortCode) {
            commit('onLoading')
            await axios
                .get('/api/timeshareweek/getByResortCode/' + resortCode)
                .then((result) => {
                    commit('setWeeks', result.data)
                    console.log(result)
                })
        },
        async getWeeks({ commit, rootGetters }) {
            //commit("onLoading");
            await axios
                .get('/api/timeshareweek')
                .then((result) => {
                    const arr2 = result.data.filter((x) => x.region !== null)
                        //console.log(arr2)
                    const arr3 = result.data.filter((x) => x.region === null)
                    for (const item in arr3) {
                        const myItem = arr3[item]
                        if (myItem !== undefined) {
                            const code = myItem.resort.resortCode
                            myItem.region = {
                                regionName: rootGetters['timeshare/getRegionCode'](code),
                            }
                            myItem.resort = {
                                resortName: rootGetters['timeshare/getResortName'](code),
                                resortCode: code,
                            }
                        }
                    }

                    console.log(code)
                        //console.log(arr3)
                    commit('setWeeks', arr2)
                })
                .catch(console.error)
        },
        // getWeeks({ commit }) {
        //   axios.get("api/timeshareweek").then(result =>
        //     commit(
        //       "setWeeks",
        //       result.data.filter(x => x.region !== null)
        //     )
        //   );
        // },
        clearFilter({ commit }, filter) {
            commit('onClearFilter', filter)
        },
    },
}