import { Component, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IProjectModel } from '@fleio-api/openstack/project/model/project.model';
import { ConfigService } from '@shared/config/config.service';
import { InvoicesApiService } from '@fleio-api/billing/invoices/invoices-api.service';
import { NotificationService } from '@shared/ui-api/notification.service';
import { RefreshService } from '@shared/ui-api/refresh.service';
import { IProjectQuotasCreateOptionsModel } from '@fleio-api/openstack/project/model/project-quotas-create-options.model';
import { UntypedFormBuilder } from '@angular/forms';

@Component({
  selector: 'app-project-select-region',
  templateUrl: './project-select-region.component.html',
  styleUrls: ['./project-select-region.component.scss']
})
export class ProjectSelectRegionComponent extends DetailsComponentBase<IProjectModel> implements OnInit {
  selectRegionForm = this.formBuilder.group({
    region_id: [''],
  });

  createOptions: IProjectQuotasCreateOptionsModel;

  constructor(
    private config: ConfigService,
    private invoiceApi: InvoicesApiService,
    private notificationService: NotificationService,
    private refreshService: RefreshService,
    private formBuilder: UntypedFormBuilder,
  ) {
    super();
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.createOptions = this.objectController.additionalObjects.createOptions as IProjectQuotasCreateOptionsModel;
      this.selectRegionForm.controls.region_id.setValue(this.createOptions.selected_region);
      this.selectRegionForm.controls.region_id.valueChanges.subscribe(region => {
        this.objectController.componentEvent.next(region);
      });
    }
  }
}
