import { Component, OnInit } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { IClientModel } from '@fleio-api/client-user/model/client.model';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { CoreApiService } from '@fleio-api/core/core-api.service';
import { Observable, of } from 'rxjs';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { IClientAutoInvoicingSettingsModel } from '@fleio-api/client-user/model/client-auto-invoicing-settings.model';
import { NotificationService } from '@shared/ui-api/notification.service';
import { ValidatorsHelper } from '@shared/ui-api/helpers/validators-helper';

@Component({
  selector: 'app-configure-auto-invoicing-form',
  templateUrl: './configure-auto-invoicing-form.component.html',
  styleUrls: ['./configure-auto-invoicing-form.component.scss']
})
export class ConfigureAutoInvoicingFormComponent extends DetailsFormBase<IClientModel> implements OnInit {
  autoInvoicingSettingsForm = this.formBuilder.group({
    enable_auto_invoicing: [false, Validators.required],
    auto_invoicing_type: [false, Validators.required],
    auto_invoicing_amount: [null],
  });
  autoInvoicingSettings: IClientAutoInvoicingSettingsModel;

  constructor(
    private formBuilder: UntypedFormBuilder,
    private coreApi: CoreApiService,
    private notificationService: NotificationService,
  ) {
    super();
  }

  changedEnableAutoInvoicing(value: boolean) {
    if (value) {
      this.autoInvoicingSettingsForm.controls.auto_invoicing_type.enable();
      this.autoInvoicingSettingsForm.controls.auto_invoicing_amount.enable();
    } else {
      this.autoInvoicingSettingsForm.controls.auto_invoicing_type.disable();
      this.autoInvoicingSettingsForm.controls.auto_invoicing_amount.disable();
    }
  }

  changedAutoInvoicingType(value: string) {
    if (value === 'fixed') {
      this.autoInvoicingSettingsForm.controls.auto_invoicing_amount.setValidators(
        ValidatorsHelper.requiredNumericValueValidator
      );
    } else {
      this.autoInvoicingSettingsForm.controls.auto_invoicing_amount.clearValidators();
      this.autoInvoicingSettingsForm.controls.auto_invoicing_amount.setErrors(null);
    }
  }

  ngOnInit(): void {
    super.ngOnInit();
    this.autoInvoicingSettingsForm.controls.enable_auto_invoicing.valueChanges.subscribe(value => {
      this.changedEnableAutoInvoicing(value);
    });
    this.autoInvoicingSettingsForm.controls.auto_invoicing_type.valueChanges.subscribe(value => {
      this.changedAutoInvoicingType(value);
    });
    if (this.objectController) {
      this.objectController.actionCallback = () => this.saveSettings();
      this.autoInvoicingSettings = this.objectController.additionalObjects.autoInvoicingSettings;
      this.autoInvoicingSettingsForm.patchValue(this.autoInvoicingSettings.auto_invoicing_settings);
    }
  }

  saveSettings(): Observable<IActionResult> {
    this.validate();
    if (this.autoInvoicingSettingsForm.invalid) {
      this.displayControlErrors();
      return of(null);
    }
    const value = this.autoInvoicingSettingsForm.getRawValue();

    this.objectController.loading = true;
    this.coreApi.setCreditAutoInvoicingSettings(value).subscribe({
      next: () => {
        this.objectController.loading = false;
        this.notificationService.showMessage($localize`Settings saved`);
      },
      error: error => {
        this.objectController.loading = false;
        if (error.error) {
          this.setErrors(error.error);
        }
      }
    });

    return of(null);
  }
}
