import { secureFetch } from '../../common/actions'
import { fetchBaseGtfs } from './editor'
import { clearGtfsContent } from './active'
import { handleFetchError, startJobMonitor } from '../../manager/actions/status'
import { getConfigProperty } from '../../common/util/config'
import { downloadS3Key } from '../../manager/actions/versions'

// SNAPSHOT ACTIONS

export function requestingSnapshots () {
  return {
    type: 'REQUESTING_GTFSEDITOR_SNAPSHOTS'
  }
}

export function receiveSnapshots (feedSource, snapshots) {
  return {
    type: 'RECEIVE_GTFSEDITOR_SNAPSHOTS',
    feedSource,
    snapshots
  }
}

export function fetchSnapshots (feedSource) {
  return function (dispatch, getState) {
    dispatch(requestingSnapshots())
    const url = `/api/editor/secure/snapshot?feedId=${feedSource.id}`
    return dispatch(secureFetch(url, 'get'))
      .then((response) => {
        return response.json()
      }).then((snapshots) => {
        dispatch(receiveSnapshots(feedSource, snapshots))
      })
  }
}

export function restoringSnapshot () {
  return {
    type: 'RESTORING_SNAPSHOT'
  }
}

export function restoredSnapshot (name) {
  return {
    type: 'RESTORED_SNAPSHOT',
    name
  }
}

export function restoreSnapshot (feedSource, snapshot) {
  return function (dispatch, getState) {
    dispatch(restoringSnapshot())
    const url = `/api/editor/secure/snapshot/${snapshot.id}/restore`
    return dispatch(secureFetch(url, 'post'))
      .then((response) => {
        return response.json()
      }).then((stops) => {
        dispatch(restoredSnapshot(snapshot.name))
        dispatch(fetchSnapshots(feedSource))
        dispatch(clearGtfsContent())
        dispatch(fetchBaseGtfs(feedSource.id))
      })
  }
}

export function downloadingSnapshot (feedSource, snapshot) {
  return {
    type: 'DOWNLOADING_SNAPSHOT',
    feedSource,
    snapshot
  }
}

export function downloadedSnapshot (name) {
  return {
    type: 'DOWNLOADED_SNAPSHOT',
    name
  }
}

export function downloadSnapshotViaToken (feedSource, snapshot) {
  return function (dispatch, getState) {
    dispatch(downloadingSnapshot(feedSource, snapshot))
    const url = `/api/editor/secure/snapshot/${snapshot.id}/downloadtoken`
    dispatch(secureFetch(url))
    .then((response) => response.json())
    .then((credentials) => {
      if (getConfigProperty('application.data.use_s3_storage')) {
        dispatch(downloadS3Key(credentials, `${snapshot.feedId}_${snapshot.snapshotTime}.zip`, 'snapshots'))
      } else {
        // use token to download feed
        window.location.assign(`/api/manager/downloadsnapshot/${credentials.id}`)
      }
    })
  }
}

export function creatingSnapshot () {
  return {
    type: 'CREATING_SNAPSHOT'
  }
}

export function createdSnapshot (name) {
  return {
    type: 'CREATED_SNAPSHOT',
    name
  }
}

export function createSnapshot (feedSource, name, comment) {
  return function (dispatch, getState) {
    dispatch(creatingSnapshot())
    const url = `/api/editor/secure/snapshot`
    const payload = {
      feedId: feedSource.id,
      name,
      comment
    }
    return dispatch(secureFetch(url, 'post', payload))
      .then((response) => {
        return response.json()
      }).then(() => {
        dispatch(createdSnapshot(name))
        return dispatch(fetchSnapshots(feedSource))
      })
  }
}

export function deletingSnapshot () {
  return {
    type: 'DELETING_SNAPSHOT'
  }
}

export function deleteSnapshot (feedSource, snapshot) {
  return function (dispatch, getState) {
    dispatch(deletingSnapshot())
    const url = `/api/editor/secure/snapshot/${snapshot.id}`
    return dispatch(secureFetch(url, 'delete'))
      .then((response) => {
        return response.json()
      }).then(() => {
        console.log('deleted!')
        dispatch(fetchSnapshots(feedSource))
      })
  }
}

export function loadingFeedVersionForEditing (feedVersion) {
  return {
    type: 'LOADING_FEEDVERSION_FOR_EDITING',
    feedVersion
  }
}

export function loadFeedVersionForEditing (feedVersion) {
  return function (dispatch, getState) {
    dispatch(loadingFeedVersionForEditing())
    const url = `/api/editor/secure/snapshot/import?feedVersionId=${feedVersion.id}`
    return dispatch(secureFetch(url, 'post'))
      .then((res) => {
        if (res.status >= 400) {
          return res.json()
        }
        dispatch(startJobMonitor())
        return res.json()
      }).then((json) => {
        if (json.code >= 400) {
          dispatch(handleFetchError(json.message))
        } else {
          dispatch(fetchSnapshots(feedVersion.feedSource))
        }
      })
  }
}

@@ -1397,18 +1397,42 @@
 /restore
+?feedId=$%7BfeedSource.id%7D
 %60%0A
-
     retu
@@ -2232,16 +2232,40 @@
 oadtoken
+?feedId=$%7BfeedSource.id%7D
 %60%0A    di
@@ -2624,22 +2624,21 @@
 n(%60/api/
-manage
+edito
 r/downlo
@@ -2665,16 +2665,40 @@
 ials.id%7D
+?feedId=$%7BfeedSource.id%7D
 %60)%0A     
@@ -3077,16 +3077,40 @@
 snapshot
+?feedId=$%7BfeedSource.id%7D
 %60%0A    co
@@ -3701,24 +3701,48 @@
 snapshot.id%7D
+?feedId=$%7BfeedSource.id%7D
 %60%0A    return
@@ -4234,16 +4234,27 @@
 Editing(
+feedVersion
 ))%0A    c
@@ -4300,16 +4300,52 @@
 /import?
+feedId=$%7BfeedVersion.feedSource.id%7D&
 feedVers

add feedId to all action URLs