"use strict";

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: admin
 * Copyright IBM Corp. 2016, 2017
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['underscore', 'bi/commons/ui/View', 'jquery', 'text!bi/admin/common/templates/ErrorViewTemplate.html', 'bi/admin/nls/StringResource'], function (_, BaseView, $, template, StringResource) {
  /**
   * Error view. This class renders a view with a list of errors.
   * @param - options
   * {
   *		errorList: Array of error objects
   * }
   */
  var ErrorView = BaseView.extend({
    templateString: template,
    events: {},
    displayObject: {
      errorList: []
    },
    errorIcons: {
      'ERROR': '#common-failed',
      'WARNING': '#common-warning',
      'INFO': '#common-error'
    },
    errorIconClass: {
      'ERROR': 'failed',
      'WARNING': 'warning',
      'INFO': 'info'
    },
    errorIconsTitleMap: {
      'ERROR': 'Error',
      'WARNING': 'Warning',
      'INFO': 'Information'
    },
    init: function init(options) {
      ErrorView.inherited('init', this, arguments);
      $.extend(this, options);
      this.displayObject.errorList = _.map(options.displayMsgList, function (errorItem) {
        return {
          severity: errorItem.severity,
          msg: errorItem.message,
          msgDetail: errorItem.messageDetail,
          errorIcon: this.errorIcons[errorItem.severity],
          errorIconClass: 'simple-dialog-view-severity-' + this.errorIconClass[errorItem.severity],
          severityIconTitle: StringResource.get(this.errorIconsTitleMap[errorItem.severity])
        };
      }.bind(this));
    },
    render: function render() {
      this.$el.empty();
      var sHtml = this.dotTemplate({
        displayObject: this.displayObject
      });
      this.$el.append(sHtml);

      this._bindEvents();
    },
    _bindEvents: function _bindEvents() {
      var errorEls = this.$el.find('.simple-dialog-item-header');
      errorEls.on('primaryaction', function (e) {
        var errorDetailsEl = $(e.target).closest('.simple-dialog-item-header').siblings('.error-view-details');

        if (errorDetailsEl && errorDetailsEl.is(':visible')) {
          errorDetailsEl.hide(100);
        } else {
          errorDetailsEl.show(100);
        }
      });
    }
  });
  return ErrorView;
});