import { EventEmitter, Injectable } from "@angular/core";
import { Planet } from "../model/planet.model";
import { Ship, ShipConfig } from "../model/ships/ship.model";

@Injectable({
  providedIn: 'root',
})
export class GameService {
  public showPlanetInfo: Planet | undefined;
  public showShipInfo: Ship | undefined;
  public showBuyShip = false;

  public ships: Ship[] = [];
  public planets: Planet[] = [];

  showTutorial = true;

  public money = 100000;

  onShipCreate: EventEmitter<Ship> = new EventEmitter();
  onShipDestroy: EventEmitter<Ship> = new EventEmitter();

  constructor() {}

  showDialog(planet: Planet) {
    this.showPlanetInfo = planet;
  }

  showShip(ship: Ship) {
    this.showShipInfo = ship;
  }

  get canDrag(): boolean {
    return this.showShipInfo == undefined && this.showPlanetInfo == undefined;
  }


  createShip(config: ShipConfig) {
    if (this.money < config.buyCost) {
      return;
    }
    this.money -= config.buyCost;
    const ship = new Ship(this, config);
    this.ships.push(ship);
    this.onShipCreate.emit(ship);
  }

  sellShip(ship: Ship) {
    this.ships = this.ships.filter(s => s != ship);
    ship.sell();
    this.onShipDestroy.emit(ship);
    this.money += ship.buyCost;
  }
}