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 {CdkDragHandle, DragDropModule} from '@angular/cdk/drag-drop';
import {MatTooltipModule} from '@angular/material/tooltip';

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

  ngOnInit() {
  }


  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().filter(item => item.demandRate || item.amount)
  }

  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 {
    const val =  (item.amount / (item.demandRate * this.planet.population * this.planet.demandSecondsBuffer)) * 100;
    if (isNaN(val)) {
      return 0;
    }
    return Math.min(Math.floor(val), 100);
  }


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

  get goodsInTransit(): { type: GoodType, amount: number}[] {
    const res: { type: GoodType, amount: number}[] = [];
    this.planet.goodsInTransit.forEach(item => {
      const ex = res.find(r => r.type == item.type);
      if (ex) {
        ex.amount += item.amount;
      } else {
        res.push({
          type: item.type,
          amount: item.amount
        })
      }
    });
    return res;
  }

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


  buildHarbour() {
    this.planet.buildHarbour()
  }

  getOfferedAmount(item: Good): number {
    const offers = this.planet.offeredGoods;
    return offers.find(o => o.type == item.type)?.amount ?? 0;
  }

  upgradeProduction() {
    this.planet.upgradeProduction();
  }

  getProductionAmount(item: Good): number {
    if (!item.productionRate) { return 0; }

    return this.planet.getProductionLvl(item) * item.productionRate;
  }

  get canUpgradeProduction(): boolean {
    return this.gameService.money > this.planet.productionLvlUpgradeCost;
  }

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

  buildFactory() {
    this.planet.buildFactory();
  }
}

