import {
  PATCH_STORY,
} from '../constants/ActionTypes';
import {
  fetchCraftedStories,
  fetchCraftedStoriesByIssue,
  toggleIssuesState,
  deletePullRequestsBranch,
  mergePullRequests,
} from '../craftperson/story';
import {
  getConfigAll,
  bulkSetConfig,
} from '../utils/configs';
import {
  updateAppGlobal,
} from './settingActionCreators';

export function patchStories(stories) {
  return {
    type: PATCH_STORY,
    payload: {
      stories: stories,
    },
  };
}

export function fetchStories() {
  return (dispatch) => {
    getConfigAll()
      .then((configs) => {
        dispatch(updateAppGlobal(configs));
        return fetchCraftedStories(configs);
      }).then((value) => {
        dispatch(patchStories(value));
      }).then(() => {
        // FIXME: avoid using new Date() directly
        return bulkSetConfig(new Map([
          ['storyUpdatedAt', new Date()],
          ['autopilotedAt', new Date()],
        ]));
      }).then(() => {
        return getConfigAll();
      }).then((configs) => {
        dispatch(updateAppGlobal(configs));
      }).catch((error) => {
        console.error(error);// eslint-disable-line no-console
      });
  };
}

export function reloadStory(identifier = {}) {
  return (dispatch) => {
    getConfigAll()
      .then((configs) => {
        dispatch(updateAppGlobal(configs));
        return fetchCraftedStoriesByIssue({
          configs: configs,
          identifiers: [identifier],
        });
      }).then((value) => {
        dispatch(patchStories(value));
      }).catch((error) => {
        console.error(error);// eslint-disable-line no-console
      });
  };
}

export function toggleStoryState(identifier = {}) {
  return (dispatch) => {
    getConfigAll()
      .then((configs) => {
        dispatch(updateAppGlobal(configs));
        return toggleIssuesState({
          configs: configs,
          identifiers: [identifier],
        });
      }).then((value) => {
        dispatch(patchStories(value));
      }).catch((error) => {
        console.error(error);// eslint-disable-line no-console
      });
  };
}

export function deleteStoryBranch(identifier = {}) {
  return (dispatch) => {
    getConfigAll()
      .then((configs) => {
        dispatch(updateAppGlobal(configs));
        return deletePullRequestsBranch({
          configs: configs,
          identifiers: [identifier],
        });
      }).then((value) => {
        dispatch(patchStories(value));
      }).catch((error) => {
        console.error(error);// eslint-disable-line no-console
      });
  };
}

export function mergeStoryPullRequest(identifier = {}) {
  return (dispatch) => {
    getConfigAll()
      .then((configs) => {
        dispatch(updateAppGlobal(configs));
        return mergePullRequests({
          configs: configs,
          identifiers: [identifier],
        });
      }).then((value) => {
        dispatch(patchStories(value));
      }).catch((error) => {
        console.error(error);// eslint-disable-line no-console
      });
  };
}

@@ -257,16 +257,42 @@
 Config,%0A
+  setConfig,%0A  getConfig,%0A
 %7D from '
@@ -1543,32 +1543,288 @@
 tories(value));%0A
+      %7D).then(() =%3E %7B%0A        return setConfig('storyUpdatedAt', new Date());%0A      %7D).then(() =%3E %7B%0A        return getConfig('storyUpdatedAt');%0A      %7D).then((configValue) =%3E %7B%0A        dispatch(updateAppGlobal(new Map(%5B%5B'storyUpdatedAt', configValue%5D%5D)));%0A
       %7D).catch((
@@ -2253,32 +2253,288 @@
 tories(value));%0A
+      %7D).then(() =%3E %7B%0A        return setConfig('storyUpdatedAt', new Date());%0A      %7D).then(() =%3E %7B%0A        return getConfig('storyUpdatedAt');%0A      %7D).then((configValue) =%3E %7B%0A        dispatch(updateAppGlobal(new Map(%5B%5B'storyUpdatedAt', configValue%5D%5D)));%0A
       %7D).catch((
@@ -2971,32 +2971,288 @@
 tories(value));%0A
+      %7D).then(() =%3E %7B%0A        return setConfig('storyUpdatedAt', new Date());%0A      %7D).then(() =%3E %7B%0A        return getConfig('storyUpdatedAt');%0A      %7D).then((configValue) =%3E %7B%0A        dispatch(updateAppGlobal(new Map(%5B%5B'storyUpdatedAt', configValue%5D%5D)));%0A
       %7D).catch((
@@ -3647,32 +3647,32 @@
 en((value) =%3E %7B%0A
-
         dispatch
@@ -3686,32 +3686,288 @@
 tories(value));%0A
+      %7D).then(() =%3E %7B%0A        return setConfig('storyUpdatedAt', new Date());%0A      %7D).then(() =%3E %7B%0A        return getConfig('storyUpdatedAt');%0A      %7D).then((configValue) =%3E %7B%0A        dispatch(updateAppGlobal(new Map(%5B%5B'storyUpdatedAt', configValue%5D%5D)));%0A
       %7D).catch((

update story updated at