import { Component } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { SettingsApiService } from '@fleio-api/core/settings-api.service';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { NotificationService } from '@shared/ui-api/notification.service';
import { ConfigService } from '@shared/config/config.service';
import { IReferralSettingsModel } from '@fleio-api/core/model/referral-settings.model';
import { ValidatorsHelper } from '@shared/ui-api/helpers/validators-helper';

@Component({
  selector: 'app-referral-settings',
  templateUrl: './referral-settings.component.html',
  styleUrl: './referral-settings.component.scss'
})
export class ReferralSettingsComponent extends DetailsComponentBase<IBaseFleioObjectModel> {
  referralSettingsForm = this.formBuilder.group({
    enable_referral_system: [false],
    referee_credit: [null, [ValidatorsHelper.requiredNumericValueValidator, Validators.min(0), ValidatorsHelper.nonZeroValidator]],
    referee_credit_valid_days: [null, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
    referrer_credit: [null, [ValidatorsHelper.requiredNumericValueValidator, Validators.min(0), ValidatorsHelper.nonZeroValidator]],
    referee_spends_credit: [null, [ValidatorsHelper.requiredNumericValueValidator, Validators.min(0), ValidatorsHelper.nonZeroValidator]],
    referee_active_days: [null, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
  });
  loading = false;
  referralSettings: IReferralSettingsModel;

  constructor(
    private settingsApi: SettingsApiService,
    private formBuilder: UntypedFormBuilder,
    private notificationService: NotificationService,
    public config: ConfigService,
  ) {
    super();
  }

  ngOnInit(refreshTimerInterval?: number) {
    super.ngOnInit(refreshTimerInterval);
    this.referralSettingsForm.controls.enable_referral_system.valueChanges.subscribe(value => {
      if (value) {
        this.referralSettingsForm.controls.referee_credit.enable();
        this.referralSettingsForm.controls.referee_credit_valid_days.enable();
        this.referralSettingsForm.controls.referrer_credit.enable();
        this.referralSettingsForm.controls.referee_spends_credit.enable();
        this.referralSettingsForm.controls.referee_active_days.enable();
      } else {
        this.referralSettingsForm.controls.referee_credit.disable();
        this.referralSettingsForm.controls.referee_credit_valid_days.disable();
        this.referralSettingsForm.controls.referrer_credit.disable();
        this.referralSettingsForm.controls.referee_spends_credit.disable();
        this.referralSettingsForm.controls.referee_active_days.disable();
      }
    })
  }

  protected initTabData() {
    this.loading = true;
    this.settingsApi.getReferralSettings().subscribe(referralSettings => {
      this.referralSettings = referralSettings;
      this.referralSettingsForm.patchValue(referralSettings);
    }, error => {
      if (error.status === 403) {
        this.referralSettingsForm.disable();
      }
    }).add(() => {
      this.loading = false;
    });
  }

  saveSettings() {
    const referralSettings = this.referralSettingsForm.value;
    this.objectController.loading = true;
    this.settingsApi.saveReferralSettings(referralSettings).subscribe(() => {
      this.notificationService.showMessage($localize`Referral settings saved`);
    }, () => {
      this.notificationService.showMessage($localize`Failed to save referral settings, check logs for details`);
    }).add(() => {
      this.objectController.loading = false;
    });
  }
}
