/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
/* eslint-disable */
import axios from 'axios'
import { reject } from 'core-js/fn/promise'

export default {
    namespaced: true,
    state: {
        resorts: [],
        resort: {},
        images: [],
        layout: '',
        description: '',
        resortMedia: undefined,
        resortsWithListings: [],
    },
    mutations: {
        setResort(state, resort) {
            state.resort = resort
        },
        setDescription(state, description) {
            state.description = description
        },
        addImage(state, image) {
            state.images.push(`data:image/jpeg;base64,${image}`)
        },
        clear(state) {
            state.resort = {}
            state.images = []
            state.layout = ''
        },
        setLayout(state, image) {
            state.layout = `data:image/jpeg;base64,${image}`
        },
        setResorts(state, list) {
            state.resorts = list
        },
        setResortMedia(state, media) {
            state.resortMedia = media
        },
        setResortsWithListings(state, data) {
            state.resortsWithListings = data
        },
    },
    getters: {},
    actions: {
        async initResort({ commit, dispatch }, id) {
            commit('clear')
            await dispatch('getResort', id)
            dispatch('getDescription', id)
            dispatch('getImage1', id)
            dispatch('getImage2', id)
            dispatch('getImage3', id)
            dispatch('getImage4', id)
            dispatch('getImage5', id)
            dispatch('getLayout', id)
        },
        getDescription({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/dsc`,
                )
                .then((result) => {
                    commit('setDescription', result.data)
                })
                .catch(console.error)
        },
        getResort({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/TRESORT/${id}`,
                )
                .then((result) => {
                    commit('setResort', result.data)
                    console.log(result.data)
                })
                .catch(console.error)
        },
        async getImage1({ commit }, id) {
            return axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/image/0`,
                )
                .then((result) => {
                    commit('addImage', result.data)
                    return Promise.resolve(result.data)
                })
                .catch(console.error)
        },
        getImage2({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/image/1`,
                )
                .then((result) => commit('addImage', result.data))
                .catch(console.error)
        },
        getImage3({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/image/2`,
                )
                .then((result) => commit('addImage', result.data))
                .catch(console.error)
        },
        getImage4({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/image/3`,
                )
                .then((result) => {
                    if (result.data && result.data.length > 0) {
                        commit('addImage', result.data)
                    }
                })
                .catch(console.error)
        },
        getImage5({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/image/4`,
                )
                .then((result) => {
                    if (result.data && result.data.length > 0) {
                        commit('addImage', result.data)
                    }
                })
                .catch(console.error)
        },
        getLayout({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${id}/layout`,
                )
                .then((result) => commit('setLayout', result.data))
                .catch(console.error)
        },
        getResorts({ commit }, regionCode) {
            return new Promise((resolve, reject) => {
                axios
                    .get(`/api/resort/${regionCode}`)
                    .then((result) => {
                        commit('setResorts', result.data)
                        resolve()
                    })
                    .catch((error) => reject(new Error(error.message)))
            })
        },
        getResortMedia({ commit }, id) {
            axios
                .get(
                    `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/social/${id}`,
                )
                .then((result) => commit('setResortMedia', result.data))
        },
        async getResortData({ commit }, id) {
            await axios
                .get(`/api/resort/GetResortData/${id}`)
                .then((result) => {
                    commit('setResort', result.data)
                })
                .catch((error) => console.log(new Error(error.message)))
        },
        async getResortWithListings({ commit }, id) {
            await axios
                .get('/api/resort/GetPublishedResortsWithListings')
                .then((result) => {
                    commit('setResortsWithListings', result.data)
                })
        },
    },
}