import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { HelperService } from '@k-services/svc.helper'

import 'rxjs/add/operator/toPromise'

@Injectable()
export class UserProfileService {

    constructor(
        private _http: HttpClient,
        private _helper: HelperService
    ) { }


    getUserInfo() {

        let body = {
            key: this._helper.apiKey,
            websiteId: this._helper.websiteId,
            storeId: this._helper.storeId,
            getUserInfo: true
        }


        return this._http.post(this._helper.server + 'feed/get/customer', body).toPromise().then((response: any) => {

            console.log('[' + new Date() + ']', 'get user info', response)
            return response
        })
    }


    setUserInfo(user) {

        let body = {
            key: this._helper.apiKey,
            websiteId: this._helper.websiteId,
            storeId: this._helper.storeId,
            update: true
        }

        for(let detail of Object.keys(user)) {
            body[detail] = user[detail]
        }


        return this._http.post(this._helper.server + 'feed/set/customer', body).toPromise().then((response: any) => {
            return response
        })

    }


    getOrders() {
            let body = {
            key: this._helper.apiKey,
            websiteId: this._helper.websiteId,
            storeId: this._helper.storeId,
            getOrders: true
        }

        return this._http.post(this._helper.server + 'feed/get/customer', body).toPromise().then((response: any) => {
            return response
        })
    }
}