import {
  Component,
  ChangeDetectionStrategy,
  Input,
  OnInit,
  SimpleChanges,
  OnChanges,
  ChangeDetectorRef,
  SimpleChange,
} from '@angular/core';
import { FormGroup } from '@angular/forms';
import { VATDTO } from '@swagger/oms';
import { VatTypeToDisplayNamePipe } from '../../pipes/vat-type-to-display-name.pipe';
import { VatDtoToVatValuePipe } from '../../pipes/vat-dto-to-vat-value.pipe';
import { VatDtoToVatTypePipe } from '../../pipes/vat-dto-to-vat-type.pipe';

@Component({
  selector: 'app-shelf-edit-order-item',
  templateUrl: 'edit-order-item.component.html',
  styleUrls: ['./edit-order-item.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ShelfEditOrderItemComponent implements OnInit, OnChanges {
  @Input() orderItemForm: FormGroup;
  @Input() onlyChild: boolean;
  @Input() firstChild: boolean;
  @Input() vatOptions: VATDTO[];

  vatLabelPipe: VatTypeToDisplayNamePipe;
  vatOptionsPipe = new VatDtoToVatValuePipe();
  valuePipe = new VatDtoToVatTypePipe();
  isGroupOpen: boolean;

  vatLabelTransform = (value) => {
    return this.vatLabelPipe.transform(value, this.vatOptions);
  };

  constructor() {}

  ngOnChanges({ vatOptions }: SimpleChanges) {
    if (vatOptions.currentValue) {
      this.vatLabelPipe = new VatTypeToDisplayNamePipe();
    }
  }

  ngOnInit() {
    this.isGroupOpen = this.onlyChild || this.firstChild;
  }

  expandGroup(shouldExpand: boolean) {
    this.isGroupOpen = shouldExpand;
  }
}
