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

export default {
  namespaced: true,
  state: {
    resort: {},
    image1: '',
    image2: '',
    image3: '',
    layout: '',
    description: '',
  },
  mutations: {
    setResort(state, resort) {
      state.resort = resort;
    },
    setDescription(state, description) {
      state.description = description;
    },
    clear(state) {
      state.resort = {};
      state.image1 = '';
      state.image2 = '';
      state.image3 = '';
      state.layout = '';
    },
    setImage1(state, image) {
      state.image1 = `data:image/jpeg;base64,${image}`;
    },
    setImage2(state, image) {
      state.image2 = `data:image/jpeg;base64,${image}`;
    },
    setImage3(state, image) {
      state.image3 = `data:image/jpeg;base64,${image}`;
    },
    setLayout(state, image) {
      state.layout = `data:image/jpeg;base64,${image}`;
    },
  },
  getters: {},
  actions: {
    initResort({
      commit,
      dispatch,
    }, resortCode) {
      commit('clear');
      dispatch('getResort', resortCode);
      dispatch('getDescription', resortCode);
      dispatch('getImage1', resortCode);
      dispatch('getImage2', resortCode);
      dispatch('getImage3', resortCode);
      dispatch('getLayout', resortCode);
    },
    getDescription({
      commit,
    }, resortCode) {
      axios.get(
        `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${resortCode}/dsc`,
      )
        .then((result) => {
          commit('setDescription', result.data);
        })
        .catch(console.error);
    },
    getResort({
      commit,
    }, resortCode) {
      axios.get(
        `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/TRESORT/${
          resortCode}`,
      )
        .then((result) => {
          commit('setResort', result.data);
        })
        .catch(console.error);
    },
    getImage1({
      commit,
    }, resortCode) {
      axios.get(`https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${
        resortCode}/image/1`)
        .then(result => commit('setImage1', result.data))
        .catch(console.error);
    },
    getImage2({
      commit,
    }, resortCode) {
      axios.get(`https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${
        resortCode}/image/2`)
        .then(result => commit('setImage2', result.data))
        .catch(console.error);
    },
    getImage3({
      commit,
    }, resortCode) {
      axios.get(`https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${
        resortCode}/image/3`)
        .then(result => commit('setImage3', result.data))
        .catch(console.error);
    },
    getLayout({
      commit,
    }, resortCode) {
      axios.get(`https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/${
        resortCode}/layout`)
        .then(result => commit('setLayout', result.data))
        .catch(console.error);
    },
  },
};
