import { Injectable }           from '@angular/core';
import { Headers, Http }        from '@angular/http';

import { HelperService }        from './svc.helper';
import { SharedService }        from './svc.shared';

import 'rxjs/add/operator/toPromise';



@Injectable()
export class PromotionService {

    constructor(
        private _http: Http, 
        private _helper: HelperService,
        private _shared: SharedService
    ) { }

    private key = '?key=' + this._helper.apiKey + '&storeId=50' + this._helper.websiteId + '&websiteId=' + this._helper.websiteId;
    private api = this._helper.server+'feed/get';
    private promotionApi = this.api + '/block' + this.key + '&stripContent';
    
    
    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only TODO: create better error handling
        return Promise.reject(error.message || error);
    }


    getPromotions(): Promise<any> {
        return this._http.get(this.promotionApi)
            .toPromise()
            .then(response => {
                return response.json().data
            })
            .catch(this.handleError);
    }
}