/*-
 * ~~~~~~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('entaxyCiCdBuildInfo', {
        bindings: {
            type: '@'
        },
        template:
        `
          <div class="entaxy-cicd-header-container">
            <div class="entaxy-cicd-title-with-info-container">
              <span class="first lead">Build</span>
              <span>Project: {{$ctrl.project}}</span>
              <span>Version: {{$ctrl.version}}</span>
              <div ng-if="$ctrl.mavenUrl">
                <span>Maven URL: {{$ctrl.mavenUrl}}</span>
                <entaxy-copy-to-clipboard-button title="Copy maven URL to clipboard"
                        text-to-copy="$ctrl.mavenUrl"></entaxy-copy-to-clipboard-button>
              </div>
            </div>
            <div class="entaxy-cicd-header-buttons-container">
              <div class="btn-group" uib-dropdown>
                <button id="single-button" type="button" uib-dropdown-toggle ng-disabled="disabled">
                  Export
                  <span class="caret"></span>
                </button>
                <ul class="dropdown-menu dropdown-menu-right" uib-dropdown-menu
                        role="menu" aria-labelledby="single-button">
                  <li role="menuitem"><a ng-click="$ctrl.exportToFile()">Export To File</a></li>
                  <li role="menuitem"><a ng-click="$ctrl.exportToRepository()">Export To Repository</a></li>
                </ul>
              </div>
            </div>
          </div>
          
          <div class="entaxy-cicd-content-container">
            <h2 class="first">
              {{ $ctrl.type }}
            </h2>
            
            <entaxy-ci-cd-project-items ng-if="$ctrl.isItems()"></entaxy-ci-cd-project-items>
            <entaxy-ci-cd-requirements ng-if="$ctrl.isRequirements()"
                    version="$ctrl.version" type="BuiltProject"></entaxy-ci-cd-requirements>
            <entaxy-ci-cd-project-components ng-if="$ctrl.isComponents()"></entaxy-ci-cd-project-components>
          </div>
        `,
        controller: entaxyCICDBuildInfoController
    })
        .name;

    function entaxyCICDBuildInfoController(workspace, entaxyCICDService, entaxyService, $uibModal) {
        'ngInject';

        let ctrl = this;
        const storage = EntaxyCICD.getStorage(workspace.selection);
        const TYPE = {
            ITEMS: 'Items',
            REQUIREMENTS: 'Requirements',
            COMPONENTS: 'Components'
        }

        ctrl.isItems = function () {
            return ctrl.type === TYPE.ITEMS;
        }

        ctrl.isRequirements = function () {
            return ctrl.type === TYPE.REQUIREMENTS;
        }

        ctrl.isComponents = function () {
            return ctrl.type === TYPE.COMPONENTS;
        }

        ctrl.$onInit = function() {

            ctrl.project = EntaxyCICD.getProjectOrApplication(workspace.selection);
            ctrl.version = EntaxyCICD.getVersion(workspace.selection);

            entaxyCICDService.executeOperation('readMavenUrl', [storage, ctrl.project, ctrl.version])
            .then(result => {
                ctrl.mavenUrl = result;
            });
        }

        ctrl.exportToFile = function () {
            entaxyCICDService.executeOperation('exportToFile', [storage, ctrl.project, ctrl.version])
            .then(result => {
                let fileName = ctrl.project + '-' + ctrl.version + '.jar';
                entaxyService.downloadFileFromBase64StringContent(result, fileName);
            }).catch(error => {
                Entaxy.notificationError(error);
                EntaxyCICD.log.error(error);
            });
        }

        ctrl.exportToRepository = function () {
            entaxyCICDService.executeOperation('listRepositories', [])
            .then(result => {
                let formFields = [
                    {
                        label: 'Repository',
                        name: 'repository',
                        enum: true,
                        required: true,
                        values: JSON.parse(result),
                        setFocused: true
                    }
                ];

                showExportModal(formFields);
            }).catch(error => {
                Entaxy.notificationError(error);
            });
        }

        function showExportModal(formFields) {
            $uibModal.open({
                component: 'entaxyCiCdSimpleFieldsModal',
                resolve: {
                    modalTitle: () => 'Export Project To Repository',
                    formFields: () => formFields
                },
                backdrop: 'static'
            }).result.then((args) => {
                exportToRepository(args[0]);
            }, reason => {
                if (reason && reason !== Entaxy.MODAL_CANCEL_REASON.ESCAPE_KEY_PRESS) {
                    Entaxy.notificationError(reason);
                }
            });
        }

        function exportToRepository(repository) {
            entaxyCICDService.executeOperation('exportToRepository',
                [storage, ctrl.project, ctrl.version, repository])
            .then(result => {
                Entaxy.notificationSuccess(result);
            }).catch(error => {
                Entaxy.notificationError(error);
                EntaxyCICD.log.error(error);
            });
        }

    }
    entaxyCICDBuildInfoController.$inject = ['workspace', 'entaxyCICDService', 'entaxyService', '$uibModal'];

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