import { inject } from "@angular/core";
import { MapScene } from "../../scene/map.scene";
import { GameService } from "../../service/game.service";
import { Planet } from "../planet.model";
import { Ship } from "../ships/ship.model";
import { ShipUi } from "../ship";

export class SpaceshipLauncher {
  durationInSeconds = 10;
  x: number;
  y: number;

  private planet: Planet;
  private gameService: GameService;

  public buildProcess: number = 0;

  
  constructor(config: {planet: Planet, x: number, y: number, gameService: GameService}) {
    this.planet = config.planet;
    this.gameService = config.gameService;
    this.x = config.x;
    this.y = config.y;
  }


  async buildShip(scene: MapScene, ship: Ship) {
    await this.waitForBuilding();
    const ui = new ShipUi(scene, this.x, this.y, this.gameService, ship);
    ui.activateRoute();
    scene.physics.world.enable(ui);
    scene.ships.push(ui);
    this.buildProcess = 0;
  }

  private async waitForBuilding() {
    this.buildProcess = 0;
    return new Promise(async resolve=> {
      for (let i = 0; i < this.durationInSeconds * 100; i++) {
        this.buildProcess += 0.10;
        await this.timeout(10);
      }
      resolve(null)
    })
  }

  private async timeout(ms: number) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(null);
      }, ms)
    })
  }
}