import { Component, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IInstanceModel } from '@fleio-api/openstack/model/instance.model';
import { InstancesApiService } from '@fleio-api/openstack/instance/instances-api.service';
import { IInstanceHistoryLogModel } from '@fleio-api/openstack/model/instance-history-log.model';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { NotificationService } from '@shared/ui-api/notification.service';
import { ConfigService } from '@shared/config/config.service';
import { PaginatorService } from '@shared/ui-api/paginator.service';

@Component({
  selector: 'app-instance-details-history-log',
  templateUrl: './instance-details-history-log.component.html',
  styleUrls: ['./instance-details-history-log.component.scss']
})
export class InstanceDetailsHistoryLogComponent extends DetailsComponentBase<IInstanceModel> implements OnInit {
  historyLogs: IInstanceHistoryLogModel[];
  loading: boolean;
  nextPage = false;
  previousPage = false;
  marker = null;
  markerHistory: Array<string> = [];

  constructor(
    private instancesApiService: InstancesApiService,
    private notificationService: NotificationService,
    public config: ConfigService,
    public paginator: PaginatorService,
  ) {
    super();
  }

  changePage(action: string) {
    if (action === 'next') {
      this.marker = this.historyLogs[this.historyLogs.length - 1].request_id;
      this.markerHistory.push(this.marker);
    }
    if (action === 'previous') {
      this.markerHistory.pop();
      if (!this.markerHistory.length) {
        this.marker = null;
      } else {
        this.marker = this.markerHistory[this.markerHistory.length - 1];
      }
    }
    this.loadHistoryLog();
  };

  private loadHistoryLog() {
    this.loading = true;
    this.instancesApiService.getActions(
      this.object.id, this.paginator.defaultPageSize, this.marker,
    ).subscribe({
      next: (response: FleioObjectsList<IInstanceHistoryLogModel>) => {
        this.historyLogs = response.objects;
        this.nextPage = response.hasMore;
        this.previousPage = !!this.marker;
        this.loading = false;
      }, error: () => {
        this.loading = false;
        this.notificationService.showMessage($localize`Could not load history logs.`);
      }
    });
  }

  public openedLog(historyLog: IInstanceHistoryLogModel, index: number) {
    this.historyLogs[index].loading = true;
    this.instancesApiService.getActionDetails(this.object.id, historyLog.request_id,).pipe().subscribe(
      (response: IInstanceHistoryLogModel) => {
        this.historyLogs[index].events = response.events;
        this.historyLogs[index].loading = false;
      }, () => {
        this.notificationService.showMessage($localize`Could not load history log details.`);
        this.historyLogs[index].loading = false;
      });
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.objectController.currentTabIndex$.subscribe(newTabIndex => {
        if (newTabIndex === this.componentTabIndex) {
          this.loadHistoryLog();
        }
      });
    }
  }

}
