import { Component } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { NotificationService } from '@shared/ui-api/notification.service';
import { RegionsApiService } from '@fleio-api/openstack/region/regions-api.service';
import { IRegionModel } from '@fleio-api/openstack/model/region.model';
import { UntypedFormBuilder } from '@angular/forms';

@Component({
  selector: 'app-region-settings',
  templateUrl: './region-settings.component.html',
  styleUrls: ['./region-settings.scss']
})
export class RegionSettingsComponent extends DetailsFormBase<IBaseFleioObjectModel> {
  protected focusFirstInput = false;
  regions: IRegionModel[] = null;
  regionsControls = this.formBuilder.array([]);
  regionsForm = this.formBuilder.group({
    regions: this.regionsControls,
  });

  constructor(
    private regionsAPI: RegionsApiService,
    private formBuilder: UntypedFormBuilder,
    private notificationService: NotificationService,
  ) {
    super();
  }

  private initRegionControls(region: IRegionModel) {
    const regionGroup = this.formBuilder.group({
      enabled: [true],
      enable_volumes: [true],
      enable_snapshots: [true],
      disable_for_enduser: [true],
      can_process_metrics: [true],
      enable_networks: [true],
      enable_routers: [true],
      enable_instances: [true],
      floating_ip_on_instance_create: [false],
    });

    regionGroup.controls.enabled.valueChanges.subscribe(value => {
      if (value) {
        regionGroup.controls.disable_for_enduser.enable();
        regionGroup.controls.enable_volumes.enable();
        regionGroup.controls.enable_snapshots.enable();
        regionGroup.controls.can_process_metrics.enable();
        regionGroup.controls.enable_networks.enable();
        regionGroup.controls.enable_routers.enable();
        regionGroup.controls.enable_instances.enable();
        regionGroup.controls.floating_ip_on_instance_create.enable();
      } else {
        regionGroup.controls.disable_for_enduser.disable();
        regionGroup.controls.enable_volumes.disable();
        regionGroup.controls.enable_snapshots.disable();
        regionGroup.controls.can_process_metrics.disable();
        regionGroup.controls.enable_networks.disable();
        regionGroup.controls.enable_routers.disable();
        regionGroup.controls.enable_instances.disable();
        regionGroup.controls.floating_ip_on_instance_create.disable();
      }
    })

    regionGroup.patchValue(region);
    this.regionsControls.push(regionGroup);
  }

  protected initTabData() {
    this.regionsAPI.getAllRegions().subscribe(regionList => {
      this.regions = regionList.objects;
      this.regionsControls.clear();
      for (const region of this.regions) {
        this.initRegionControls(region);
      }
    }).add(() => {
    });
  }

  save() {
    this.objectController.loading = true;

    const formValue = this.regionsForm.getRawValue();
    for (let i = 0; i < formValue.regions.length; i++) {
      this.regions[i] = {...this.regions[i], ...formValue.regions[i]};
    }

    this.regionsAPI.updateRegions(this.regions).subscribe({
        next: () => {
          this.notificationService.showMessage($localize`Region settings saved successfully.`);
        },
        error: () => {
          this.notificationService.showMessage(
            $localize`Cannot save region settings, check logs for details!`
          );
        }
      }
    ).add(() => {
      this.objectController.loading = false;
    });
  }
}
