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

import 'rxjs/add/operator/toPromise';

// Helpers
import { HelperService } from './svc.helper';

@Injectable()
export class MediaService {

    constructor(
        private http: Http,
        private helper: HelperService
    ) { }

    api = this.helper.server+'feed/get/block';
    key = this.helper.getCustomCredentials(10);

    mediaPagesPath = this.api+this.key;
    specificMediaPagesPath = this.mediaPagesPath+'&block=';
    listMediaPages = this.api+this.key+'&flatList';


    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }
    
    getMediaPagesList(): Promise<any> {

        return this.http.get(this.mediaPagesPath)
            .toPromise()
            .then((response) => response.json().data)
            .catch(this.handleError)
    }

    getSpecificMediaPage(media: string): Promise<any> {

        return this.http.get(this.specificMediaPagesPath+media)
            .toPromise()
            .then((response) => response.json().data)
            .catch(this.handleError)
    }
}