import { Component, OnInit } from '@angular/core';
import { HousePoints } from './HousePoints';
import { HousePointsRemoteService } from './house-points-remote.service';

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

  constructor(private housePointsService: HousePointsRemoteService) { }

  ngOnInit() {
    this.getHousePoints();
    setInterval(()=>{this.getHousePoints();}, 10000);
  }

  getHousePoints(): void {
    this.housePointsService.getPoints$()
      .subscribe(points => this.housePoints = points);
  }

  updateHousePoints(housePoints: HousePoints): void {
    this.housePointsService.updatePoints$(housePoints)
      .subscribe(result => {
        console.log(result);
        this.getHousePoints();
      });
  }

  title = 'Hogwart\'s Point Keeper';
  
  housePoints: HousePoints;

  changeGryffindorPoints($event) {
    this.housePointsService.getPoints$()
      .subscribe(points => {
        points.gryffindor += $event;
        this.updateHousePoints(points);
      });
  }

  changeRavenclawPoints($event) {
    this.housePointsService.getPoints$()
      .subscribe(points => {
        points.ravenclaw += $event;
        this.updateHousePoints(points);
      });
  }

  changeHufflepuffPoints($event) {
    this.housePointsService.getPoints$()
      .subscribe(points => {
        points.hufflepuff += $event;
        this.updateHousePoints(points);
      });
  }

  changeSlytherinPoints($event) {
    this.housePointsService.getPoints$()
      .subscribe(points => {
        points.slytherin += $event;
        this.updateHousePoints(points);
      });
  }

  resetPoints() {
    this.updateHousePoints({
      gryffindor:0,
      ravenclaw:0,
      hufflepuff:0,
      slytherin:0
    });
  }
}
