const axios = require("axios");
const DJANGO_API_BASE = process.env.VUE_APP_DJANGO_API_BASE;

const feature = {
  namespaced: true,
  state: {
    attachmentFormset: [],
    linkedFormset: [],
    features: [],
    form: null,
    extra_form: [
      {
        field_type: "char",
        name: "deplacement_frontiere",
        label: "Déplacement frontière"
      },
      {
        field_type: "list",
        name: "deplacement_status",
        label: "Statut du déplacement",
        choices: [
          "à confirmer",
          "confirmé"
        ]
      },
      {
        field_type: "integer",
        name: "deplacement_value",
        label: "Mesure du déplacement"
      },
      {
        field_type: "boolean",
        name: "is_deplacement",
        label: "Déplacement confirmé"
      },
      {
        field_type: "date",
        name: "deplacement_date",
        label: "Date de déplacement"
      },
      {
        field_type: "decimal",
        name: "deplacement_decimal",
        label: "Importance de déplacement"
      },
      {
        field_type: "text",
        name: "deplacement_description",
        label: "Déscription du déplacement"
      },
    ]
  },
  mutations: {
    SET_FEATURES(state, features) {
      state.features = features;
    },
    UPDATE_FORM(state, payload) {
      state.form = payload;
    },
    UPDATE_EXTRA_FORM(state, payload) {
      state.extra_form = payload;
    },
    ADD_ATTACHMENT_FORM(state, dataKey) {
      state.attachmentFormset = [...state.attachmentFormset, { dataKey }];
    },
    UPDATE_ATTACHMENT_FORM(state, payload) {
      const index = state.attachmentFormset.findIndex((el) => el.dataKey === payload.dataKey);
      if (index !== -1) state.attachmentFormset[index] = payload
    },
    REMOVE_ATTACHMENT_FORM(state, payload) {
      state.attachmentFormset = state.attachmentFormset.filter(form => form.dataKey !== payload);
    },
    ADD_LINKED_FORM(state, dataKey) {
      state.linkedFormset = [...state.linkedFormset, { dataKey }];
    },
    UPDATE_LINKED_FORM(state, payload) {
      const index = state.linkedFormset.findIndex((el) => el.dataKey === payload.dataKey);
      if (index !== -1) state.linkedFormset[index] = payload
    },
    REMOVE_LINKED_FORM(state, payload) {
      state.linkedFormset = state.linkedFormset.filter(form => form.dataKey !== payload);
    },
  },
  getters: {

  },
  actions: {
    GET_PROJECT_FEATURES({ commit, dispatch }, project_slug) {
      axios
        .get(`${DJANGO_API_BASE}projet/${project_slug}/feature`)
        .then((response) => {
          const features = response.data.features;
          commit("SET_FEATURES", features);
          dispatch("map/ADD_FEATURES", features.map(el => {
            const geometry = el.geom;
            const properties = { ...el }
            return { geometry, properties }
          }), { root: true });
        })
        .catch((error) => {
          throw error;
        });
    },
    /* UPDATE_FEATURES({dispatch, commit}, features) {
      commit("SET_FEATURES", features);
      dispatch ("map/ADD_FEATURES");
    }, */

    POST_FEATURE({ state }) {
      const data = {
        form: state.form,
        attachmentFormset: state.attachmentFormset,
        linkedFormset: state.linkedFormset,
        extra_form: state.extra_form,
      }
      console.log("data", data)

      /* axios
        .post(`${DJANGO_API_BASE}feature_type/`, data)
        .then((response) => {
          const routerHistory = router.options.routerHistory 
          commit("SET_USER", response.data.user);
          router.push(routerHistory[routerHistory.length - 1] || "/")
          dispatch("GET_USER_LEVEL_PROJECTS");
        })
        .catch(() => {
          commit("SET_USER", false)
        }); */
    },
    DELETE_FEATURE({ state }, feature_slug) {
      console.log("Deleting feature:", feature_slug, state)

      /* axios
        .post(`${DJANGO_API_BASE}feature_type/`, data)
        .then((response) => {
          const routerHistory = router.options.routerHistory 
          commit("SET_USER", response.data.user);
          router.push(routerHistory[routerHistory.length - 1] || "/")
          dispatch("GET_USER_LEVEL_PROJECTS");
        })
        .catch(() => {
          commit("SET_USER", false)
        }); */
    },
    POST_COMMENT({ state }, data) {
      console.log("post comment", data, state)

      /* axios
        .post(`${DJANGO_API_BASE}feature_type/`, data)
        .then((response) => {
          const routerHistory = router.options.routerHistory 
          commit("SET_USER", response.data.user);
          router.push(routerHistory[routerHistory.length - 1] || "/")
          dispatch("GET_USER_LEVEL_PROJECTS");
        })
        .catch(() => {
          commit("SET_USER", false)
        }); */
    },
    EXPORT_FEATURES({ state }) {
      console.log("Export features", state.features)
    }
  },

}

export default feature