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

import 'rxjs/add/operator/toPromise';

import {Hero} from "./hero";

@Injectable()
export class HeroService {

  private heroesUrl = 'api/heroes';
  private headers = new Headers({'Content-Type': 'application/json'});

  constructor(
    private http: Http
  ) {}

  getHeroes(): Promise<Hero[]> {
    //   return Promise.resolve(HEROES);
    return this.http.get(this.heroesUrl)
                    .toPromise()
                    .then(response => response.json().data as Hero[])
                    .catch(this.handleError)
  }

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

  getHeroesSlowly(): Promise<Hero[]> {
    return new Promise(resolve => {
      // Simulate server latency with 2 second delay
      setTimeout(() => resolve(this.getHeroes()), 2000);
    });
  }

  getHero(id: number): Promise<Hero> {
    // return this.getHeroes()
    //   .then(heroes => heroes.find(hero => hero.id === id))
    const url = this.heroesUrl + '/' + id;

    return this.http.get(url)
                    .toPromise()
                    .then(response => response.json().data as Hero)
                    .catch(this.handleError);
  }

  update(hero: Hero): Promise<Hero> {
    const url = this.heroesUrl + '/' + hero.id;

    return this.http
      .put(url, JSON.stringify(hero), {headers: this.headers})
      .toPromise()
      .then(() => hero)
      .catch(this.handleError);
  }

  create(heroName: string): Promise<Hero> {
    const url = this.heroesUrl;

    return this.http
      .post(url, JSON.stringify({name: heroName}), {headers: this.headers})
      .toPromise()
      .then(res => res.json().data)
      .catch(this.handleError)
  }

  
(heroId: number): Promise<void> {
    const url = this.heroesUrl + '/' + heroId;

    return this.http
      .delete(url, {headers: this.headers})
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
}

delete