import { Component, inject, InjectionToken } from '@angular/core';
import { Planet, TradeInstance } from '../../../model/planet.model';
import { MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
import { CommonModule } from '@angular/common';
import { Good } from '../../../model/goods/good.interface';
import { GoodType } from '../../../model/goods/good-type.enum';
import { GameService } from '../../../service/game.service';
import {DragDropModule} from '@angular/cdk/drag-drop';

@Component({
  selector: 'app-planet-dialog',
  imports: [CommonModule, MatDialogModule, DragDropModule],
  templateUrl: './planet-dialog.component.html',
  styleUrl: './planet-dialog.component.scss'
})
export class PlanetDialogComponent {
  public gameService: GameService = inject(GameService)

  ngOnInit() {
    console.log(this.planet)
  }


  get planet(): Planet {
    return this.gameService.showPlanetInfo!;
  }

  get population(): number {
    return Math.floor(this.planet.population);
  }

  get producedItems(): Good[] {
    if (!this.planet) { return []; }
    return this.planet.getAllGoods().filter(g => g.productionRate)
  }

  get storedItems(): Good[] {
    return this.planet.getAllGoods()
  }

  get consumedItems(): Good[] {
    return this.planet.getAllGoods().filter(g => g.demandRate)
  }

  requestedItems(): {
      type: GoodType;
      amount: number;
    }[] {
      if (!this.planet) { return []; }
    return this.planet.requestedGoods
  }

  getFillPercentange(item: Good): number {
    return (item.amount / (item.demandRate * this.planet.population * 30)) * 100;
  }


  close() {
    this.gameService.showPlanetInfo = undefined;
  }

  get goodsInTransit(): TradeInstance[] {
    return this.planet.goodsInTransit;
  }

  upgradeHarbour() {
    this.planet.upgradeHarbour()
  }

}

