import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

// WARNING fixme todo: please do not use this anymore, use the local GraphQL cache
export default new Vuex.Store({
  modules: {},

  state: {
    specialContainerClass: '',
    showModal: '',
    showMobileNavigation: false,
    contentBlockPosition: {},
    scrollPosition: 0,
    currentContent: '',
    currentNoteBlock: '',
    noteType: '',
    currentRoomEntry: '',
    parentRoom: null,
    parentModule: '',
    currentObjectiveGroup: '',
    parentProject: null,
    currentNote: null,
    currentProjectEntry: null,
    currentNoteParent: '',
    imageUrl: '',
    infographic: {
      id: 0,
      type: ''
    },
    vimeoId: null,
    scrollToAssignmentId: '',
    scrollToAssignmentReady: false,
    scrollingToAssignment: false,
    editModule: false,
    modulePayload: [],
    modalResolve: () => {},
    modalReject: () => {},
  },

  getters: {
    showModal: state => state.showModal,
    showMobileNavigation: state => state.showMobileNavigation,
    scrollToAssignmentId: state => state.scrollToAssignmentId,
    scrollToAssignmentReady: state => state.scrollToAssignmentReady,
    scrollingToAssignment: state => state.scrollingToAssignment,
    currentProjectEntry: state => state.currentProjectEntry,
    currentObjectiveGroup: state => state.currentObjectiveGroup,
    currentContent: state => state.currentContent,
    currentNoteBlock: state => state.currentNoteBlock,
    currentNote: state => state.currentNote,
    currentNoteParent: state => state.currentNoteParent,
    noteType: state => state.noteType,
    modulePayload: state => state.modulePayload
  },

  // todo: clean up
  actions: {
    setSpecialContainerClass({commit}, payload) {
      commit('setSpecialContainerClass', payload);
    },
    confirmModal({dispatch, state}) {
      dispatch('hideModal');
      state.modalResolve();
    },
    cancelModal({dispatch, state}) {
      dispatch('hideModal');
      state.modalReject();
    },
    hideModal({commit, dispatch}) {
      document.body.classList.remove('no-scroll'); // won't get at the body any other way
      commit('setModal', false);
      dispatch('resetModalState');
    },
    resetModalState({commit}) {
      commit('setCurrentRoomEntry', '');
      commit('setCurrentContent', '');
      commit('setCurrentNoteBlock', '');
      commit('setCurrentNoteParent', '');
      commit('setContentBlockPosition', {});
      commit('setParentRoom', null);
      commit('setParentModule', '');
      commit('setCurrentObjectiveGroup', '');
      commit('setParentProject', null);
      commit('setCurrentProjectEntry', null);
      commit('setImageUrl', '');
      commit('setInfographic', {
        id: 0,
        type: ''
      });
      commit('setVimeoId', null);
      commit('setCurrentNote', null);
      commit('setNoteType', '');
      commit('setModulePayload', []);
    },
    resetContentBlockPosition({commit}) {
      commit('setContentBlockPosition', {});
    },
    resetCurrentNoteBlock({commit}) {
      commit('setCurrentNoteBlock', '');
    },
    editContentBlock({commit, dispatch}, payload) {
      commit('setCurrentNoteBlock', payload);
      dispatch('showModal', 'edit-content-block-wizard');
    },
    addContentBlock({commit, dispatch}, payload) {
      commit('setContentBlockPosition', payload);
      dispatch('showModal', 'new-content-block-wizard');
    },
    addObjective({commit, dispatch}, payload) {
      commit('setCurrentObjectiveGroup', payload);
      dispatch('showModal', 'new-objective-wizard');
    },
    addRoomEntry({commit, dispatch}, payload) {
      commit('setParentRoom', payload);
      dispatch('showModal', 'new-room-entry-wizard');
    },
    editRoomEntry({commit, dispatch}, payload) {
      commit('setCurrentRoomEntry', payload);
      dispatch('showModal', 'edit-room-entry-wizard');
    },
    showModal({commit}, payload) {
      document.body.classList.add('no-scroll'); // won't get at the body any other way

      commit('setModal', payload);
      return new Promise((resolve, reject) => {
        commit('setModalResolve', resolve);
        commit('setModalReject', reject);
      });
    },
    addProjectEntry({commit, dispatch}, payload) {
      commit('setParentProject', payload);
      dispatch('showModal', 'new-project-entry-wizard');
    },
    editProjectEntry({commit, dispatch}, payload) {
      commit('setCurrentProjectEntry', payload);
      dispatch('showModal', 'edit-project-entry-wizard');
    },
    addNote({commit, dispatch}, payload) {
      if (payload.block) {
        commit('setCurrentNoteBlock', payload.block);
        commit('setNoteType', payload.type);
        commit('setCurrentContent', payload.content);
      } else {
        commit('setCurrentNoteParent', payload.parent);
      }
      dispatch('showModal', 'new-note-wizard');
    },
    editNote({commit, dispatch}, payload) {
      commit('setCurrentNote', payload);
      dispatch('showModal', 'edit-note-wizard');
    },
    showFullscreenImage({commit, dispatch}, payload) {
      commit('setImageUrl', payload);
      dispatch('showModal', 'fullscreen-image');
    },
    showFullscreenInfographic({commit, dispatch}, payload) {
      commit('setInfographic', payload);
      dispatch('showModal', 'fullscreen-infographic');
    },
    showFullscreenVideo({commit, dispatch}, payload) {
      commit('setVimeoId', payload);
      dispatch('showModal', 'fullscreen-video');
    },
    showMobileNavigation({commit}, payload) {
      commit('setShowMobileNavigation', payload);
    },
    scrollToAssignmentId({commit}, payload) {
      commit('setScrollToAssignmentId', payload);
    },
    scrollToAssignmentReady({commit}, payload) {
      commit('setScrollToAssignmentReady', payload);
    },
    scrollingToAssignment({commit, state, dispatch}, payload) {
      if (payload && !state.scrollingToAssignment) {
        commit('setScrollingToAssignment', true);
      }

      if (!payload && state.scrollingToAssignment) {
        commit('setScrollingToAssignment', false);
        dispatch('scrollToAssignmentId', '');
      }
    },
    editModule({commit}, payload) {
      commit('setEditModule', payload);
    },
    editClassName({dispatch}) {
      dispatch('showModal', 'edit-class-name-wizard');
    },
    editTeamName({dispatch}) {
      dispatch('showModal', 'edit-team-name-wizard');
    },
    deactivateUser({commit, dispatch}, payload) {
      commit('setModulePayload', payload);
      return dispatch('showModal', 'deactivate-person');
    }
  },

  mutations: {
    setModal(state, payload) {
      state.showModal = payload;
    },
    setSpecialContainerClass(state, payload) {
      state.specialContainerClass = payload;
    },
    setScrollPosition(state, payload) {
      state.scrollPosition = payload;
    },
    setContentBlockPosition(state, payload) {
      state.contentBlockPosition = payload;
    },
    setCurrentContent(state, payload) {
      state.currentContent = payload;
    },
    setCurrentNoteBlock(state, payload) {
      state.currentNoteBlock = payload;
    },
    setParentRoom(state, payload) {
      state.parentRoom = payload;
    },
    setCurrentNote(state, payload) {
      state.currentNote = payload;
    },
    setCurrentRoomEntry(state, payload) {
      state.currentRoomEntry = payload;
    },
    setParentModule(state, payload) {
      state.parentModule = payload;
    },
    setCurrentObjectiveGroup(state, payload) {
      state.currentObjectiveGroup = payload;
    },
    setParentProject(state, payload) {
      state.parentProject = payload;
    },
    setCurrentProjectEntry(state, payload) {
      state.currentProjectEntry = payload;
    },
    setImageUrl(state, payload) {
      state.imageUrl = payload;
    },
    setInfographic(state, payload) {
      state.infographic = payload;
    },
    setVimeoId(state, payload) {
      state.vimeoId = payload;
    },
    setShowMobileNavigation(state, payload) {
      state.showMobileNavigation = payload;
    },
    setScrollToAssignmentId(state, payload) {
      state.scrollToAssignmentId = payload;
    },
    setScrollToAssignmentReady(state, payload) {
      state.scrollToAssignmentReady = payload;
    },
    setScrollingToAssignment(state, payload) {
      state.scrollingToAssignment = payload;
    },
    setEditModule(state, payload) {
      state.editModule = payload;
    },
    setCurrentNoteParent(state, payload) {
      state.currentNoteParent = payload;
    },
    setNoteType(state, payload) {
      state.noteType = payload;
    },
    setModulePayload(state, payload) {
      state.modulePayload = payload;
    },
    setModalResolve(state, payload) {
      state.modalResolve = payload;
    },
    setModalReject(state, payload) {
      state.modalReject = payload;
    }
  }
});
