import Promise from 'bluebird'
import jQuery from 'jquery'

import {
  LOGIN_REQUEST,
  LOGIN_SUCCESS,
  LOGIN_FAILURE,

  LOGOUT_REQUEST,
  LOGOUT_SUCCESS,
  LOGOUT_FAILURE,

  CHECK_AUTH_REQUEST,
  CHECK_AUTH_SUCCESS,
  CHECK_AUTH_FAILURE
} from '../actionTypes'

function _login(email, password, cb) {
  const payload = {
    email: email,
    password: password
  }

  jQuery.post('api/auth/login', payload)
    .done((response) => {
      console.log(response)
      cb({
        authenticated: true,
        token: Math.random().toString(36).substring(7)
      })
    })
    .fail((response) => {
      console.log(response)
      cb({
        authenticated: false,
        message: response.responseJSON.message
      })
    })
}

export function login(email, password) {
  return {
    types: [LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE],
    promise: () => {
      return new Promise((resolve, reject) => {

        _login(email, password, (response) => {

          if (!!response.authenticated) {
            const token = response.token
            localStorage.onlineStoreSSID = token
            resolve({
              isLoggedIn: true,
              ssid: token
            })
          }

          else {
            reject({
              isLoggedIn: false,
              message: response.message
            })
          }
        })
      })
    }
  }
}

export function logout() {
  return {
    types: [LOGOUT_REQUEST, LOGOUT_SUCCESS, LOGOUT_FAILURE],
    promise: () => {
      return new Promise((resolve/*, reject*/) => {

        delete localStorage.onlineStoreSSID
        resolve({})
      })
    }
  }
}

export function checkAuth() {
  return {
    types: [CHECK_AUTH_REQUEST, CHECK_AUTH_SUCCESS, CHECK_AUTH_FAILURE],
    promise: () => {
      return new Promise((resolve, reject) => {

        if (!!localStorage.onlineStoreSSID) {
          resolve({
            ssid: localStorage.onlineStoreSSID
          })
        }

        else {
          reject({})
        }
      })
    }
  }
}

@@ -294,12 +294,16 @@
 word
+/*
 , cb
+*/
 ) %7B%0A
@@ -368,16 +368,63 @@
 rd%0A  %7D%0A%0A
+  return new Promise((resolve, reject) =%3E %7B%0A%0A  
   jQuery
@@ -460,16 +460,18 @@
 ad)%0A    
+  
 .done((r
@@ -468,29 +468,27 @@
   .done((res
-ponse
+ult
 ) =%3E %7B%0A     
@@ -492,41 +492,22 @@
     
-console.log(response)%0A      cb(%7B%0A
+  resolve(%7B%0A  
     
@@ -543,55 +543,31 @@
     
+  
 token: 
-Math.random().toString(36).substring(7)%0A
+result.ssid%0A  
     
@@ -571,27 +571,31 @@
      %7D)%0A    
+  
 %7D)%0A
+  
     .fail((r
@@ -593,24 +593,21 @@
  .fail((
-response
+error
 ) =%3E %7B%0A 
@@ -615,38 +615,16 @@
     
-console.log(response)%0A      cb
+  reject
 (%7B%0A 
@@ -622,32 +622,34 @@
 eject(%7B%0A        
+  
 authenticated: f
@@ -654,32 +654,34 @@
  false,%0A        
+  
 message: respons
@@ -673,24 +673,21 @@
 essage: 
-response
+error
 .respons
@@ -696,24 +696,35 @@
 SON.message%0A
+        %7D)%0A
       %7D)%0A   
@@ -720,18 +720,17 @@
      %7D)%0A
-  
+%0A
   %7D)%0A%7D%0A%0A
@@ -943,66 +943,37 @@
 word
-, (response) =%3E %7B%0A%0A          if (!!response.authenticated)
+)%0A          .then((result) =%3E
  %7B%0A 
@@ -1000,21 +1000,19 @@
 en = res
-ponse
+ult
 .token%0A 
@@ -1157,33 +1157,33 @@
   %7D)%0A          %7D
-%0A
+)
 %0A          else 
@@ -1169,36 +1169,49 @@
    %7D)%0A          
-else
+.catch((error) =%3E
  %7B%0A            r
@@ -1274,24 +1274,21 @@
 essage: 
-response
+error
 .message
@@ -1314,27 +1314,18 @@
        %7D
-%0A        %7D
 )
+%0A
 %0A      %7D

use Promise for async actions