import fetch from 'cross-fetch'
var WooCommerceAPI = require('woocommerce-api');
var Base64 = require('base-64');


export const SEND_ORDER = 'SEND_ORDER'

var WooCommerce = new WooCommerceAPI({
  url: 'https://localhost',
  consumerKey: 'ck_79837fb6f49f9f3eca830e34faeeea35b6cc1287',
  consumerSecret: 'cs_18bd03a0e6afd7d5c9e51e1d1c86032e425bd8d8',
  wpAPI: true,
  version: 'wc/v2',
});

export function sendOrder() {

  var data = {
    payment_method: 'bacs',
    payment_method_title: 'Direct Bank Transfer',
    set_paid: true,
    billing: {
      first_name: 'John',
      last_name: 'Doe',
      address_1: '969 Market',
      address_2: '',
      city: 'San Francisco',
      state: 'CA',
      postcode: '94103',
      country: 'US',
      email: 'john.doe@example.com',
      phone: '(555) 555-5555'
    },
    shipping: {
      first_name: 'John',
      last_name: 'Doe',
      address_1: '969 Market',
      address_2: '',
      city: 'San Francisco',
      state: 'CA',
      postcode: '94103',
      country: 'US'
    },
    line_items: [
      {
        product_id: 93,
        quantity: 2
      },
      {
        product_id: 22,
        variation_id: 23,
        quantity: 1
      }
    ]
  };

  WooCommerce.post('orders', data, function (err, data, res) {
    console.log(res);
  });
}

export function logIn(username, password) {

  let auth = Base64.encode(username + ':' + password)
  let fullPath = 'https://localhost/wp-json/wiaas/v1/user'
  return fetch(fullPath, {
    method: 'get',
    headers: {
      'authorization': 'Basic ' + auth
    }
  }).then(response => {
      switch (response.status) {
        case 401:
          console.log(response.code)
          break
          case 200:
          return response.json()
        default:
          console.log(response.code)
      }
    })
}
