/*
 * Copyright (c) 2020, FusionAuth, All Rights Reserved
 */
'use strict';

var FusionAuth = FusionAuth || {};
FusionAuth.Admin = FusionAuth.Admin || {};

/**
 * @constructor
 */
FusionAuth.Admin.TenantForm = function() {
  Prime.Utils.bindAll(this);

  this.form =  Prime.Document.queryById('tenant-form');
  this.addTenant = this.form.getAttribute('action') === '/admin/tenant/add';
  this.smtpEditor = null;
  this.reorderConnector = null;
  this.addPolicyManager = new FusionAuth.Admin.TenantPolicyManager();
  new FusionAuth.Admin.SMTPTestDialog(this);

  // Spoilers
  new FusionAuth.UI.Spoiler(this.form.queryFirst('[data-spoiler=long-description]'), 'io.fusionauth.tenant.codeGeneratorDescription').initialize();
  new FusionAuth.UI.Spoiler(this.form.queryFirst('[data-spoiler=connector-settings-long-description]'), 'io.fusionauth.tenant.connectorDescription').initialize();

  new Prime.Widgets.Tabs(Prime.Document.queryFirst('.tabs'))
      .withErrorClassHandling('error')
      .withSelectCallback(this._handleTabSelect)
      .withLocalStorageKey(this.addTenant ? null : 'settings.tenant.tabs')
      .initialize();

  this.childRegistrationCheckbox = this.form.queryFirst('input[name="tenant.familyConfiguration.allowChildRegistrations"]')
                                       .addEventListener('change', this._handleChildRegistrationChange);
  this.noChildRegistrationDiv = new Prime.Effects.SlideOpen(Prime.Document.queryById('no-child-registration'));
  this.childRegistrationDiv = new Prime.Effects.SlideOpen(Prime.Document.queryById('child-registration'));

  this.failedAuthenticationUserAction = Prime.Document.queryById('tenant_failedAuthenticationConfiguration_userActionId')
                                             .addEventListener('change', this._handleFailedAuthenticationUserActionChange);
  this.failedAuthenticationSettings = new Prime.Effects.SlideOpen(Prime.Document.queryById('failed-authentication-options'));
};

FusionAuth.Admin.TenantForm.constructor = FusionAuth.Admin.TenantForm;
FusionAuth.Admin.TenantForm.prototype = {

  /* ===================================================================================================================
   * Private Methods
   * ===================================================================================================================*/

  _handleChildRegistrationChange: function() {
    if (this.childRegistrationCheckbox.isChecked()) {
      this.noChildRegistrationDiv.close();
      this.childRegistrationDiv.open();
    } else {
      this.noChildRegistrationDiv.open();
      this.childRegistrationDiv.close();
    }
  },

  _handleFailedAuthenticationUserActionChange: function() {
    if (this.failedAuthenticationUserAction.getValue() === '') {
      this.failedAuthenticationSettings.close();
    } else {
      this.failedAuthenticationSettings.open();
    }
  },

  _handleTabSelect: function(tab, tabContent) {
    if (tabContent.getId() === 'advanced-configuration') {
      var smtpProperties = Prime.Document.queryFirst('textarea[name="tenant.emailConfiguration.properties"]');
      if (this.smtpEditor === null) {
        this.smtpEditor = new FusionAuth.UI.TextEditor(smtpProperties)
            .withOptions({
              'mode': 'properties',
              'lineNumbers': true
            })
            .render()
            .setHeight(100);
      }
    }

    if (tabContent.getId() === 'connector-configuration') {
      this.addPolicyManager.onTabSelect();
    }
  }
};
