import { Component, OnInit, ViewChild } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { IDbaasInstanceModel } from '@fleio-api/openstack/model/dbaas-instance.model';
import { FlavorsAsCardsComponent } from '@shared/fleio-data-controls/flavors-as-cards/flavors-as-cards.component';
import { UntypedFormBuilder } from '@angular/forms';
import { Router } from '@angular/router';
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';
import { IFlavorModel } from '@fleio-api/openstack/model/flavor.model';
import { of } from 'rxjs';

@Component({
  selector: 'app-dbaas-instance-resize-flavor-form',
  templateUrl: './dbaas-instance-resize-flavor-form.component.html',
  styleUrls: ['./dbaas-instance-resize-flavor-form.component.scss']
})
export class DbaasInstanceResizeFlavorFormComponent extends DetailsFormBase<IDbaasInstanceModel> implements OnInit {
  @ViewChild('flavorsAsCards') flavorsAsCardsComponent: FlavorsAsCardsComponent;
  databaseInstanceResizeForm = this.formBuilder.group({
    flavor_id: [''],
  });
  resizeOptions: {
    flavors: Array<IFlavorModel>;
  };

  constructor(
    private formBuilder: UntypedFormBuilder,
    private router: Router,
    private config: ConfigService,
    private notificationService: NotificationService,
    private dbaasInstancesApiService: DbaasInstancesApiService,
  ) {
    super();
  }


  ngOnInit(refreshTimerInterval?: number) {
    super.ngOnInit(refreshTimerInterval);
    if (this.objectController) {
      this.objectController.actionCallback = () => this.resizeDatabaseInstance();
      if (this.objectController.additionalObjects) {
        this.resizeOptions = this.objectController.additionalObjects.resizeOptions;
        if (!this.resizeOptions.flavors.length) {
          this.primaryAction.disabled = true;
        }
      }
    }
  }

  onFlavorChosen(flavor: IFlavorModel) {
    if (flavor) {
      this.databaseInstanceResizeForm.controls.flavor_id.setValue(flavor.id);
    } else {
      this.databaseInstanceResizeForm.controls.flavor_id.setValue('');
    }
  }

  resizeDatabaseInstance() {
    this.validate();
    if (this.formGroup.invalid) {
      this.displayControlErrors();
      return of(null);
    }
    if (!this.flavorsAsCardsComponent.selectedFlavor) {
      this.notificationService.showMessage($localize`Select a flavor`);
      return of(null);
    }
    const value = this.databaseInstanceResizeForm.value;
    this.objectController.loading = true;
    this.dbaasInstancesApiService.resizeFlavor(this.object.id, value.flavor_id).subscribe({
      next: () => {
        this.notificationService.showMessage($localize`Resize database instance started`);
        this.router.navigateByUrl(
          this.config.getPanelUrl(`openstack/database-instances/${this.object.id}`)
        ).catch(() => {
        });
      }, error: (error) => {
        this.objectController.loading = false;
        this.formErrors.setBackendErrors(error.error);
      }
    });

    return of(null);
  }

}
