"use strict";

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Cognos Analytics
 * Copyright IBM Corp. 2016, 2017
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['bi/glass/app/ContentView', 'underscore', 'bi/admin/nls/StringResource', 'q', 'require', 'bi/commons/ui/properties/PropertyUIControl'], function (View, _, StringResource, Q, require, PropertyUIControl) {
  'use strict'; //NOSONAR

  var UserGroupSelectorPane = View.extend({
    init: function init(options) {
      UserGroupSelectorPane.inherited('init', this, arguments);

      _.extend(this, options);
    },
    render: function render() {
      var deferred = Q.defer();

      this._renderFacets().then(function () {
        deferred.resolve();
      });

      return deferred.promise;
    },
    _onClear: function _onClear() {
      this._oPropertyUIControl.getProperty('users').uncheck(false);

      this._oPropertyUIControl.getProperty('groups').uncheck(false);

      this._oPropertyUIControl.getProperty('roles').uncheck(false);

      this.callback.filterStringValue.account = false;
      this.callback.filterStringValue.group = false;
      this.callback.filterStringValue.role = false;

      this.callback._filter(this.callback.filterStringValue);
    },
    _renderPropertyControl: function _renderPropertyControl(checkBoxItems) {
      var deferred = Q.defer();
      this._oPropertyUIControl = new PropertyUIControl({
        'el': this.$el,
        'glassContext': this.glassContext,
        'slideout': this.slideout,
        'items': checkBoxItems
      });

      this._oPropertyUIControl.render().then(function () {
        deferred.resolve(this);
      }.bind(this));

      return deferred.promise;
    },
    _initCheckBoxItems: function _initCheckBoxItems() {
      //NOSONAR
      var checkBoxItems = [{
        type: 'SingleLineLinks',
        name: 'clearAll',
        items: [{
          align: 'right',
          items: [{
            type: 'text',
            value: StringResource.get('clearAll'),
            clickCallback: function () {
              this._onClear();
            }.bind(this)
          }]
        }]
      }, {
        'type': 'CheckBox',
        'name': 'users',
        'icon': 'wft_checkmark',
        'disabled': this.callback.listAdaptor.allowedSelectionTypes && !_.include(this.callback.listAdaptor.allowedSelectionTypes, "account") ? true : false,
        'label': StringResource.get('account'),
        'checked': this.callback.filterStringValue.account ? true : false,
        'onChange': function (name, value) {
          if (value === true) {
            this.callback.filterStringValue.account = true;
          } else {
            this.callback.filterStringValue.account = false;
          }

          this.callback._filter(this.callback.filterStringValue);
        }.bind(this)
      }, {
        'type': 'CheckBox',
        'name': 'groups',
        'icon': 'wft_checkmark',
        'disabled': this.callback.listAdaptor.allowedSelectionTypes && !_.include(this.callback.listAdaptor.allowedSelectionTypes, "group") ? true : false,
        'label': StringResource.get('group'),
        'checked': this.callback.filterStringValue.group ? true : false,
        'onChange': function (name, value) {
          if (value === true) {
            this.callback.filterStringValue.group = true;
          } else {
            this.callback.filterStringValue.group = false;
          }

          this.callback._filter(this.callback.filterStringValue);
        }.bind(this)
      }, {
        'type': 'CheckBox',
        'name': 'roles',
        'icon': 'wft_checkmark',
        'disabled': false,
        'label': StringResource.get('role'),
        'checked': this.callback.filterStringValue.role ? true : false,
        'onChange': function (name, value) {
          if (value === true) {
            this.callback.filterStringValue.role = true;
          } else {
            this.callback.filterStringValue.role = false;
          }

          this.callback._filter(this.callback.filterStringValue);
        }.bind(this)
      }];
      return checkBoxItems;
    },
    _renderFacets: function _renderFacets() {
      //NOSONAR
      var deferred = Q.defer();

      var checkBoxItems = this._initCheckBoxItems();

      this._renderPropertyControl(checkBoxItems).then(function () {
        deferred.resolve();
      });

      return deferred.promise;
    }
  });
  return UserGroupSelectorPane;
});