import { fetch } from '../util/api'

export const GOTO_STEP = 'GOTO_STEP'
export const NEXT_STEP = 'NEXT_STEP'
export const SKIP_TO_END = 'SKIP_TO_END'

export const SETTINGS_CHANGED = 'SETTINGS_CHANGED'

export const DECK_TOGGLED = 'DECK_TOGGLED'

export const QUIZ_ANSWERED = 'QUIZ_ANSWERED'
export const QUIZ_CHANGED = 'QUIZ_CHANGED'
export const QUIZ_CLOSED = 'QUIZ_CLOSED'

export const LOADING_INITIAL_DECKS = 'LOADING_INITIAL_DECKS'
export const LOADING_INITIAL_DECKS_SUCCEEDED = 'LOADING_INITIAL_DECKS_SUCCEEDED'
export const LOADING_INITIAL_DECKS_FAILED = 'LOADING_INITIAL_DECKS_FAILED'

export const INITIALIZING = 'INITIALIZING'
export const INITIALIZING_SUCCEEDED = 'INITIALIZING_SUCCEEDED'
export const INITIALIZING_FAILED = 'INITIALIZING_FAILED'

export const lastStep = 3
export const quizStep = 1

export const nextStep = () => {
  return {
    type: NEXT_STEP
  }
}

export const loadingDecks = () => ({
  type: LOADING_INITIAL_DECKS
})

export const initializing = () => ({
  type: INITIALIZING
})

export const initializingSucceeded = () => ({
  type: INITIALIZING_SUCCEEDED
})

export const initializingFailed = () => ({
  type: INITIALIZING_FAILED
})

export const loadingDecksSucceeded = (decks) => ({
  type: LOADING_INITIAL_DECKS_SUCCEEDED,
  decks
})
export const loadingDecksFailed = () => ({
  type: LOADING_INITIAL_DECKS_FAILED
})

export const loadDecks = () => (dispatch, getState) => {
  dispatch(loadingDecks())

  return fetch('/api/initialize')
    .then(data => data.json())
    .then(json => dispatch(loadingDecksSucceeded(json.available_decks)))
    .catch((ex) => {
      console.log('Got error', ex)
    })
}

export const finalize = () => {
  dispatch(initializing())
  return (dispatch, getState) => {
    const { onboard } = getState()
    let options = {
      include_ip: onboard.settings.includeIP,
      include_asn: onboard.settings.includeNetwork,
      include_country: onboard.settings.includeCountry,
      should_upload: onboard.settings.shareResults,
      preferred_backend: onboard.settings.uploadMethod,
      deck_config: {}
    }
    onboard.decks.forEach((deck) => {
      options['deck_config'][deck.id] = deck.enabled
    })
    return fetch('/api/initialize', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(options)
    }).then(_ => dispatch(initializingSucceeded()))
      .catch((ex) => {
        console.log('Got error while initializing', ex)
        dispatch(initializingFailed())
      })
  }
}

export const gotoStep = (stepNumber) => ({
  type: GOTO_STEP,
  payload: stepNumber
})

export const skipToEnd = () => ({
  type: SKIP_TO_END
})

export const settingsChanged = (key, value) => ({
  type: SETTINGS_CHANGED,
  key: key,
  value: value
})

export const deckToggled = (deckID) => ({
  type: DECK_TOGGLED,
  payload: deckID
})

export const quizAnswered = () => ({
  type: QUIZ_ANSWERED
})

export const quizClosed = () => ({
  type: QUIZ_CLOSED
})

export const quizChanged = (key, value) => ({
  type: QUIZ_CHANGED,
  key: key,
  value: value
})

@@ -1667,27 +1667,51 @@
 lize = () =%3E
+ (dispatch, getState) =%3E
  %7B%0A
-
   dispatch(i
@@ -1729,45 +1729,8 @@
 ())%0A
-  return (dispatch, getState) =%3E %7B%0A  
   co
@@ -1760,18 +1760,16 @@
 ate()%0A  
- 
- 
 let opti
@@ -1776,18 +1776,16 @@
 ons = %7B%0A
-  
     incl
@@ -1820,18 +1820,16 @@
 ludeIP,%0A
-  
     incl
@@ -1874,18 +1874,16 @@
 rk,%0A    
-  
 include_
@@ -1928,18 +1928,16 @@
 ry,%0A    
-  
 should_u
@@ -1974,18 +1974,16 @@
 esults,%0A
-  
     pref
@@ -2032,18 +2032,16 @@
 od,%0A    
-  
 deck_con
@@ -2052,18 +2052,14 @@
  %7B%7D%0A
-  
   %7D%0A  
- 
- 
 onbo
@@ -2084,26 +2084,24 @@
 (deck) =%3E %7B%0A
-  
     options%5B
@@ -2139,25 +2139,21 @@
 enabled%0A
-  
   %7D)%0A
-  
   return
@@ -2184,18 +2184,16 @@
 , %7B%0A    
-  
 method: 
@@ -2204,18 +2204,16 @@
 T',%0A    
- 
- 
 headers:
@@ -2215,18 +2215,16 @@
 ders: %7B%0A
-  
       'C
@@ -2264,19 +2264,15 @@
     
-  
 %7D,%0A    
- 
- 
 body
@@ -2297,18 +2297,16 @@
 ptions)%0A
-  
   %7D).the
@@ -2347,18 +2347,16 @@
 ded()))%0A
-  
     .cat
@@ -2370,26 +2370,24 @@
  =%3E %7B%0A      
-  
 console.log(
@@ -2428,18 +2428,16 @@
 )%0A      
-  
 dispatch
@@ -2467,16 +2467,10 @@
     
-  %7D)%0A  
 %7D
+)
 %0A%7D%0A%0A

Fix bug in onboarding