/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-h2-plugin
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
var EntaxyH2;
(function (EntaxyH2) {
    EntaxyH2.log.debug("loading console");
	
    EntaxyH2._module
    .component('entaxyH2Console', {
        template: `<iframe id="h2consoleFrame" src='/h2console/h2console/' style="width: 100%; height: 100%; overflow: scroll; border: none"></iframe>`,
        controller: EntaxyH2ConsoleController
    })
    .name;
	
	function EntaxyH2ConsoleController($scope, $element, $location, workspace, localStorage, jolokia) {
        'ngInject';
        var ctrl = this;
        this.$location = $location;
		entaxyH2location = this.$location;
		entaxyH2ctrl = ctrl;
		entaxyH2scope = $scope; 
		entaxyH2element = $element;

		var iFrame = $element.context.childNodes[0]; 

		this.prepareUrl = (function (){
			var path = workspace.selection.entries.path;
			var resultPath = path;
			if ((path.at(1)=="_") && (path.at(2)=="/")) {
				resultPath = path.substring(0,1) + ":" + path.substring(2);
			}
			return "jdbc:h2:" + resultPath; 
		}).bind(this);

		this.onLoad = (function() {
			if (iFrame.contentDocument.getElementById('login')){
				var form = iFrame.contentDocument.getElementById('login');
				
				// set correct connection url
				form.getElementsByTagName('input')
					.namedItem("url").value = this.prepareUrl();
				
				// add href for opening in new window
				var p = form.getElementsByTagName("p").item(0);
				var space = angular.element('<span>&nbsp;&nbsp;</span>');
				var link = angular.element('<a href="/h2console/h2console" target="_blank">[Open in new window]</a>');
				angular.element(p).append(space);
				angular.element(p).append(link);
			}
		}).bind(this);
	
	
		iFrame.onload = this.onLoad;
		
	}
	EntaxyH2ConsoleController.$inject = ['$scope', '$element', '$location', 'workspace', 'localStorage', 'jolokia'] 
	
})(EntaxyH2 || (EntaxyH2 = {}));
