import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { BreadcrumbService } from '@core/breadcrumb';
import { DomainGoodsService } from '@domain/oms';
import { combineLatest, Observable } from 'rxjs';
import { map, shareReplay, switchMap, withLatestFrom } from 'rxjs/operators';

@Component({
  selector: 'page-goods-out-edit',
  templateUrl: 'goods-out-edit.component.html',
  styleUrls: ['goods-out-edit.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class GoodsOutEditComponent implements OnInit {
  get processId() {
    return +this._activatedRoute.snapshot.parent.data.processId;
  }

  processId$ = this._activatedRoute.parent.data.pipe(map((params) => +params.processId));

  orderNumber$: Observable<string> = this._activatedRoute.params.pipe(
    map((params) => decodeURIComponent(params.orderNumber ?? '') || undefined)
  );

  processingStatus$ = this._activatedRoute.params.pipe(map((params) => params?.processingStatus || undefined));

  compartmentCode$: Observable<string> = this._activatedRoute.params.pipe(
    map((params) => decodeURIComponent(params?.compartmentCode ?? '') || undefined)
  );

  items$ = combineLatest([this.orderNumber$, this.compartmentCode$]).pipe(
    switchMap(([orderNumber, compartmentCode]) =>
      compartmentCode
        ? this._domainGoodsInService.getWarenausgabeItemByCompartment(compartmentCode)
        : this._domainGoodsInService.getWarenausgabeItemByOrderNumber(orderNumber)
    ),
    withLatestFrom(this.processingStatus$),
    map(([response, processingStatus]) => response.result.filter((item) => item.processingStatus === +processingStatus)),
    shareReplay()
  );

  constructor(
    private _activatedRoute: ActivatedRoute,
    private _breadcrumb: BreadcrumbService,
    private _domainGoodsInService: DomainGoodsService,
    private _router: Router
  ) {}

  ngOnInit() {
    this.updateBreadcrumb();
  }

  async updateBreadcrumb() {
    const orderNumber = this._activatedRoute.snapshot.params.orderNumber;
    const compartmentCode = this._activatedRoute.snapshot.params.compartmentCode;
    const processingStatus = this._activatedRoute.snapshot.params.processingStatus;
    await this._breadcrumb.addOrUpdateBreadcrumbIfNotExists({
      key: this.processId,
      name: 'Bearbeiten',
      path: compartmentCode
        ? `/kunde/${this.processId}/goods/out/details/compartment/${encodeURIComponent(compartmentCode)}/${processingStatus}/edit`
        : `/kunde/${this.processId}/goods/out/details/order/${encodeURIComponent(orderNumber)}/${processingStatus}/edit`,
      section: 'customer',
      tags: ['goods-out', 'edit', compartmentCode || orderNumber],
    });
  }

  navigateToDetailsPage({ options }: { options?: { processingStatus?: number } }) {
    const orderNumber = this._activatedRoute.snapshot.params.orderNumber;
    const compartmentCode = this._activatedRoute.snapshot.params.compartmentCode;
    const processingStatus = options?.processingStatus ? options.processingStatus : this._activatedRoute.snapshot.params.processingStatus;
    compartmentCode
      ? this._router.navigate([
          `/kunde/${this.processId}/goods/out/details/compartment/${encodeURIComponent(compartmentCode)}/${processingStatus}`,
        ])
      : this._router.navigate([`/kunde/${this.processId}/goods/out/details/order/${encodeURIComponent(orderNumber)}/${processingStatus}`]);
  }
}
