import { Component, OnInit, ChangeDetectionStrategy, Input, Output, EventEmitter } from '@angular/core';
import { OrderDetailsCardInput } from './order-details-card-input';
import { ProcessingStatusNameMap } from '../../constants';
import { OrderItemProcessingStatusValue } from '@swagger/oms';
import { DateAdapter } from '@isa-ui/core/date';

@Component({
  selector: 'app-shelf-order-details-card',
  templateUrl: 'order-details-card.component.html',
  styleUrls: ['order-details-card.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ShelfOrderDetailsCardComponent implements OnInit {
  minDate = this.dateAdapter.addCalendarDays(new Date(), -1);

  get processingKeys() {
    const copy = new Map(ProcessingStatusNameMap);

    if (this.orderDetails) {
      if (this.orderDetails.processingStatus === 16 || this.orderDetails.processingStatus === 8192) {
        copy.delete(128);
      }
      if (this.orderDetails.processingStatus >= 0) {
        copy.delete(this.orderDetails.processingStatus);
      }
    }

    return copy;
  }

  get pickupDeadlines() {
    const dedalineMap = new Map<string, Date>();

    let TWO_WEEKS = 1000 * 60 * 60 * 24 * 14;
    let FOUR_WEEKS = 1000 * 60 * 60 * 24 * 28;

    if (this.orderDetails) {
      TWO_WEEKS += new Date(this.orderDetails.pickupDeadline).getTime();
      FOUR_WEEKS += new Date(this.orderDetails.pickupDeadline).getTime();
    } else {
      TWO_WEEKS += Date.now();
      FOUR_WEEKS += Date.now();
    }

    dedalineMap.set('um 2 Wochen verlängern', new Date(TWO_WEEKS));
    dedalineMap.set('um 4 Wochen verlängern', new Date(FOUR_WEEKS));

    return dedalineMap;
  }

  @Input()
  orderDetails: OrderDetailsCardInput;

  @Output()
  changeProcessingStatus = new EventEmitter<OrderItemProcessingStatusValue>();

  @Output()
  changeEstimatedDeliveryDate = new EventEmitter<Date>();

  @Output()
  changePickUpDeadline = new EventEmitter<Date>();

  constructor(private dateAdapter: DateAdapter<Date>) {}

  ngOnInit() {}
}
