import { Component, OnInit, Input } from '@angular/core';
import { CustomerOrderItem } from '../../../../core/models/customer-order-item';
import { Store } from '@ngxs/store';
import { Router } from '@angular/router';
import { ChangeCurrentRoute } from 'apps/sales/src/app/core/store/actions/process.actions';
import { Breadcrumb } from 'apps/sales/src/app/core/models/breadcrumb.model';
import { AddBreadcrumb } from 'apps/sales/src/app/core/store/actions/breadcrumb.actions';
import { SetEditOrder } from 'apps/sales/src/app/core/store/actions/collecting-shelf.action';
import { OrderDTO, ReceiptListItemDTO } from '@swagger/oms';
import { OrderPaymentReceipt } from 'apps/sales/src/app/core/models/order-payment-receipt.model';
import { isNullOrUndefined } from 'util';
import { receiptType, orderPaymentType } from 'apps/sales/src/app/core/mappings/shelf.mapping';
import { DatePipe } from '@angular/common';
import { ShelfNavigationService } from '../../../shelf/shared/services';

@Component({
  selector: 'app-customer-order-item-row',
  templateUrl: './customer-order-item-row.component.html',
  styleUrls: ['./customer-order-item-row.component.scss'],
})
export class CustomerOrderItemRowComponent implements OnInit {
  @Input() item: CustomerOrderItem;
  @Input() isDelivery = false;
  @Input() customerId: number;
  @Input() order: OrderDTO;
  @Input() isPayed = false;
  @Input() isLast = false;
  @Input() isOnlyOne: boolean;
  @Input() paymentDataDTO: ReceiptListItemDTO;
  @Input() disableEdit = false;
  @Input() estimatedDateLabel = 'Versand';

  isPreorder = false;
  shrinkedData = false;
  paymentData: OrderPaymentReceipt;

  get paymentType() {
    if (this.order.paymentType) {
      return orderPaymentType[this.order.paymentType];
    }
  }

  constructor(
    private store: Store,
    private router: Router,
    private datePipe: DatePipe,
    private shelfNavigationService: ShelfNavigationService
  ) {}

  ngOnInit() {
    this.loadPreorderedTag();
    this.initializePaymentData();
  }

  expand() {
    this.shrinkedData = !this.shrinkedData;
  }

  editOrder(id: number) {
    if (this.order) {
      this.store.dispatch(new SetEditOrder(this.order));
      let breadcrumbName = '';
      if (this.order.buyer) {
        breadcrumbName = this.order.buyer.firstName + ' ' + this.order.buyer.lastName + ' Bearbeiten';
      }

      const subItem = this.order.items.find((t) => t.id === id).data.subsetItems[0].data;
      const path = `/shelf/edit/${subItem.compartmentCode ? subItem.compartmentCode : this.order.id}/${this.order.id}/${
        subItem.processingStatus
      }/${subItem.compartmentCode ? 'c' : 'o'}/customer/${this.customerId}/${subItem.id}`;
      this.store.dispatch(
        new AddBreadcrumb(
          <Breadcrumb>{
            name: breadcrumbName,
            path: path,
          },
          'customer'
        )
      );
      this.store.dispatch(new ChangeCurrentRoute(path));
      this.router.navigate([path]);
    }
  }

  loadPreorderedTag() {
    this.isPreorder = this.item.preordered;
  }

  initializePaymentData() {
    if (this.paymentDataDTO && this.isPayed) {
      const receiptNumber = this.paymentDataDTO.receiptNumber;
      const receiptTp = receiptType[this.paymentDataDTO.receiptType];
      let printedDate: string;
      if (this.paymentDataDTO.printedDate) {
        printedDate = this.datePipe.transform(this.paymentDataDTO.printedDate, 'dd.MM.yy | HH:mm') + ' Uhr';
      }
      const placeOfPayment = this.paymentDataDTO.placeOfPayment;
      if (this.paymentDataDTO.paidAt && !isNullOrUndefined(this.paymentDataDTO.paidAt)) {
        this.isPayed = true;
      } else {
        this.isPayed = false;
      }
      const invoiceText = this.item && this.item.invoiceText ? this.item.invoiceText : '';
      this.paymentData = <OrderPaymentReceipt>{
        receiptNumber,
        placeOfPayment,
        invoiceText,
        printedDate,
        receiptType: receiptTp,
      };
    }
  }
}
