import { Component, OnInit } from '@angular/core';
import { Hero } from '../models/hero';
import { HeroService } from '../hero.service';
import { MessageService } from '../message.service';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.scss']
})
export class HeroesComponent implements OnInit {

  heroes: Hero[];
  selectedHero: Hero;

  constructor(private heroService: HeroService, public messageService: MessageService) { }

  ngOnInit(): void {
    this.heroService.getHeroes().subscribe(
      (heroes: Hero[]) => {
        this.heroes = heroes;
      },
      (error: any) => {
        this.messageService.add("error นะ ไม่สามารถเอาข้อมูล Heroes มาได้");
      }
    );
    this.selectedHero = this.heroes[0];
  }

  onSelect(hero: Hero): void {
    this.selectedHero = hero;
    //console.warn(this.selectedHero);
    this.messageService.add(`ผู้ใช้เลือก Hero คนใหม่เป็น ${hero.name} id=${hero.id}`);
  }
}
