"use strict";

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Cognos Analytics
 * Copyright IBM Corp. 2017
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['react', 'react-dom', 'underscore', 'bi/admin/nls/StringResource', 'bacontentnav/common/ContentListPageView', 'ba-react-admin/ba-react-admin.min', 'jquery'], function (React, ReactDOM, _, StringResource, ContentView, AdminReact, $) {
  'use strict'; //NOSONAR

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

      _.extend(this, options);

      this._modified = false;
      this._orgValues = $.extend({}, this.values);
    },
    render: function render() {
      this.$el.empty();
      return this._getSupportedContentLocales().then(function () {
        this.$el.css('height', '100%');
        var valuesArray = [],
            item;

        for (item in this.values) {
          valuesArray.push({
            locale: item,
            value: this.values[item]
          });
        }

        ReactDOM.render(React.createElement(AdminReact.SetLanguagesPane, {
          values: valuesArray,
          supportedContentLocales: this._supportedContentLocales,
          width: this.slideout.width,
          saveCallback: this._saveCallback.bind(this),
          cancelCallback: this._cancelCallback.bind(this),
          el: this.$el,
          StringResource: StringResource
        }), this.$el[0]);
      }.bind(this));
    },
    _getSupportedContentLocales: function _getSupportedContentLocales() {
      return this.glassContext.services.fetch.get('v1/configuration/keys/supportedContentLocales_' + this.glassContext.services.userProfile.preferences.productLocale).then(function (response) {
        this._supportedContentLocales = [];

        _.map(JSON.parse(response.data['supportedContentLocales_' + this.glassContext.services.userProfile.preferences.productLocale]), function (value, key) {
          this._supportedContentLocales.push({
            'icon': '',
            'label': value,
            'key': key,
            'value': value,
            'group': key
          });
        }.bind(this));

        this._supportedContentLocales = _.sortBy(this._supportedContentLocales, function (locale) {
          return locale.label;
        });
        return Promise.resolve(true);
      }.bind(this));
    },
    _saveCallback: function _saveCallback(values) {
      this.values = {};
      var length = values.length;

      for (var i = 0; i < length; i++) {
        if (values[i].value !== '') {
          this.values[values[i].locale] = values[i].value;
        }
      }

      if (this.saveCallback) {
        this.saveCallback(this.values);
        this.slideout.hide();
      }
    },
    _cancelCallback: function _cancelCallback() {
      this.slideout.hide();
    },
    _deleteCallBack: function _deleteCallBack(index) {
      this.valuesArray.splice(index, 1);
    }
  });
  return SetLanguagesView;
});