/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
import axios from 'axios'

export default {
    namespaced: true,
    state: {
        regions: [],
        detailedRegion: [],
        availRegion: [],
        initFlag: false,
    },
    mutations: {
        addItem(state, item) {
            state[item.name].push(item.value)
        },
        setItem(state, item) {
            state[item.name] = item.value
        },
    },
    getters: {},
    actions: {
        init({ state, commit, dispatch }) {
            if (!state.initFlag) {
                commit('setItem', {
                    name: 'availRegion',
                    value: [],
                })
                dispatch('getAvail')
                dispatch('getRegions')
                commit('setItem', {
                    name: 'initFlag',
                    value: true,
                })
            }
        },
        forceInit({ commit, dispatch }) {
            commit('setItem', {
                name: 'initFlag',
                value: false,
            })
            dispatch('init')
        },
        getAvail({ commit }) {
            axios
                .get('/api/timeshareweek/getAvailResort')
                .then((r) => {
                    for (const reg in r.data) {
                        const newRegion = {}
                        newRegion.regionCode = r.data[reg].regionCode
                        newRegion.regionName = r.data[reg].regionName
                        newRegion.available = r.data[reg].available
                        newRegion.resorts = []
                        for (const res in r.data[reg].resorts) {
                            const newResort = {
                                resortId: '',
                                resortCode: '',
                                resortName: '',
                                lat: 0,
                                long: 0,
                            }
                            newResort.resortCode = r.data[reg].resorts[res].resortCode
                            newResort.resortName = r.data[reg].resorts[res].resortName
                            newResort.available = r.data[reg].resorts[res].available
                            newResort.resortId = r.data[reg].resorts[res].resortId
                            if (r.data[reg].resorts[res]) {
                                axios
                                    .get(
                                        `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/TRESORT/${r.data[reg].resorts[res].resortId}`,
                                    )
                                    .then((result) => {
                                        newResort.lat = result.data.prLatitude
                                        newResort.long = result.data.prLongitude
                                    })
                                    .catch(console.error)
                            }
                            newRegion.resorts.push(newResort)
                        }
                        commit('addItem', {
                            name: 'availRegion',
                            value: newRegion,
                        })
                    }
                })
                .catch(console.error)
        },
        getRegions({ commit, dispatch }) {
            commit('setItem', {
                name: 'detailedRegion',
                value: [],
            })
            axios
                .get(
                    'https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/regions/list/ZA/',
                )
                .then((result) => {
                    commit('setItem', {
                        name: 'regions',
                        value: result.data,
                    })
                    if (result.data) {
                        for (const i in result.data) {
                            const region = result.data[i]
                            dispatch('getDetailedRegion', region)
                        }
                    }
                })
                .catch(console.error)
        },
        getDetailedRegion({ commit }, region) {
            if (region) {
                axios
                    .get(
                        `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/resorts/${region.regionCode}/`,
                    )
                    .then((result) =>
                        commit('addItem', {
                            name: 'detailedRegion',
                            value: {
                                id: region.id,
                                regionCode: region.regionCode,
                                regionName: region.regionName,
                                resorts: result.data,
                            },
                        }),
                    )
                    .catch(console.error)
            }
        },
    },
}