import axios from 'axios';

export default {
  namespaced: true,
  state: {
    property: {
      createdBy: '',
      propertyTypeId: 0,
      propertyName: '',
      unit: '',
      operationalCosts: 0.0,
      price: 0,
      pricePer: '',
      isSale: false,
      description: '',
      shortDescription: '',
      addressLine1: '',
      addressLine2: '',
      addressLine3: '',
      suburbId: 0,
      cityId: 0,
      provinceId: 0,
      published: false,
      propertyUserFields: [],
      propertyImages: [],
      id: 0,
    },
    propertyImages: [],
    propertyTypes: [],
    propertyTypesRes: [],
    propertyTypesCom: [],
    propertyOverviewFields: [],
    propertyFields: [],
    properties: [],
    latestProperties: [],
  },
  mutations: {
    setProperty(state, property) {
      state.property = property;
    },
    setPropertyImages(state, images) {
      state.propertyImages = images;
    },
    setPropertyTypes(state, types) {
      state.propertyTypes = types;
    },
    setPropertyTypesRes(state, types) {
      state.propertyTypesRes = types;
    },
    setPropertyTypesCom(state, types) {
      state.propertyTypesCom = types;
    },
    setPropertyOverviewFields(state, fields) {
      state.propertyOverviewFields = fields;
    },
    setPropertyFields(state, fields) {
      state.propertyFields = fields;
    },
    updateCurrentProperty(state, property) {
      state.property = property;
    },
    updateSearch(state, propertySearch) {
      state.properties = propertySearch;
    },
    setLatestProperties(state, properties) {
      state.latestProperties = properties;
    },
    clearProperty(state) {
      state.property = {
        createdBy: '',
        propertyTypeId: 0,
        propertyName: '',
        unit: '',
        operationalCosts: 0.0,
        price: 0,
        pricePer: '',
        isSale: false,
        description: '',
        shortDescription: '',
        addressLine1: '',
        addressLine2: '',
        addressLine3: '',
        suburbId: 0,
        cityId: 0,
        provinceId: 0,
        published: false,
        propertyUserFields: [],
        propertyImages: [],
        id: 0,
      };
    },
    clearPropertyImages(state) {
      state.propertyImages = [];
    },
    setProperties(state, properties) {
      state.properties = properties;
    },
  },
  getters: {},
  actions: {
    getProperty({ commit }, id) {
      axios
        .get(`/api/Property/${id}`)
        .then(result => commit('setProperty', result.data))
        .catch(console.error);
    },
    getPropertyImages({ commit }, id) {
      axios
        .get(`/api/PropertyImage/getPropertyImages/${id}`)
        .then(result => commit('setPropertyImages', result.data))
        .catch(console.error);
    },
    getPropertyTypes({ commit }, propertyType) {
      axios
        .get(`/api/PropertyType/type/${propertyType}`)
        .then(result => commit('setPropertyTypes', result.data))
        .catch(console.error);
    },
    getPropertyTypesRes({ commit }) {
      axios
        .get('/api/PropertyType/type/Residential')
        .then(result => commit('setPropertyTypesRes', result.data))
        .catch(console.error);
    },
    getPropertyTypesCom({ commit }) {
      axios
        .get('/api/PropertyType/type/Commercial')
        .then(result => commit('setPropertyTypesCom', result.data))
        .catch(console.error);
    },
    getPropertyOverviewFields({ commit }) {
      axios
        .get('/api/PropertyFields/Property Overview')
        .then(response => commit('setPropertyOverviewFields', response.data));
    },
    getPropertyFields({ commit }, propertyType) {
      axios
        .get(`/api/propertyFields/PropertyType/${propertyType}`)
        .then(response => commit('setPropertyFields', response.data));
    },
    getPropertySavedOverviewFields({ commit }, id) {
      axios
        .get(`/api/PropertyFields/GetSavedValues/Residential/Property Overview/${id}`)
        .then(response => commit('setPropertyOverviewFields', response.data))
        .catch(console.error);
    },
    getPropertySavedFields({ commit }, item) {
      axios
        .get(`/api/PropertyFields/GetSavedValues/${item.type}/All/${item.id}`)
        .then(response => commit('setPropertyFields', response.data))
        .catch(console.error);
    },
    saveProperty({ commit }, item) {
      axios
        .post('/api/Property', item)
        .then(result => commit('updateCurrentProperty', result.data))
        .catch(console.error);
    },
    searchPropertiesParams({ commit }, item) {
      axios
        .get(
          `/api/property/search/${item.type}/${item.propertyType}/${item.province}/${item.city}/${item.suburb}/${item.propType}`,
        )
        .then(response => commit('updateSearch', response.data))
        .catch(console.error);
    },
    searchPropertiesKeyword({ commit }, item) {
      axios
        .get(`/api/Property/Search/${item.keyword}`)
        .then(response => commit('updateSearch', response.data))
        .catch(console.error);
    },
    searchLatestProperties({ commit }) {
      axios
        .get('/api/property/latestProperties')
        .then(response => commit('setLatestProperties', response.data))
        .catch(console.error);
    },
    clearProperty({ commit }) {
      commit('clearProperty');
    },
    clearPropertyImages({ commit }) {
      commit('clearPropertyImages');
    },
    getSavedPropertyData({ commit }, item) {
      commit('clearProperty');
      commit('clearPropertyImages');

      axios
        .get(`/api/Property/${item.id}`)
        .then(result => commit('setProperty', result.data))
        .catch(console.error);

      axios
        .get(`/api/PropertyImage/getPropertyImages/${item.id}`)
        .then(result => commit('setPropertyImages', result.data))
        .catch(console.error);

      axios
        .get(`/api/PropertyFields/GetSavedValues/Residential/Property Overview/${item.id}`)
        .then(response => commit('setPropertyOverviewFields', response.data))
        .catch(console.error);

      axios
        .get(`/api/PropertyFields/GetSavedValues/${item.type}/All/${item.id}`)
        .then(response => commit('setPropertyFields', response.data))
        .catch(console.error);
    },
  },
};
