/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-atlasmap-plugin
 * ==========
 * Copyright (C) 2020 - 2024 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 EntaxyAtlasmap;
(function (EntaxyAtlasmap) {
    EntaxyAtlasmap.log.debug("loading console");

    EntaxyAtlasmap._module
        .component('entaxyAtlasmapConsole', {
            template: `<iframe id="atlasmapConsoleFrame" src='/atlasmap-ui' style="width: calc(100% + 52px); height: 100%; margin: 0 -20px -6px -32px; overflow: scroll; border: none;"></iframe>`,
            controller: EntaxyAtlasmapConsoleController
        })
        .name;

    function EntaxyAtlasmapConsoleController($element, $uibModal, $scope, $interval, workspace, operationsService) {
        'ngInject';
        let ctrl = this;
        let iframe = $element.context.childNodes[0];

        window.onmessage = function (event) {
            if (event.origin !== iframe.contentWindow.origin) {
                return;
            }
            switch (event.data) {
                case 'openRegistryViewerForExport':
                    openViewer('folder', 'foldersOnly', true);
                    break;
                case 'openRegistryViewerForImport':
                    openViewer('resource', '*.adm', false);
                    break;
                case 'openRegistryViewerForImportInstanceOrSchemaFile':
                    openViewer('resource', ['*.json', '*.xml', '*.xsd', '*.csv'], false);
                    break;
            }
        }

        function openViewer(target, filter, enableFolderAdding) {

            let config = {
                resourceProvider: 'schemas',
                target: target,
                filter: filter,
                useShortUrl: true,
                enableFolderAdding: enableFolderAdding
            };

            $uibModal.open({
                component: 'entaxyResourceViewerModal',
                resolve: {
                    location: () => ctrl.location,
                    config: () => config
                },
                size: 'xl',
                backdrop: 'static'
            }).result.then(location => {
                ctrl.location = location;
                iframe.contentWindow.postMessage(location, iframe.contentWindow.origin);
            });
        }

        let pingInterval;

        $scope.startUpdate = function() {
            if (angular.isDefined(pingInterval)) return;

            pingInterval = $interval(ping, 60000);
        }

        $scope.startUpdate();

        $scope.stopUpdate = function() {
            if (angular.isDefined(pingInterval)) {
                $interval.cancel(pingInterval);
                pingInterval = undefined;
            }
        }

        $scope.$on('$destroy', function() {
            $scope.stopUpdate();
        });

        function ping() {
            let entaxyAtlasmapJmxDomain = localStorage['entaxyAtlasmapJmxDomain'] || "ru.entaxy.atlasmap";
            let atlasmapMBean = workspace.findMBeanWithProperties(entaxyAtlasmapJmxDomain, { module: 'main' });
            operationsService.executeOperation(atlasmapMBean.objectName, { name: 'ping' }, []).then(result => {});
        }

    }
    EntaxyAtlasmapConsoleController.$inject = ['$element', '$uibModal', '$scope', '$interval', 'workspace',
        'operationsService']

})(EntaxyAtlasmap || (EntaxyAtlasmap = {}));
