/// <reference types="@types/long" />
import { Injectable }    from '@angular/core';
import {Headers, Http, RequestOptions, ResponseContentType} from '@angular/http';

import { meerkat } from "./bundle.js";
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

  @Injectable()
  export class BBService {
    private headers = new Headers({'Content-Type': 'application/x-protobuf'});
    private bbUrl = '/bbserver';  // URL to web api
    private statusPath = '/status';

    constructor(private http: Http) {  }

    getStatus(): Observable<meerkat.ServerStatus> {
      let options = new RequestOptions({
        responseType: ResponseContentType.ArrayBuffer
      });

      let res =  this.http.get(this.bbUrl + this.statusPath, options);

      return res
        .catch(this.handleError)
        .map(x => {
          return meerkat.ServerStatus.decode(new Uint8Array(x.arrayBuffer()));
        });
    }

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