'use strict';

/**
 * Licensed Materials - Property of IBM
 * IBM Watson Analytics (C) Copyright IBM Corp. 2018, 2020
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 */

define(['../../lib/@waca/core-client/js/core-client/ui/core/View', '../../lib/@waca/core-client/js/core-client/utils/ContentFormatter', 'jquery', 'underscore', '../../widgets/livewidget/nls/StringResources', '../../util/JumpToActionHelper', 'text!../templates/JumpTo.html'], function (View, ContentFormatter, $, _, stringResources, JumpToActionHelper, JumpToTemplate) {

	/**
 *
 * View to manage/launch  Jump to
 **/
	var JumpToView = View.extend({
		templateString: JumpToTemplate,
		events: {
			'primaryaction .drillToTarget': '_drillToTarget',
			'primaryaction .manageDrillThrough': '_manageDrillThrough',
			'primaryaction .addNewTarget': '_addNewTarget'
		},

		init: function init(options) {
			JumpToView.inherited('init', this, arguments);
			options = options || {};
			this.content = options.content;
			this.dashboard = options.dashboardApi;
			this.sourceId = options.sourceId;
			if (options.jumpToTargets) {
				this.jumpToTargets = options.jumpToTargets(this.content, this.sourceId, options.selections);
			}
			this.isDataPointSelection = options.isDataPointSelection || false;
			this.scopedPageContext = options.scopedPageContext;
		},

		remove: function remove() {
			JumpToView.inherited('remove', this, arguments);
		},

		render: function render() {
			return this.dashboard.getDashboardSvc('DrillThroughService').then(function (drillThroughService) {
				return drillThroughService.getDrillThroughModelApi().then(this._getJumpToTargets.bind(this)).then(this._renderHtml.bind(this));
			}.bind(this));
		},

		notifyRenderComplete: function notifyRenderComplete() {
			this.$el.find('.visJumpToView-item.drillToTarget .visJumpToView-item-content').each(function (index, elem) {
				ContentFormatter.middleShortenString(elem);
			});
			//Set focus to the first item
			this.$el.find('.visJumpToView-item:first').focus();
		},

		_renderHtml: function _renderHtml(items) {
			if (items.length === 0) {
				this._addNewTargetItem(items);
			} else {
				_.each(items, function (item) {
					item.isTarget = true;
				});

				//Add manage to this list of items
				//check if the 'Manage...' item has already been added to the items array.
				var manageIndex = items.findIndex(function (item) {
					return item.isManage === true;
				});
				if (manageIndex === -1) {
					this._addManageItem(items);
				}
			}

			if (items.length > 0) {
				items[items.length - 1].hideDivider = true;
			}

			var sHtml = this.dotTemplate({ jumpToItems: items });
			var $sHtml = $(sHtml);
			this.$el.empty().append($sHtml);
		},

		_addNewTargetItem: function _addNewTargetItem(items) {
			if (!this._isDataPointSelection() && this._getAuthoringMode()) {
				items.push({
					addNewTarget: true,
					getId: function getId() {
						return 'addNewTarget';
					},
					getName: function getName() {
						return stringResources.get('drillthrough_addNewTarget');
					}
				});
			}
		},

		_getAuthoringMode: function _getAuthoringMode() {
			return this.dashboard.getMode() === 'authoring';
		},

		_addManageItem: function _addManageItem(items) {
			if (!this._isDataPointSelection() && this._getAuthoringMode()) {
				items.push({
					isManage: true,
					getId: function getId() {
						return 'manage';
					},
					getName: function getName() {
						return stringResources.get('drillthrough_manage');
					}
				});
			}
		},

		_getJumpToTargets: function _getJumpToTargets(dtModelApi) {
			if (this.jumpToTargets) {
				return Promise.resolve(this.jumpToTargets.targets);
			} else {
				var entries = dtModelApi.getDrillDefinitionEntries(this.content);
				this.jumpToTargets = JumpToActionHelper.getJumpToTargets({
					dashboardApi: this.dashboard,
					content: this.content,
					drillDefinitions: entries,
					selections: undefined,
					sourceId: this.sourceId
				});
				return Promise.resolve(this.jumpToTargets.targets);
			}
		},

		_isDataPointSelection: function _isDataPointSelection() {
			return this.isDataPointSelection;
		},

		_drillToTarget: function _drillToTarget(event) {
			var id = event.currentTarget.getAttribute('data-id');
			var target = this.jumpToTargets.targetsMap[id];
			if (target) {
				target.jumpTo();
			}
		},

		_manageDrillThrough: function _manageDrillThrough() {
			if (this.toolbar) {
				this.remove();
				this.toolbar.hide();
			}
			return this.dashboard.getDashboardSvc('DrillThroughService').then(function (drillThroughService) {
				return drillThroughService.getDrillThroughModelApi().then(function (dtModelApi) {
					var controller = drillThroughService.getDrillThroughController();
					var dlg = controller.manageDrillDefinition({
						content: this.content,
						dashboard: this.dashboard,
						drillThroughModelApi: dtModelApi
					});
					dlg.open();
					dlg.disableOk();
				}.bind(this));
			}.bind(this));
		},

		_addNewTarget: function _addNewTarget() {

			if (this.toolbar) {
				this.remove();
				this.toolbar.hide();
			}

			return this.dashboard.getDashboardSvc('DrillThroughService').then(function (drillThroughService) {
				var controller = drillThroughService.getDrillThroughController();
				controller.createDrillDefinition(this.content);
			}.bind(this));
		},

		preload: function preload() {
			return Promise.resolve();
		},

		renderCallBack: function renderCallBack(toolbar) {
			this.toolbar = toolbar;
		}
	});

	return JumpToView;
});
//# sourceMappingURL=JumpToView.js.map