import { Component, OnInit } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { ConfigurationsApiService } from '@fleio-api/configurations/configurations-api.service';
import { ConfigService } from '@shared/config/config.service';
import { Observable, of } from 'rxjs';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { NotificationService } from '@shared/ui-api/notification.service';
import { IConfigurationDomainsModel } from '@fleio-api/configurations/model/configuration-domains.model';
import { ValidatorsHelper } from '@shared/ui-api/helpers/validators-helper';

@Component({
  selector: 'app-domains-plugin-configuration',
  templateUrl: './domains-plugin-configuration.component.html',
  styleUrls: ['./domains-plugin-configuration.component.scss']
})
export class DomainsPluginConfigurationComponent extends DetailsFormBase<IConfigurationDomainsModel> implements OnInit {
  configurationDomainsForm = this.formBuilder.group({
    // Domain options
    allow_domain_registration: [false],
    allow_domain_transfer: [false],
    renew_domains_automatically_on_payment: [false],
    // Default TLD
    enable_default_tld: [false],
    default_tld: ['', Validators.required],
    // Default nameservers
    enable_default_nameservers: [false],
    default_nameserver1: ['', Validators.required],
    default_nameserver2: [''],
    default_nameserver3: [''],
    default_nameserver4: [''],
    // Domains notifications
    domain_notifications_enabled: [false],
    first_domain_renewal_reminder_remaining_hours: ['', ValidatorsHelper.requiredIntegerValidator],
    first_domain_renewal_reminder_notification_template: [''],
    second_domain_renewal_reminder_remaining_hours: ['', ValidatorsHelper.requiredIntegerValidator],
    second_domain_renewal_reminder_notification_template: [''],
    third_domain_renewal_reminder_remaining_hours: ['', ValidatorsHelper.requiredIntegerValidator],
    third_domain_renewal_reminder_notification_template: [''],
    fourth_domain_renewal_reminder_remaining_hours: ['', ValidatorsHelper.requiredIntegerValidator],
    fourth_domain_renewal_reminder_notification_template: [''],
    fifth_domain_renewal_reminder_remaining_hours: ['', ValidatorsHelper.requiredIntegerValidator],
    fifth_domain_renewal_reminder_notification_template: [''],
    domain_registration_confirmation_notification_template: [''],
    domain_transfer_initiated_notification_template: [''],
    domain_transfer_completed_notification_template: [''],
    domain_transfer_failed_notification_template: [''],
    domain_expired_notification_template: [''],
    prevent_orders_with_existing_domains: [true],
  });

  updatingControls = false;
  domainsConfiguration: IConfigurationDomainsModel;

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

  ngOnInit(): void {
    super.ngOnInit();
    if (this.objectController) {
      this.domainsConfiguration =
        this.objectController.additionalObjects.domainsConfiguration as IConfigurationDomainsModel;
    }

    this.configurationDomainsForm.valueChanges.subscribe(() => {
      if (!this.updatingControls) {
        this.updatingControls = true;
        this.updateControls();
        this.updatingControls = false;
      }
    });

    if (this.domainsConfiguration) {
      this.configurationDomainsForm.patchValue(this.domainsConfiguration);
    } else {
      this.configurationDomainsForm.disable();
    }
  }

  protected initTabData() {
    this.objectController.actionCallback = () => this.saveConfiguration();
  }

  updateControls() {
    const controls = this.configurationDomainsForm.controls;
    if (controls.enable_default_tld.value) {
      controls.default_tld.enable();
    } else {
      controls.default_tld.disable();
    }
    if (controls.enable_default_nameservers.value) {
      controls.default_nameserver1.enable();
      controls.default_nameserver2.enable();
      controls.default_nameserver3.enable();
      controls.default_nameserver4.enable();
    } else {
      controls.default_nameserver1.disable();
      controls.default_nameserver2.disable();
      controls.default_nameserver3.disable();
      controls.default_nameserver4.disable();
    }

    if (controls.domain_notifications_enabled.value) {
      controls.first_domain_renewal_reminder_remaining_hours.enable();
      controls.first_domain_renewal_reminder_notification_template.enable();
      controls.second_domain_renewal_reminder_remaining_hours.enable();
      controls.second_domain_renewal_reminder_notification_template.enable();
      controls.third_domain_renewal_reminder_remaining_hours.enable();
      controls.third_domain_renewal_reminder_notification_template.enable();
      controls.fourth_domain_renewal_reminder_remaining_hours.enable();
      controls.fourth_domain_renewal_reminder_notification_template.enable();
      controls.fifth_domain_renewal_reminder_remaining_hours.enable();
      controls.fifth_domain_renewal_reminder_notification_template.enable();
      controls.domain_registration_confirmation_notification_template.enable();
      controls.domain_transfer_initiated_notification_template.enable();
      controls.domain_transfer_completed_notification_template.enable();
      controls.domain_transfer_failed_notification_template.enable();
      controls.domain_expired_notification_template.enable();
    } else {
      controls.first_domain_renewal_reminder_remaining_hours.disable();
      controls.first_domain_renewal_reminder_notification_template.disable();
      controls.second_domain_renewal_reminder_remaining_hours.disable();
      controls.second_domain_renewal_reminder_notification_template.disable();
      controls.third_domain_renewal_reminder_remaining_hours.disable();
      controls.third_domain_renewal_reminder_notification_template.disable();
      controls.fourth_domain_renewal_reminder_remaining_hours.disable();
      controls.fourth_domain_renewal_reminder_notification_template.disable();
      controls.fifth_domain_renewal_reminder_remaining_hours.disable();
      controls.fifth_domain_renewal_reminder_notification_template.disable();
      controls.domain_registration_confirmation_notification_template.disable();
      controls.domain_transfer_initiated_notification_template.disable();
      controls.domain_transfer_completed_notification_template.disable();
      controls.domain_transfer_failed_notification_template.disable();
      controls.domain_expired_notification_template.disable();
    }
  }

  saveConfiguration(): Observable<IActionResult> {
    this.validate();
    if (this.configurationDomainsForm.invalid) {
      this.displayControlErrors();
      return of(null);
    }

    const value = this.configurationDomainsForm.getRawValue();

    value.id = this.object.id;

    const request = this.configurationsApiService.objectPutAction(value.id, 'domains', value);

    this.objectController.loading = true;
    request.subscribe((response) => {
      this.notificationService.showMessage(response.detail);
    }, (error) => {
      this.setErrors(error.error);
    }).add(() => {
      this.objectController.loading = false;
    });

    return of(null);
  }
}
