/* eslint-disable valid-typeof */
/* eslint-disable max-len */
/* eslint-disable indent */
import axios from "axios";
import _ from "lodash";
import log from "../../../assets/Log";

export default {
  namespaced: true,
  state: {
    propertySearch: {
      userName: "",
      salesType: "Sale",
      propertyUsageType: "All",
      propertyType: "All",
      province: "All",
      city: "All",
      suburb: "All",
      minPrice: 0,
      maxPrice: 0,
      availableFrom: undefined,
      propertyId: 0
    },
    properties: [],
    latestProperties: [],
    suburbs: [],
    searchText: "",
    suburbList: [],
    resultsShowing: false
  },
  mutations: {
    updateSearch(state, propertySearch) {
      state.properties = [];
      state.properties = propertySearch;
    },
    setLatestProperties(state, properties) {
      state.latestProperties = properties;
    },
    setPropertySearch(state, search) {
      state.propertySearch = search;
    },
    onClearFilter(state, filter) {
      if (filter === "availableFrom") {
        state.propertySearch[filter] = undefined;
      } else {
        state.propertySearch[filter] = "All";
      }
    },
    setSuburbs(state, items) {
      state.suburbList = [];
      state.suburbs = items;
      // eslint-disable-next-line no-plusplus
      for (let i = 0; i < state.suburbs.length; i++) {
        state.suburbList.push(state.suburbs[i].display);
      }
    },
    setFilter(state, value) {
      state.searchText = value;
    },
    setResultsShowing(state, value) {
      state.resultsShowing = value;
    }
  },
  getters: {
    filterSuburbs: state => {
      let subs = state.suburbs;
      if (state.searchText) {
        subs = _.filter(subs, s => s.display.contains(state.searchText));
      }
      return subs;
    }
  },
  actions: {
    clearFilter({ commit }, filter) {
      commit("onClearFilter", filter);
    },
    searchProperties({ commit }, item) {
      return new Promise((resolve, reject) => {
        if (item.keyword === "") {
          item.keyword = "All";
        }
        if (log.isLoggedIn()) {
          item.userName = log.getUser().username;
        } else if (item.userName === "") {
          item.userName = "Unknown";
        }
        if (item.suburb === "") {
          item.suburb = "All";
        }
        if (item.city === "") {
          item.city = "All";
        }
        if (item.province === "") {
          item.province = "All";
        }
        console.log(
          `/api/Property/Search/${item.userName}/${item.keyword}/${item.salesType}/${item.propertyUsageType}/${item.propertyType}/${item.province}/${item.city}/${item.suburb}/${item.minPrice}/${item.maxPrice}/${item.availableFrom}/${item.propertyId}`
        );

        axios
          .get(
            `/api/Property/Search/${item.userName}/${item.keyword}/${item.salesType}/${item.propertyUsageType}/${item.propertyType}/${item.province}/${item.city}/${item.suburb}/${item.minPrice}/${item.maxPrice}/${item.availableFrom}/${item.propertyId}`
          )
          .then(result => {
            commit("updateSearch", result.data);
            resolve();
          })
          .catch(error => reject(new Error(error.message)));
      });
    },
    searchLatestProperties({ commit }) {
      axios
        .get("/api/property/latestProperties")
        .then(response => commit("setLatestProperties", response.data))
        .catch(console.error);
    },
    searchLatestPropertiesType({ commit }, type) {
      axios
        .get(`/api/property/latestProperties/${type}`)
        .then(response => commit("setLatestProperties", response.data))
        .catch(console.error);
    },
    applyFilter({ commit }, value) {
      commit("setFilter", { value });
    },
    updateResultsShowing({ commit }, value) {
      commit("setResultsShowing", value);
    }
  }
};
