import { ChangeDetectorRef, Component, NgZone, OnDestroy, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IClientModel } from '@fleio-api/client-user/model/client.model';
import { ConfigService } from '@shared/config/config.service';
import { JournalsApiService } from '@fleio-api/billing/journal/journal-api.service';
import { ObjectListController } from '@objects-view/object-list-controller';
import { ObjectController } from '@objects-view/object-controller';
import { map } from 'rxjs/operators';
import { PaginatorService } from '@shared/ui-api/paginator.service';
import { Sort } from '@angular/material/sort';
import { OrderingHelper } from '@shared/ui-api/helpers/ordering-helper';
import { JournalEditTrackerService } from '../../../../../staff/billing/journal/journal-edit-tracker.service';
import { Subscription } from 'rxjs';
import { ClientDetailsJournalListUiService } from '../../client-details-journal-list-ui.service';

@Component({
  selector: 'app-client-details-journal',
  templateUrl: './client-details-journal.component.html',
  styleUrls: ['./client-details-journal.component.scss']
})
export class ClientDetailsJournalComponent extends DetailsComponentBase<IClientModel> implements OnInit, OnDestroy {
  objectListController: ObjectListController<ObjectController>;
  currentPageSize = this.paginator.defaultPageSize;
  sort = {active: 'date_added', direction: 'desc'} as Sort;
  loading = false;

  private journalEditTrackerServiceSubscription: Subscription;

  constructor(
    public paginator: PaginatorService,
    private journalsApiService: JournalsApiService,
    public config: ConfigService,
    private journalEditTrackerService: JournalEditTrackerService,
    public journalListUiService: ClientDetailsJournalListUiService,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, config);
  }

  protected tabActivated() {
    super.tabActivated();
    this.loadJournal();
  }

  ngOnInit() {
    super.ngOnInit();
    this.journalEditTrackerServiceSubscription = this.journalEditTrackerService.journalChanged$.subscribe(()=>{
      this.loadJournal();
    })
  }

  increasePageSize() {
    this.currentPageSize += this.paginator.defaultPageSize;
    this.loadJournal();
  }

  sortFn(data: Sort) {
    this.sort = data;
    this.loadJournal();
  }

  loadJournal() {
    this.loading = true;
    this.objectListController = null;
    this.changeDetectorRef.detectChanges();
    this.objectListController = new ObjectListController<ObjectController>(
      this.journalsApiService.list({
        client_id: this.object.id,
        page_size: this.currentPageSize,
        ordering: OrderingHelper.toStr(this.sort),
      }).pipe(map(data => {
        this.loading = false;
        return data;
      })),
      this.journalListUiService,
    );
  }

  ngOnDestroy() {
    if (this.journalEditTrackerServiceSubscription) {
      this.journalEditTrackerServiceSubscription.unsubscribe();
      this.journalEditTrackerServiceSubscription = null;
    }
    super.ngOnDestroy();
  }
}
