/* 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: [],
    maxSleep: [],
    bankedEntities: [],
    agencies: [],
    agents: [],
    sellItem: {
      referedByAgent: false,
      agent: undefined,
      agency: undefined,
      otherResort: false,
      otherResortName: undefined,
      resort: undefined,
      region: undefined,
      owner: undefined,
      season: undefined,
      module: undefined,
      unitNumber: undefined,
      bedrooms: undefined,
      maxSleep: undefined,
      weekNumber: undefined,
      levyAmount: undefined,
      currentYearBanked: false,
      bankedWith: undefined,
      leviesPaidInFull: false,
      weekPlacedForRental: false,
      originalPurchasePrice: undefined,
      originalPurchaseDate: undefined,
      arrivalDate: undefined,
      departureDate: undefined,
      sellingPrice: undefined,
      agentCommission: undefined,
      mandate: undefined,
      status: undefined,
    },
  },
  mutations: {
    setUnitConfigurationList(state, list) {
      state.unitConfigurationList = list;
    },
    setAgency(state, agencies) {
      state.agencies = agencies;
    },
    setAgent(state, agents) {
      state.agents = agents;
    },
    setSeason(state, seasons) {
      state.seasons = seasons;
    },
    setResortBedrooms(state, resortBedrooms) {
      state.resortBedrooms = resortBedrooms;
    },
    setMaxSleep(state, maxSleep) {
      state.maxSleep = maxSleep;
    },
    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('setMaxSleep', MyData.maxBedrooms);
      commit('setBankedEntities', MyData.bankedEntities);
      dispatch('getSeasons');
      dispatch('getAgents');
      dispatch('getAgencies');
      dispatch('getResorts');
      dispatch('getRegions');
    },
    getSeasons({
      commit,
    }) {
      axios
        .get('/api/season')
        .then(result => commit('setSeason', result.data))
        .catch(console.error);
    },
    getAgencies({
      commit,
    }) {
      console.log(123);
      axios
        .get('/api/agency')
        .then(result => commit('setAgency', result.data))
        .catch(console.error);
    },
    getAgents({
      commit,
    }) {
      axios
        .get('/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);
          }
        }
      }
    },
  },
};
