/*
 * Copyright (c) 2018-2019, FusionAuth, All Rights Reserved
 */
'use strict';

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

/**
 * JavaScript for the manage user page.
 *
 * @constructor
 * @param userId {string} the user id.
 */
FusionAuth.Admin.ManageUser = function(userId) {
  Prime.Utils.bindAll(this);

  // Last Login table
  this.userId = userId;
  new FusionAuth.Admin.LastLogins(this.userId);

  // Attach a delegated listener to all the ajax forms anchors
  Prime.Document.addDelegatedEventListener('click', '[data-ajax-form="true"], [data-ajax-view="true"]', this._handleActionClick);

  // Setup the entity search
  var resultDiv = Prime.Document.queryById('entity-search-results');
  var container = Prime.Document.queryById('entities');
  var form = Prime.Document.queryById('entity-search-form');
  this.ajaxSearch = new FusionAuth.Admin.AJAXSearchForm()
      .withContainer(container)
      .withDatabaseSort()
      .withDefaultSearchCriteria({
        's.userId': userId,
        's.orderBy': 'name asc'
      })
      .withForm(form)
      .withResultDiv(resultDiv)
      .withStorageKey('io.fusionauth.user[' + userId + '].entities')
      .initialize();

  this.entityAJAXSearchInitialized = false;

  // Initialize last since the handleTabSelect may need to use the search form
  this.tabs = new Prime.Widgets.Tabs(Prime.Document.queryFirst('.tabs'))
      .withErrorClassHandling('error')
      .withLocalStorageKey('user.manage.tabs')
      .withSelectCallback(this._handleTabSelect)
      .initialize();
};

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

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

  _handleActionUserDialogOpen: function() {
    new FusionAuth.Admin.UserActioningForm(Prime.Document.queryById('user-actioning-form'));
  },

  _handleAddConsentDialogOpen: function() {
    new FusionAuth.Admin.AddUserConsentForm(Prime.Document.queryById('add-user-consent-form'));
  },

  _handleTabSelect: function(tab, tabContent) {
    // Search the first time we render this tab
    if (tabContent.getId() === 'entities' && !this.entityAJAXSearchInitialized) {
      this.ajaxSearch.search();
      this.entityAJAXSearchInitialized = true;
    }
  },

  __handleTwoFactorDialogOpen: function() {
    // When we do an admin disable, or a recovery code panel, we won't have this form Id.
    var form = Prime.Document.queryById('two-factor-form');
    if (form !== null) {
      this.twoFactorObject = new FusionAuth.Admin.TwoFactor(form);
    }
  },

  _handleActionClick: function(event, target) {
    Prime.Utils.stopEvent(event);
    var button = new Prime.Document.Element(target);
    var uri = button.getAttribute('href');

    if (uri.indexOf('/ajax/user/consent/add') !== -1) {
      new Prime.Widgets.AJAXDialog()
          .withFormHandling(true)
          .withCallback(this._handleAddConsentDialogOpen)
          .withFormErrorCallback(this._handleAddConsentDialogOpen)
          .withAdditionalClasses(button.is('[data-ajax-wide-dialog=true]') ? 'wide' : '')
          .open(uri);
    } else if (uri.indexOf('/ajax/user/two-factor') !== -1) {
      new Prime.Widgets.AJAXDialog()
          .withFormHandling(true)
          .withCallback(this.__handleTwoFactorDialogOpen)
          .withAdditionalClasses(button.is('[data-ajax-wide-dialog=true]') ? 'wide' : '')
          .open(uri);
    } else if (uri.indexOf('/ajax/user/action') === -1 && uri.indexOf('/ajax/user/modify-action') === -1) {
      // General handling - not for User Action
      var formHandling = uri.indexOf("/view") === -1; // if this isn't a view ajax, it has a form
      new Prime.Widgets.AJAXDialog()
          .withFormHandling(formHandling)
          .withAdditionalClasses(button.is('[data-ajax-wide-dialog=true]') ? 'wide' : '')
          .open(uri);
    } else {
      new Prime.Widgets.AJAXDialog()
          .withFormHandling(true)
          .withCallback(this._handleActionUserDialogOpen)
          .withFormErrorCallback(this._handleActionUserDialogOpen)
          .withAdditionalClasses(button.is('[data-ajax-wide-dialog=true]') ? 'wide' : '')
          .open(uri);
    }
  }
};