/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-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 Entaxy;
(function (Entaxy) {
    Entaxy._module.component('entaxyConfigProperties', {
        template:
        `
          <h2>
            Configuration Properties
          </h2>
          
          <pf-toolbar class="entaxy-toolbar" config="$ctrl.toolbarConfig"></pf-toolbar>
          <pf-table-view config="$ctrl.tableConfig"
                          columns="$ctrl.tableColumns"
                          page-config="$ctrl.pageConfig"
                          items="$ctrl.viewedItems">
          </pf-table-view>
        `,
        controller: EntaxyConfigPropertiesController
    })
    .name;

    function EntaxyConfigPropertiesController(workspace, $location, $scope, $q, jolokia, jolokiaService, operationsService,
                                   entaxyService) {
        'ngInject';

        let ctrl = this;

        ctrl.tableConfig = {
            selectionMatchProp: 'displayName',
            showCheckboxes: false
        };

        ctrl.toolbarConfig = {
            filterConfig: {
                fields: [
                    {
                        id: 'displayName',
                        title: 'Name',
                        placeholder: 'Filter by Name...',
                        filterType: 'text'
                    }
                ],
                appliedFilters: [],
                onFilterChange: filterChange
            },
            isTableView: true
        };

        ctrl.tableColumns = [
            { header: 'Property', itemField: 'name' },
            { header: 'Name', itemField: 'displayName' },
            { header: 'Type', itemField: 'type' },
            { header: 'Value', itemField: 'value' }
        ];

        ctrl.pageConfig = Entaxy.getStandardPageConfig();

        function filterChange(filters) {
            ctrl.viewedItems = Entaxy.applyFilters(ctrl.items, filters, matchesFilter);
            ctrl.toolbarConfig.filterConfig.resultsCount = ctrl.viewedItems.length;
        };

        function matchesFilter(item, filter) {
            return item.displayName.match(filter.value) !== null;
        };

        ctrl.items = [];
        ctrl.viewedItems = [];

        ctrl.$onInit = function() {
            populateTable();
        }

        function populateTable() {
            let items = [];

            let selectedMbeanName = workspace.getSelectedMBeanName();

            entaxyService.readConfiguration(selectedMbeanName).then(config => {

                let itemKeys = Object.keys(config.properties);
                let configHint = JSON.parse(Entaxy.base64ToString(config.properties.__entaxy_config_hint));

                itemKeys.forEach(key => {
                    let fieldDescription = configHint.fields[key];

                    if (!fieldDescription.isInternal) {
                        items.push({
                            name: key,
                            displayName: fieldDescription.displayName,
                            type: fieldDescription.type,
                            value: config.properties[key]
                        });
                    }
                });

                ctrl.items = items;
                ctrl.viewedItems = items;
                ctrl.toolbarConfig.filterConfig.resultsCount = items.length;
            });
        }
    }
    EntaxyConfigPropertiesController.$inject = ['workspace', '$location', '$scope', '$q', 'jolokia', 'jolokiaService',
        'operationsService', 'entaxyService'];

})(Entaxy || (Entaxy = {}));
