import api from '../api';
import * as types from './mutation-types';

export const setSchool = ({ dispatch }, school) => {
  dispatch(types.SET_SCHOOL, school);
};

export const setUserTimetable = ({ dispatch }, school, year, sem) => {
  // first get the list of all modules
  api.getModulesList(school, year, sem).then(response => {
    dispatch(types.RETRIEVE_MODULES_LIST, response);
    return api.getUserModules(school, year, sem);
  })
  // then get the previously saved modules, if any
  .then(response => {
    dispatch(types.ATTACH_USER_MODULES, response);
  })
  .catch(() => {
    dispatch(types.RETRIEVE_ALL_ERROR);
  });
};

/*
Gets the timetable from local forage, by default meaning if the user
changes the school or semester, this is the last saved timetable.
For a new user, this becomes NTU, 2016, sem 1 timetable.
*/
export const setDefaultTimetable = ({ dispatch }) => {
  const previous = api.getDefault();
  const modulesList = previous.then((response) => {
    if (response) {
      return api.getModulesList(response.school, response.year, response.sem);
    }
    return api.getModulesList('NTU', 2016, 1);
  });
  const userModules = previous.then((response) => {
    if (response) {
      return api.getUserModules(response.school, response.year, response.sem);
    }
    return api.getUserModules('NTU', 2016, 1);
  });
  Promise.all([previous, modulesList, userModules]).then(values => {
    const previousSettings = values[0];
    // by default its 'NTU', for now
    const school = previousSettings ? previousSettings.school : 'NTU';
    // set the school as before
    dispatch(types.SET_SCHOOL, school);
    // set the modules list as before
    dispatch(types.RETRIEVE_MODULES_LIST, values[1]);
    // set the modules
    const modules = values[2];
    dispatch(types.ATTACH_USER_MODULES, modules);
    /* TODO: decide if modules should auto update and how to handle auto update
    if (modules) {
      modules.forEach((module) => {
        api.getModule(school, values[0].year, values[0].sem, module.code).then(response => {
          dispatch(types.ADD_MODULE, response);
        })
      })
    }
    */
  })
  .catch(() => {
    dispatch(types.RETRIEVE_ALL_ERROR);
  });
};

export const addModule = ({ dispatch }, school, year, sem, moduleCode) => {
  api.getModule(school, year, sem, moduleCode).then(response => {
    dispatch(types.ADD_MODULE, response);
  }).catch(() => {
    dispatch(types.ADD_ERROR);
  });
};

function makeAction(type) {
  return ({ dispatch }, arg) => dispatch(type, arg);
}

export const deleteModule = makeAction(types.DELETE_MODULE);
export const toggleSearchStatus = makeAction(types.TOGGLE_SEARCH_STATUS);

export const changeModuleColor = ({ dispatch }, module, colorHex) => {
  dispatch(types.CHANGE_MODULE_COLOR, module, colorHex);
};

export const onClickLesson = makeAction(types.ON_CLICK_LESSON);
export const onClickOutside = ({ dispatch }) => {
  dispatch(types.ON_CLICK_OUTSIDE);
};

@@ -309,32 +309,33 @@
 , sem).then(
+(
 response
  =%3E %7B%0A    di
@@ -314,32 +314,33 @@
 ).then((response
+)
  =%3E %7B%0A    dispat
@@ -388,60 +388,73 @@
 ;%0A  
-  return api.getUserModules(school, year, sem
+%7D)%0A  .catch(() =%3E %7B%0A    dispatch(types.RETRIEVE_ALL_ERROR
 );%0A  %7D)
+;
 %0A  /
@@ -503,30 +503,68 @@
 f any%0A  
+api.getUserModules(school, year, sem)
 .then(
+(
 response
  =%3E %7B%0A  
@@ -547,32 +547,33 @@
 ).then((response
+)
  =%3E %7B%0A    dispat
@@ -944,321 +944,100 @@
 %7B%0A  
-const previous = api.getDefault();%0A  const modulesList = previous.then((response) =%3E %7B%0A    if (response) %7B%0A      return api.getModulesList(response.school, response.year, response.sem);%0A    %7D%0A    return api.getModulesList('NTU', 2016, 1);%0A  %7D);%0A  const userModules = previous.then((response) =%3E %7B%0A    if (response
+api.getDefault().then((settings) =%3E %7B%0A    // by default its 'NTU', for now%0A    if (!settings
 ) %7B%0A
@@ -1046,388 +1046,94 @@
     
-return api.getUserModules(response.school, response.year, response.sem);
+settings = %7B
 %0A    
-%7D%0A
     
-return api.getUserModules('NTU', 2016, 1);%0A  %7D);%0A  Promise.all(%5Bprevious, modulesList, userModules%5D).then(values =%3E %7B%0A    const previousSettings = values%5B0%5D;%0A    // by default its 'NTU', for now%0A    const school = previousSettings ? previousSettings.school : 'NTU';%0A    // set the school as before
+school: 'NTU',%0A        year: 2016,%0A        sem: 1,%0A      %7D;%0A    %7D
 %0A   
@@ -1152,32 +1152,41 @@
 pes.SET_SCHOOL, 
+settings.
 school);%0A    // 
@@ -1182,154 +1182,104 @@
 l);%0A
+%0A
     
-// set the m
+api.getM
 odules
- list as before%0A    dispatch(types.RETRIEVE_MODULES_LIST, values%5B1%5D);%0A    // set the modules%0A    const modules = values%5B2%5D;%0A
+List(settings.school, settings.year, settings.sem).then((modulesList) =%3E %7B%0A  
     
@@ -1293,35 +1293,32 @@
 h(types.
-ATTACH_USER
+RETRIEVE
 _MODULES
 , module
@@ -1309,16 +1309,21 @@
 _MODULES
+_LIST
 , module
@@ -1327,150 +1327,23 @@
 ules
-);%0A    /* TODO: decide if modules should auto update and how to handle auto update%0A    if (modules) %7B%0A      modules.forEach((module) =%3E %7B%0A    
+List);%0A    %7D);%0A
     
@@ -1353,90 +1353,90 @@
 .get
+User
 Module
+s
 (s
-chool, values%5B0%5D.year, values%5B0%5D.sem, module.code).then(response
+ettings.school, settings.year, settings.sem).then((userModules)
  =%3E %7B%0A
-    
     
@@ -1457,62 +1457,49 @@
 es.A
-DD
+TTACH_USER
 _MODULE
+S
 , 
-response);%0A   
+userModules);%0A
     
- 
 %7D)
-%0A      %7D)%0A    %7D%0A    */
+;
 %0A  %7D

Remove coupling of promises