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

export default {
  namespaced: true,
  state: {
    weeks: [],
    filter: {
      region: undefined,
      resort: undefined,
      bedrooms: undefined,
      date: undefined,
      minPrice: undefined,
      maxPrice: undefined,
    },
  },
  mutations: {
    onClearFilter(state, filter) {
      state.filter[filter] = undefined;
    },
    setWeeks(state, weeks) {
      state.weeks = weeks;
    },
    addWeek(state, week) {
      state.weeks.push(week);
    },
  },
  getters: {
    filteredWeeks: (state) => {
      let weekList = state.weeks;
      const {
        filter,
      } = state;
      // console.log(JSON.stringify(weekList));
      if (filter) {
        if (filter.region) {
          weekList = _.filter(weekList, x => x.region && x.region.regionCode === filter.region
            .regionCode);
        }
        if (filter.resort) {
          weekList = _.filter(weekList, x => x.resort.resortCode
            && x.resort.resortCode === filter.resort.resortCode);
        }
        if (filter.bedrooms) {
          weekList = _.filter(weekList, x => x.bedrooms
            && x.bedrooms === filter.bedrooms);
        }
        if (filter.date) {
          const minDate = new Date(filter.date);
          minDate.setDate(minDate.getDate() - 7);
          const maxDate = new Date(filter.date);
          maxDate.setDate(maxDate.getDate() + 7);
          weekList = _.filter(weekList, x => new Date(x.arrivalDate)
            && new Date(x.departureDate)
            && new Date(x.arrivalDate) >= minDate
            && new Date(x.arrivalDate) <= maxDate);
        }
        if (filter.minPrice && filter.minPrice !== 0) {
          weekList = _.filter(weekList, x => x.sellingPrice
            && x.sellingPrice >= filter.minPrice);
        }
        if (filter.maxPrice && filter.maxPrice !== 0) {
          weekList = _.filter(weekList, x => x.sellingPrice
            && x.sellingPrice <= filter.maxPrice);
        }
      }

      return weekList;
    },
  },
  actions: {
    getWeeks({
      commit,
    }) {
      axios
        .get('/api/timeshareweek')
        .then(result => commit('setWeeks', result.data))
        .catch(console.error);
    },
    clearFilter({
      commit,
    }, filter) {
      commit('onClearFilter', filter);
    },
    addDummyWeek({
      commit,
    }) {
      commit('addWeek', {
        id: 1,
        unit: '359',
        week: 'N18',
        module: '359/N18 River View',
        sellingPrice: 85000,
        currentLevy: 5455,
        arrivalDate: '2019/09/09',
        departureDate: '2019/09/16',
        region: {
          regionCode: 'EC',
          regionName: 'Eastern Cape',
          id: 1,
        },
        resort: {
          resortName: 'Ekuthuleni Hoedspruit',
          resortCode: 'EKU',
          town: 'Hoedspruit',
          city: 'Hoedspruit',
          id: 17,
        },
        bedrooms: '2',
        unitNumber: '18',
        weekNumber: '25',
      });
    },
  },
};
