import { Component, OnInit, Input } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

import { ListItem } from '../../../model';


@Component({
  selector: 'butter-item-editor',
  templateUrl: './item-editor.component.html',
  styleUrls: ['./item-editor.component.scss']
})
export class ItemEditorComponent implements OnInit {
  @Input() item: ListItem;
  workingItem: ListItem;  

  constructor(private bsModalRef : BsModalRef) { }

  ngOnInit() {
  	this.workingItem = ListItem.copy(this.item)
  }

  incrementCurrentQuantity() : void {
    this.workingItem.increment();
  }

  decrementCurrentQuantity() : void {
    this.workingItem.decrement(); 
  }

  incrementFullQuantity() : void {
    this.workingItem.fullStockQuantity++;
  }

  decrementFullQuantity() : void {
    if (this.workingItem.fullStockQuantity > 0) {
      this.workingItem.fullStockQuantity--; 
    }
  }

  save() : void {
    this.item.name = this.workingItem.name;
    this.item.currentQuantity = this.workingItem.currentQuantity;
    this.item.fullStockQuantity = this.workingItem.fullStockQuantity;
    this.item.incrementAmount = this.workingItem.incrementAmount;
    this.bsModalRef.hide();
  }

  hide() : void {
    this.bsModalRef.hide();
  }

}
