import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { ValueTupleOfStringAndIntegerAndIntegerAndNullableIntegerAndString } from '@generated/swagger/inventory-api';

@Component({
  selector: 'page-required-capacities',
  templateUrl: 'required-capacities.component.html',
  styleUrls: ['required-capacities.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class RequiredCapacitiesComponent {
  @Input()
  requiredCapacities: ValueTupleOfStringAndIntegerAndIntegerAndNullableIntegerAndString[];

  get leistungCapacity() {
    return this.requiredCapacities?.find((cap) => cap.item1 === 'Leistung') || undefined;
  }

  get leistung() {
    const capacity = this.leistungCapacity;
    if (!capacity) {
      return 0;
    }
    return capacity.item3 > capacity.item2 ? capacity.item2 : capacity.item3;
  }

  get maxLeistung() {
    const capacity = this.leistungCapacity;
    if (!capacity) {
      return 0;
    }

    const maxLeistung =
      (capacity.item4 || 0) < capacity.item2
        ? capacity.item4 || (capacity.item3 > capacity.item2 ? capacity.item2 : capacity.item3) || 0
        : capacity.item2;

    return Math.max(this.leistung, maxLeistung);
  }

  get leistungPercentage() {
    if (this.leistung === this.maxLeistung) {
      return 100;
    }

    if (this.maxLeistung > 0) {
      return Math.min(Math.max((this.leistung / this.maxLeistung) * 100, 0), 100);
    }
    return 0;
  }

  get stapelCapacity() {
    return this.requiredCapacities?.find((cap) => cap.item1 === 'Stapel') || undefined;
  }

  get stapel() {
    const capacity = this.stapelCapacity;
    if (!capacity) {
      return 0;
    }
    return capacity.item3 > capacity.item2 ? capacity.item2 : capacity.item3;
  }

  get maxStapel() {
    const capacity = this.stapelCapacity;
    if (!capacity) {
      return 0;
    }
    const maxStapel =
      (capacity.item4 || 0) < capacity.item2
        ? capacity.item4 || (capacity.item3 > capacity.item2 ? capacity.item2 : capacity.item3) || 0
        : capacity.item2;

    return Math.max(this.stapel, maxStapel);
  }

  get stapelPercentage() {
    if (this.stapel === this.maxStapel) {
      return 100;
    }

    if (this.maxStapel > 0) {
      return Math.min(Math.max((this.stapel / this.maxStapel) * 100, 0), 100);
    }
    return 0;
  }

  constructor() {}
}
