import { Component, OnInit, OnDestroy } from '@angular/core';
import { Select, Store } from '@ngxs/store';
import { CollectingShelfSelectors } from 'apps/sales/src/app/core/store/selectors/collecting-shelf.selector';
import { Observable, Subject } from 'rxjs';
import { OrderDTO } from '@swagger/oms';
import { takeUntil, filter, take, map } from 'rxjs/operators';
import { isNullOrUndefined } from 'util';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { ChangeCurrentRoute } from 'apps/sales/src/app/core/store/actions/process.actions';
import { PopBreadcrumbsAfterCurrent } from 'apps/sales/src/app/core/store/actions/breadcrumb.actions';
import { CollectingShelfService } from 'apps/sales/src/app/core/services/collecting-shelf.service';
import { SetBranchProcessCurrentPath } from 'apps/sales/src/app/core/store/actions/branch-process.actions';
@Component({
  selector: 'app-shelf-edit-order-from-customer',
  templateUrl: './shelf-edit-order-from-customer.component.html',
  styleUrls: ['./shelf-edit-order-from-customer.component.scss'],
})
export class ShelfEditOrderFromCustomerComponent implements OnInit, OnDestroy {
  @Select(CollectingShelfSelectors.getShelfEditOrder) order$: Observable<OrderDTO>;
  destroy$ = new Subject();
  order: OrderDTO;
  compartmentCode: string;
  type: string;
  status: string;
  orderId: number;
  origin: string;
  customerId: number;
  itemId: number;
  multi = false;
  constructor(private store: Store, private router: Router, private route: ActivatedRoute, private shelfService: CollectingShelfService) {}
  ngOnInit() {
    this.route.params.pipe(take(1)).subscribe(this.routeParamsSubscriber$);
  }
  ngOnDestroy() {
    this.destroy$.next();
  }
  routeParamsSubscriber$ = (params: Params) => {
    if (params['id'] && params['status'] && params['type'] && params['orderId'] && params['origin'] && params['customerId']) {
      this.status = params['status'];
      this.type = params['type'];
      this.compartmentCode = params['id'];
      this.orderId = params['orderId'];
      this.origin = params['origin'];
      this.customerId = params['customerId'];
      this.itemId = +params['itemId'];
    }
    this.order$
      .pipe(
        takeUntil(this.destroy$),
        filter((data) => !isNullOrUndefined(data)),
        map((order) => ({
          ...order,
          items:
            this.itemId === 0 || isNullOrUndefined(this.itemId)
              ? order.items
              : order.items.filter((item) => item.data.subsetItems.some((subsetItem) => subsetItem.id === this.itemId)),
        }))
      )
      .subscribe(this.orderSubscriber$);
  };
  orderSubscriber$ = (order: OrderDTO) => {
    this.order = order;
    if (order.items && order.items && order.items.length > 1) {
      this.multi = true;
    } else {
      this.multi = false;
    }
  };
  done() {
    if (this.compartmentCode || this.orderId) {
      if (this.origin === 'shelf') {
        const path = `/shelf/details/${this.compartmentCode ? this.compartmentCode : this.orderId}/${this.orderId}/${this.status}/${
          this.type
        }`;
        this.store.dispatch(new PopBreadcrumbsAfterCurrent(path));
        this.store.dispatch(new ChangeCurrentRoute(path, true));
        this.router.navigate([path]);
      }
      if (this.origin === 'customer') {
        const path = `/customer/${this.customerId}/order/${this.orderId}`;
        this.store.dispatch(new PopBreadcrumbsAfterCurrent(path));
        this.store.dispatch(new ChangeCurrentRoute(path, true));
        this.router.navigate([path]);
      }
      if (this.origin === 'goodsin') {
        const path = `/goodsin/details/${this.orderId}/${this.itemId}/${this.status}`;
        this.store.dispatch(new SetBranchProcessCurrentPath(path, true));
        this.router.navigate([path]);
      }
    }
  }
  statusChanged(processingStatus: string) {
    this.status = processingStatus;
  }
  compartmentCodeChanged(compartmentCode: string) {
    this.compartmentCode = compartmentCode;
  }
}
