import { secureFetch } from '../../common/util/util'
import { fetchFeedVersion } from './feeds'

export function setErrorMessage (message) {
  return {
    type: 'SET_ERROR_MESSAGE',
    message
  }
}

export function clearStatusModal () {
  return {
    type: 'CLEAR_STATUS_MODAL'
  }
}

/*function watchingStatus (job) {
  return {
    type: 'WATCHING_STATUS',
    job
  }
}

export function watchStatus (job) {
  return function (dispatch, getState) {
    dispatch(watchingStatus(project))

    // Check every 3 seconds if status is still there.
    // If so, then dispatch a `WATCHING_STATUS`, otherwise stop
    // the status watch.
    const intervalId = setInterval(() => {
      const active = getState().status.popover.active;

      if (active) {
        return secureFetch(`/api/manager/secure/${job}/status`, getState())
          .then(response => response.json())
          .then(status =>
            dispatch(receiveProjects(projects))
          )
      } else {
        clearInterval(intervalId);
      }
    }, 1000);
  }
}*/

export function receiveJobs (jobs) {
  return {
    type: 'RECEIVE_JOBS',
    jobs
  }
}

export function checkJobStatus () {
  return function (dispatch, getState) {
    return secureFetch(`/api/manager/secure/status/jobs`, getState())
      .then(response => response.json())
      .then(jobs => {
        // check for any just-finished jobs
        const previousJobs = getState().status.jobMonitor.jobs
        previousJobs.filter(j1 => jobs.findIndex(j2 => j1.jobId === j2.jobId) === -1).map(job => {
          dispatch(handleFinishedJob(job))
        })

        // if all jobs have finished, stop the timer
        if (previousJobs.length > 0 && jobs.length === 0) {
          dispatch(stopJobMonitor())
        }

        dispatch(receiveJobs(jobs))
      })
  }
}

export function setJobMonitorTimer (timer) {
  return {
    type: 'SET_JOBMONITOR_TIMER',
    timer
  }
}

function stopCurrentTimer (state) {
  const timer = state.status.jobMonitor.timer
  if (timer) clearInterval(timer)
}

export function startJobMonitor () {
  return function (dispatch, getState) {
    stopCurrentTimer(getState())

    const timerFunction = () => {
      dispatch(checkJobStatus())
    }

    timerFunction() // make an initial call right now
    const timer = setInterval(timerFunction, 2000)

    dispatch(setJobMonitorTimer(timer))
  }
}

export function stopJobMonitor () {
  return function (dispatch, getState) {
    stopCurrentTimer(getState())
    dispatch(setJobMonitorTimer(null))
  }
}

export function setJobMonitorVisible (visible) {
  return {
    type: 'SET_JOBMONITOR_VISIBLE',
    visible
  }
}

export function handleFinishedJob (job) {
  return function (dispatch, getState) {
    switch (job.type) {
      case 'VALIDATE_FEED':
        console.log('** Val FV job finished', job);
        dispatch(fetchFeedVersion(job.feedVersionId))
        break
    }
  }
}

@@ -88,16 +88,80 @@
 ./feeds'
+%0Aimport %7B fetchSnapshots %7D from '../../editor/actions/snapshots'
 %0A%0Aexport
@@ -2859,51 +2859,98 @@
     
-console.log('** Val FV job finished', job);
+dispatch(fetchFeedVersion(job.feedVersionId))%0A        break%0A      case 'PROCESS_SNAPSHOT':
 %0A   
@@ -2964,35 +2964,33 @@
 spatch(fetch
-FeedVersion
+Snapshots
 (job.feedVer
@@ -2985,34 +2985,43 @@
 (job.feedVersion
-Id
+.feedSource
 ))%0A        break

Refresh snapshots after new snapshot processing is complete on server