import { Component, OnInit, ViewChild, Input, Output, EventEmitter, ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';
import { DeleteDropdownComponent } from '@libs/ui';
import { CartReviewItem } from '../../pages/cart-review/cart-review.component';
import { DeliveryOption } from 'apps/sales/src/app/core/models/delivery-option.model';
import { of } from 'rxjs';
import { Store } from '@ngxs/store';

@Component({
  selector: 'app-download-cart-row',
  templateUrl: './download-cart-row.component.html',
  styleUrls: ['./download-cart-row.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DownloadCartRowComponent implements OnInit {
  @ViewChild('ref') deleteDropdown: DeleteDropdownComponent;
  @Input() item: CartReviewItem;
  @Output() details = new EventEmitter();
  @Output() editAdress = new EventEmitter();
  @Output() quantity = new EventEmitter();
  @Output() modalVisible = new EventEmitter();
  @Output() delete = new EventEmitter();
  loadingBtn = false;
  deactivateTenPlus = false;

  _open = false;
  get open$() {
    return of(this._open);
  }

  get price(): number {
    if (this.item) {
      if (this.item.deliveryType === DeliveryOption.PICK_UP) {
        return this.item.pickUpPrice ? this.item.pickUpPrice : this.item.price;
      } else if (this.item.deliveryType === DeliveryOption.DELIVERY) {
        return this.item.deliveryPrice ? this.item.deliveryPrice : this.item.price;
      } else if (this.item.deliveryType === DeliveryOption.DOWNLOAD) {
        return this.item.downloadPrice ? this.item.downloadPrice : this.item.price;
      } else {
        return this.item.price;
      }
    }
    return 0;
  }

  _btnBlocked: boolean;
  @Input() set changeItemBtnBlocked(blockedItemId: number) {
    this._btnBlocked = blockedItemId && blockedItemId !== this.item.cartEntryId ? true : false;
  }

  quantityError;
  availableQuantity = [1];

  constructor(private store: Store, private cdRef: ChangeDetectorRef) {}

  ngOnInit() {
    this.deactivateTenPlus = true;
  }

  deleteItem() {
    if (this.item) {
      this.delete.emit(this.item);
    }
  }
}
