import { createAction } from 'redux-actions'
import {
  start as startPomodoro,
  end as endPomodoro,
  newPomodoro
} from './shared/pomodoro'

// Constants
export const START = 'redux/modules/Timer/START'
export const STOP = 'redux/modules/Timer/STOP'
export const PAUSE = 'redux/modules/Timer/PAUSE'
export const ELAPSE = 'redux/modules/Timer/ELAPSE'
export const SET_TIME = 'redux/modules/Timer/SET_TIME'
export const CHANGE_TASK_TITLE = 'redux/modules/Timer/CHANGE_TASK_TITLE'

// inital
const initialState = {
  isTimerStart: false,
  pause: false,
  preveElapsedime: null,
  elapsedTime: 0,
  settingTime: 20 * 1000,
  taskTitle: ''
}

// reducer
const timerReducer = (state = initialState, action = {}) => {
  switch (action.type) {
    case START:
      return Object.assign({},
        state,
        {
          isTimerStart: true,
          pase: false,
          preveElapsedime: null
        }
      )
    case STOP:
      return Object.assign({},
      state,
        {
          isTimerStart: false,
          pause: false,
          startTime: null,
          elapsedTime: null
        }
      )
    case PAUSE:
      return Object.assign({},
        state,
        { pause: true, isTimerStart: false }
      )
    case ELAPSE:
      const baseTime = state.preveElapsedime ? state.preveElapsedime : action.payload.startTime

      return Object.assign({},
        state,
        {
          elapsedTime: state.elapsedTime + (action.payload.time - baseTime),
          preveElapsedime: action.payload.time
        }
      )
    case SET_TIME:
      return Object.assign({},
        state,
        {
          settingTime: action.payload
        }
      )
    case CHANGE_TASK_TITLE:
      return Object.assign({},
        state,
        {
          taskTitle: action.payload
        }
      )
    default:
      return state
  }
}
export default timerReducer

// Actions
export const startTimer = createAction(START, time => time)
export const stopTimer = createAction(STOP, time => time)
export const pauseTimer = createAction(PAUSE)
export const elapseTimer = createAction(ELAPSE, now => now)
export const setTime = createAction(SET_TIME, time => time)
export const changeTaskTitle = createAction(CHANGE_TASK_TITLE, title => title)

let timer = null
// middleware
export const timerMiddleware = ({ dispatch, getState }) => next => action => {
  const state = getState()

  if (action.type === START) {
    timer = setInterval(() => {
      const state = getState()

      if (state.timer.isTimerStart) {
        dispatch(elapseTimer(new Date().getTime()))
      }
    }, 10)

    dispatch(startPomodoro(action.payload))
  } else if (action.type === STOP) {
    clearInterval(timer)
    dispatch(endPomodoro(action.payload))
    // TODO 保存してから新規ポモドーロを作成する
    dispatch(newPomodoro())
  } else if (action.type === ELAPSE) {
    action.payload = {
      time: action.payload,
      startTime: state.pomodoro.startTime
    }
  }

  next(action)
}

export const timerElapseMiddleware = ({ dispatch, getState }) => next => action => {
  const state = getState()

  if (action.type === ELAPSE && state.timer.elapsedTime > state.timer.settingTime) {
    dispatch(stopTimer())
  } else {
    next(action)
  }
}

@@ -881,28 +881,38 @@
 Elapsedime: 
-null
+action.payload
 %0A        %7D%0A 
@@ -1251,105 +1251,8 @@
 SE:%0A
-      const baseTime = state.preveElapsedime ? state.preveElapsedime : action.payload.startTime%0A%0A
     
@@ -1350,39 +1350,71 @@
 e + 
-(action.payload.time - baseTime
+calcIncreaseTimeForElapse(state.preveElapsedime, action.payload
 ),%0A 
@@ -1453,21 +1453,16 @@
 .payload
-.time
 %0A       
@@ -2297,36 +2297,8 @@
 %3E %7B%0A
-  const state = getState()%0A%0A
   if
@@ -2712,146 +2712,8 @@
 %0A  %7D
- else if (action.type === ELAPSE) %7B%0A    action.payload = %7B%0A      time: action.payload,%0A      startTime: state.pomodoro.startTime%0A    %7D%0A  %7D
 %0A%0A  
@@ -2982,12 +2982,104 @@
 tion)%0A  %7D%0A%7D%0A
+%0Aconst calcIncreaseTimeForElapse = (baseTime, nextTime) =%3E %7B%0A  return nextTime - baseTime%0A%7D%0A

remove unnecessary logic for elapse