import { Component, OnInit } from '@angular/core';
import { FactionPoints } from './FactionPoints';
import { FactionPointsRemoteService } from './faction-points-remote.service';

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

  constructor(private factionPointsService: FactionPointsRemoteService) { }

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

  getFactionPoints(): void {
    this.factionPointsService.getPoints$()
      .subscribe(points => this.factionPoints = points);
  }

  updateFactionPoints(factionPoints: FactionPoints): void {
    this.factionPointsService.updatePoints$(factionPoints)
      .subscribe(result => {
        console.log(result);
        this.getFactionPoints();
      });
  }

  title = 'Galaxy Point';
  
  factionPoints: FactionPoints;

  changeKlingonPoints($event) {
    this.factionPointsService.getPoints$()
      .subscribe(points => {
        points.klingon += $event;
        this.updateFactionPoints(points);
      });
  }

  changeStarfleetPoints($event) {
    this.factionPointsService.getPoints$()
      .subscribe(points => {
        points.starfleet += $event;
        this.updateFactionPoints(points);
      });
  }

  changeRomulanPoints($event) {
    this.factionPointsService.getPoints$()
      .subscribe(points => {
        points.romulan += $event;
        this.updateFactionPoints(points);
      });
  }

  resetPoints() {
    this.updateFactionPoints({
      klingon:0,
      starfleet:0,
      romulan:0
    });
  }
}
