import { ChangeDetectorRef, Component, NgZone, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IDbaasInstanceModel } from '@fleio-api/openstack/model/dbaas-instance.model';
import { ConfigService } from '@shared/config/config.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { DbaasInstancesApiService } from '@fleio-api/openstack/dbaas/dbaas-instances-api.service';

@Component({
  selector: 'app-dbaas-instance-logs',
  templateUrl: './dbaas-instance-logs.component.html',
  styleUrls: ['./dbaas-instance-logs.component.scss']
})
export class DbaasInstanceLogsComponent extends DetailsComponentBase<IDbaasInstanceModel> implements OnInit {
  tableData: {
    name: string;
    databases: Array<string>;
  }[] = [];
  displayedColumns: string[] = ['name', 'type', 'status', 'published', 'pending', 'container', '(actions)'];
  loading = true;
  logsRefreshing = false;

  constructor(
    public config: ConfigService,
    private notificationService: NotificationService,
    private dbaasInstancesApiService: DbaasInstancesApiService,
    ngZone: NgZone,
    changeDetectorRef: ChangeDetectorRef,
  ) {
    super(ngZone, changeDetectorRef, config);
  }

  protected refreshData() {
    super.refreshData();
    this.loadLogs(true);
  }

  loadLogs(refresh=false) {
    if (!this.logsRefreshing) {
      if (!refresh) {
        // do not show loading when refreshing, only on tab activate
        this.loading = true;
      }
      this.logsRefreshing = true;
      this.dbaasInstancesApiService.getLogs(this.object.id).subscribe({
        next: value => {
          this.tableData = value.logs;
        }, error: error => {
          if (error.error?.detail) {
            this.notificationService.showMessage(error.error.detail);
          }
        }
      }).add(() => {
        this.logsRefreshing = false;
        this.loading = false;
      });
    }
  }

  enableLog(name: string) {
    this.notificationService.confirmDialog(
      {
        title: $localize`Enable ${name} logs`,
        message: $localize`Are you sure you want to enable ${name} logs?`,
        noButtonName: $localize`Cancel`,
        yesButtonName: $localize`Enable`,
      }
    ).subscribe(result => {
      if (result?.button === 'yes') {
        this.dbaasInstancesApiService.enableLogs(this.object.id, name).subscribe({
          next: () => {
            this.notificationService.showMessage($localize`Logs enabled`);
            this.loadLogs();
          }, error: error => {
            if (error.error?.detail) {
              this.notificationService.showMessage(error.error.detail);
            } else {
              this.notificationService.showMessage($localize`Failed to enable logs.`);
            }
          }
        });
      }
    });
  }

  publishLog(name: string) {
    this.notificationService.confirmDialog(
      {
        title: $localize`Publish ${name} logs`,
        message: $localize`Are you sure you want to publish ${name} logs?`,
        noButtonName: $localize`Cancel`,
        yesButtonName: $localize`Publish`,
      }
    ).subscribe(result => {
      if (result?.button === 'yes') {
        this.dbaasInstancesApiService.publishLogs(this.object.id, name).subscribe({
          next: () => {
            this.notificationService.showMessage($localize`Logs published`);
            this.loadLogs();
          }, error: error => {
            if (error.error?.detail) {
              this.notificationService.showMessage(error.error.detail);
            } else {
              this.notificationService.showMessage($localize`Failed to publish logs.`);
            }
          }
        });
      }
    });
  }

  disableLog(name: string) {
    this.notificationService.confirmDialog(
      {
        title: $localize`Disable ${name} logs`,
        message: $localize`Are you sure you want to disable ${name} logs?`,
        noButtonName: $localize`Cancel`,
        yesButtonName: $localize`Disable`,
      }
    ).subscribe(result => {
      if (result?.button === 'yes') {
        this.dbaasInstancesApiService.disableLogs(this.object.id, name).subscribe({
          next: () => {
            this.notificationService.showMessage($localize`Logs disabled`);
            this.loadLogs();
          }, error: error => {
            if (error.error?.detail) {
              this.notificationService.showMessage(error.error.detail);
            } else {
              this.notificationService.showMessage($localize`Failed to disable logs.`);
            }
          }
        });
      }
    });
  }

  discardLog(name: string) {
    this.notificationService.confirmDialog(
      {
        title: $localize`Discard ${name} logs`,
        message: $localize`Are you sure you want to discard ${name} logs?`,
        noButtonName: $localize`Cancel`,
        yesButtonName: $localize`Discard`,
      }
    ).subscribe(result => {
      if (result?.button === 'yes') {
        this.dbaasInstancesApiService.discardLogs(this.object.id, name).subscribe({
          next: () => {
            this.notificationService.showMessage($localize`Logs discarded`);
            this.loadLogs();
          }, error: error => {
            if (error.error?.detail) {
              this.notificationService.showMessage(error.error.detail);
            } else {
              this.notificationService.showMessage($localize`Failed to discard logs.`);
            }
          }
        });
      }
    });
  }

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

}
