import { Component, OnInit } from '@angular/core';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { Observable, of } from 'rxjs';
import { IConfigurationModel } from '@fleio-api/configurations/model/configuration.model';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { ConfigurationsApiService } from '@fleio-api/configurations/configurations-api.service';
import { ConfigService } from '@shared/config/config.service';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';

@Component({
  selector: 'app-configuration-edit-form',
  templateUrl: './configuration-edit-form.component.html',
  styleUrls: ['./configuration-edit-form.component.scss']
})
export class ConfigurationEditFormComponent extends DetailsFormBase<IConfigurationModel> implements OnInit {
  configurationForm = this.formBuilder.group({
    name: ['', Validators.required],
    description: [''],
    is_default: [false],
  });

  constructor(
    private formBuilder: UntypedFormBuilder, private configurationsApiService: ConfigurationsApiService,
    private router: Router, private config: ConfigService,
  ) {
    super();
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.objectController.actionCallback = () => this.saveConfiguration();
    }
    if (this.object?.id) {
      this.configurationForm.patchValue(this.object);
      if (this.object.is_default) {
        this.configurationForm.controls.is_default.disable();
      }
    }
  }

  saveConfiguration(): Observable<IActionResult> {
    const value = this.configurationForm.value as IConfigurationModel;

    this.createOrUpdate(this.configurationsApiService, value).subscribe(() => {
      this.router.navigateByUrl(
        this.config.getPrevUrl('settings/configurations')
      ).catch(() => {});
    });

    return of(null);
  }
}
