/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-cicd-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 EntaxyCICD;
(function (EntaxyCICD) {
    EntaxyCICD._module.component('entaxyCiCdApplicationManagementButtons', {
        bindings: {
            storage: '<',
            application: '<',
            version: '<',
            revision: '<',
            revisionStatus: '<',
            isConfigurationDirty: '<'
        },
        template:
        `
          <button type="button" ng-click="$ctrl.configure()"
                ng-if="!$ctrl.isInstalled && !$ctrl.isUninstalled">Configure</button>
          <button type="button" ng-click="$ctrl.deploy()"
                ng-if="!$ctrl.isInstalled && !$ctrl.isUninstalled">Deploy</button>
          <button type="button" ng-click="$ctrl.install()" ng-if="!$ctrl.isInstalled">Install</button>
          <button type="button" ng-click="$ctrl.uninstall()" ng-if="$ctrl.isInstalled">Uninstall</button>
        `,
        controller: EntaxyCICDApplicationManagementButtonsController
    })
        .name;

    function EntaxyCICDApplicationManagementButtonsController(workspace, entaxyCICDService) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function() {
            ctrl.isConfigured = ctrl.revisionStatus === EntaxyCICD.REVISION_STATUS.CONFIGURED;
            ctrl.isDeployed = ctrl.revisionStatus === EntaxyCICD.REVISION_STATUS.DEPLOYED;
            ctrl.isInstalled = ctrl.revisionStatus === EntaxyCICD.REVISION_STATUS.INSTALLED;
            ctrl.isUninstalled = ctrl.revisionStatus === EntaxyCICD.REVISION_STATUS.UNINSTALLED;
        }

        const OPERATION_TYPE = {
            CONFIGURE: 'configure',
            DEPLOY: 'deploy',
            INSTALL: 'install',
            UNINSTALL: 'uninstall'
        }

        ctrl.configure = function () {
            processOperation(OPERATION_TYPE.CONFIGURE);
        }

        ctrl.deploy = function () {
            processOperation(OPERATION_TYPE.DEPLOY);
        }

        ctrl.install = function () {
            processOperation(OPERATION_TYPE.INSTALL);
        }

        ctrl.uninstall = function () {
            processOperation(OPERATION_TYPE.UNINSTALL);
        }

        function processOperation(operationType) {
            if (!ctrl.isConfigurationDirty || confirm(Entaxy.CONFIRMATION.UNSAVED_CHANGES)) {

                entaxyCICDService.executeOperation(operationType + 'Revision',
                    [ctrl.storage, ctrl.application, ctrl.version, ctrl.revision])
                .then(result => {
                    let message =
                        operationType === OPERATION_TYPE.INSTALL || operationType === OPERATION_TYPE.UNINSTALL ?
                            EntaxyCICD.capitalize(operationType) + 'ation started successfully. ' +
                            '<b>Please notice that operation may take some time.</b>' : result;
                    Entaxy.notification(Entaxy.NOTIFICATION_TYPE.SUCCESS, message,
                        Entaxy.configuration[Entaxy.CONFIGURATION_KEYS.NOTIFICATION_TIMEOUT_SUCCESS]);
                    entaxyCICDService.treeUpdated();
                }).catch(error => {
                    Entaxy.notificationError(error);
                    EntaxyCICD.log.error(error);
                });
            }
        }
    }
    EntaxyCICDApplicationManagementButtonsController.$inject = ['workspace', 'entaxyCICDService'];

})(EntaxyCICD || (EntaxyCICD = {}));
