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

export default {
  namespaced: true,
  state: {
    resorts: [],
    regions: [],
    detailedRegion: [],
    seasons: [],
    resortBedrooms: [],
    maxBedrooms: [],
    bankedEntities: [],
    agencies: [],
    agents: [],
    sellItem: {
      referedByAgent: false,
      agent: {},
      agency: {},
      otherResort: false,
      resort: {},
      region: '',
      season: '',
      module: '',
      weekNumber: '',
      levyAmount: 0,
      currentYearBanked: false,
      bankedWith: '',
      leviesPaidInFull: false,
      weekPlacedForRental: false,
      originalPurchasePrice: 0,
      originalPurchaseDate: null,
      arrivalDate: null,
      departureDate: null,
      status: {},
    },
  },
  mutations: {
    setAgency(state, agencies) {
      state.agencies = agencies;
    },
    setAgent(state, agents) {
      state.agents = agents;
    },
    setSeason(state, seasons) {
      state.seasons = seasons;
    },
    setResortBedrooms(state, resortBedrooms) {
      state.resortBedrooms = resortBedrooms;
    },
    setMaxBedrooms(state, maxBedrooms) {
      state.maxBedrooms = maxBedrooms;
    },
    setBankedEntities(state, bankedEntities) {
      state.bankedEntities = bankedEntities;
    },
    addResort(state, resorts) {
      state.resorts = resorts;
    },
    addRegion(state, regions) {
      state.regions = regions;
    },
    addDetailedRegion(state, detailedRegion) {
      state.detailedRegion.push(detailedRegion);
    },
  },
  getters: {

  },
  actions: {
    initTimeshare({
      commit,
      dispatch,
    }) {
      commit('setResortBedrooms', MyData.resortBedrooms);
      commit('setMaxBedrooms', MyData.maxBedrooms);
      commit('setBankedEntities', MyData.bankedEntities);
      dispatch('getSeason');
      dispatch('getAgents');
      dispatch('getAgencies');
      dispatch('getResorts');
      dispatch('getRegions');
    },
    getSeasons({
      commit,
    }) {
      axios
        .get('http://localhost:57260/api/season')
        .then(result => commit('setSeason', result.data))
        .catch(console.error);
    },
    getAgencies({
      commit,
    }) {
      axios
        .get('http://localhost:57260/api/agency')
        .then(result => commit('setAgency', result.data))
        .catch(console.error);
    },
    getAgents({
      commit,
    }) {
      axios
        .get('http://localhost:57260/api/agent')
        .then(result => commit('setAgent', result.data))
        .catch(console.error);
    },
    getResorts({
      commit,
    }) {
      axios
        .get('https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/resorts/list/')
        .then(result => commit('addResort', result.data))
        .catch(console.error);
    },
    getRegions({
      dispatch,
      commit,
    }) {
      axios
        .get('https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/regions/list/ZA')
        .then((result) => {
          commit('addRegion', result.data);
          dispatch('getDetailedRegion', result.data);
        })
        .catch(console.error);
    },
    getDetailedRegion({
      commit,
    }, data) {
      if (data) {
        for (const r in Object.keys(data)) {
          const region = data[r];
          if (region !== undefined) {
            axios
              .get(
                `https://www.tradeunipoint.com/unibackend/seam/resource/rest/products/resorts/${
                  region.regionCode
                }/`,
              )
              .then(result => commit('addDetailedRegion', {
                region,
                children: result.data,
              }))
              .catch(console.error);
          }
        }
      }
    },
  },
};
