import axios from "axios";
import { Date } from "core-js";
import { result } from "lodash";
import { resolve } from "core-js/fn/promise";

export default {
  namespaced: true,
  state: {
    statuses: [],
    property: {},
    propertyImages: [],
    propertyTypes: [],
    propertyTypesRes: [],
    propertyTypesCom: [],
    propertyOverviewFields: [],
    propertyFields: []
  },
  mutations: {
    setProperty(state, property) {
      state.property = property;
    },
    setPropertyImages(state, images) {
      state.propertyImages = images;
      state.propertyImageList = [];
      for (let i = 0; i < images.length; i++) {
        state.propertyImageList.push(images[i].image);
      }
    },
    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) {
      if (state.properties) {
        state.properties.push(property);
      }
    },
    clearPropertyImages(state) {
      state.propertyImages = [];
    },
    setStatuses(state, stats) {
      state.statuses = stats;
    }
  },
  getters: {
    getFields: state => state.propertyFields
  },
  actions: {
    getStatuses({ commit }) {
      axios
        .get("/api/property/GetPropertyStatuses")
        .then(result => commit("setStatuses", result.data))
        .catch(ex => console.log(ex.response));
    },
    getProperty({ commit, dispatch }, id) {
      return new Promise((resolve, reject) => {
        axios
          .get(`/api/Property/getDetailed/${id}`)
          .then(result => {
            commit("setProperty", result.data);
            dispatch("getPropertyTypes", result.data.propertyUsageType);
            dispatch("getSavedPropertyImages", result.data.id);
            dispatch("getSavedPropertyFields", result.data.id);
            dispatch("getStatuses");
            resolve();
          })
          .catch(error => reject(new Error(error.message)));
      });
    },
    getDisplayProperty({ commit, dispatch }, id) {
      return new Promise((resolve, reject) => {
        axios
          .get(`/api/Property/getDetailed/${id}`)
          .then(result => {
            commit("setProperty", result.data);
            dispatch("getSavedPropertyImages", result.data.id);
            resolve();
          })
          .catch(error => reject(new Error(error.message)));
      });
    },
    getPropertyImages({ commit }, id) {
      axios
        .get(`/api/PropertyImage/GetImagesByProperty/${id}`)
        .then(result => commit("setPropertyImages", result.data))
        .catch(ex => console.log(ex.response));
    },
    getSavedPropertyImages({ commit }, id) {
      console.log(id);
      axios
        .get(`/api/PropertyImage/GetProperySavedImages/${id}`)
        .then(result => commit("setPropertyImages", result.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertyTypes({ commit }, propertyType) {
      axios
        .get(`/api/PropertyType/type/${propertyType}`)
        .then(result => commit("setPropertyTypes", result.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertyTypesRes({ commit }) {
      axios
        .get("/api/PropertyType/type/Residential")
        .then(result => commit("setPropertyTypesRes", result.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertyTypesCom({ commit }) {
      axios
        .get("/api/PropertyType/type/Commercial")
        .then(result => commit("setPropertyTypesCom", result.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertyOverviewFields({ commit }) {
      axios
        .get("/api/PropertyFields/Property Overview")
        .then(response => commit("setPropertyOverviewFields", response.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertyFields({ commit }, propertyType) {
      return axios.get(`/api/propertyFields/PropertyType/${propertyType}`).then(response => {
        commit("setPropertyFields", response.data);
      })
      .catch(ex => console.log(ex.response));
    },
    getSavedPropertyFields({ commit }, id) {
      axios
        .get(`/api/PropertyFields/GetSavedPropertyUserFields/${id}`)
        .then(result => commit("setPropertyFields", result.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertySavedOverviewFields({ commit }, id) {
      axios
        .get(`/api/PropertyFields/GetSavedValues/Residential/Property Overview/${id}`)
        .then(response => commit("setPropertyOverviewFields", response.data))
        .catch(ex => console.log(ex.response));
    },
    getPropertySavedFields({ commit }, item) {
      axios
        .get(`/api/PropertyFields/GetSavedValues/${item.type}/All/${item.id}`)
        .then(response => commit("setPropertyFields", response.data))
        .catch(ex => console.log(ex.response));
    },
    saveProperty({ commit }, item) {
      return new Promise((resolve, reject) => {
        axios
          .post("/api/Property", item)
          .then(resp => {
            commit("updateCurrentProperty", resp);
            resolve(resp);
          })
          .catch((ex) => {
            reject(ex.response);
          });
      });
    },
    clearProperty({ commit }) {
      axios
        .get("/api/Property/getDetailed/0")
        .then(result => {
          alert(JSON.stringify(result.data));
          commit("setProperty", result.data);
        })
        .catch(ex => console.log(ex.response));
    },
    clearPropertyImages({ commit }) {
      commit("clearPropertyImages");
    },
    getSavedPropertyData({ commit }, item) {
      commit("clearProperty");
      commit("clearPropertyImages");

      axios
        .get(`/api/Property/getProperty/${item.id}`)
        .then(result => commit("setProperty", result.data))
        .catch(ex => console.log(ex.response));

      axios
        .get(`/api/PropertyImage/GetProperySavedImages/${item.id}`)
        .then(result => commit("setPropertyImages", result.data))
        .catch(ex => console.log(ex.response));

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

      axios
        .get(`/api/PropertyFields/GetSavedValues/${item.type}/All/${item.id}`)
        .then(response => commit("setPropertyFields", response.data))
        .catch(ex => console.log(ex.response));
    },
    getSavedPropertyImages({ commit }, id) {
      axios
        .get(`/api/PropertyImage/GetProperySavedImages/${id}`)
        .then(result => commit("setPropertyImages", result.data))
        .catch(ex => console.log(ex.response));
    },
    updateProperty({ commit }, property) {
      return new Promise((resolve, reject) => {
        axios
          .put("/api/property", property)
          .then(response => {
            resolve();
          })
          .catch((ex) => {
            reject(ex.response);
          });
      });
    }
  }
};
