/*-
 * ~~~~~~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('entaxyCiCdRevision', {
        template:
        `
          <div class="entaxy-cicd-zero-revision-note" ng-if="$ctrl.revision === '0'">
            <entaxy-form-field-extra-info type="'info'" message="$ctrl.zeroRevisionNote"></entaxy-form-field-extra-info>
          </div>
          <div class="entaxy-cicd-header-container">
            <div class="entaxy-cicd-title-with-info-container">
              <span class="first">Application: {{$ctrl.application}}</span>
              <span>Version: {{$ctrl.version}}</span>
              <span class="lead">Revision: {{$ctrl.revision}}</span>
              <span>Revision Status: {{$ctrl.revisionStatus}}</span>
            </div>
            <div class="entaxy-cicd-header-buttons-container">
              <entaxy-ci-cd-application-management-buttons storage="$ctrl.storage" application="$ctrl.application"
                        version="$ctrl.version" revision="$ctrl.revision" revision-status="$ctrl.revisionStatus"
                        is-configuration-dirty="$ctrl.isConfigurationDirty">
              </entaxy-ci-cd-application-management-buttons>
            </div>
          </div>
          
          <div class="entaxy-cicd-content-container">
            <h2 class="first">
              Configurations
            </h2>
            
            <entaxy-ci-cd-configurations change-selection="$ctrl.changeSelection"
                    configured="$ctrl.isConfigured"></entaxy-ci-cd-configurations>
            
            <div class="entaxy-cicd-configuration-container" ng-if="$ctrl.configuration">
            
              <entaxy-ci-cd-configuration configuration-id="$ctrl.configuration"
                    configured="$ctrl.isConfigured" is-dirty="$ctrl.isConfigurationDirty"></entaxy-ci-cd-configuration>
            </div>
          </div>
        `,
        controller: EntaxyCICDRevisionController
    })
        .name;

    function EntaxyCICDRevisionController(workspace) {
        'ngInject';

        let ctrl = this;
        ctrl.zeroRevisionNote = 'Revision 0 cannot be configured';

        ctrl.$onInit = function() {

            ctrl.storage = EntaxyCICD.getStorage(workspace.selection);
            ctrl.application = EntaxyCICD.getProjectOrApplication(workspace.selection);
            ctrl.version = EntaxyCICD.getVersion(workspace.selection);

            let revisionInfo = EntaxyCICD.getRevisionInfo(workspace.selection);
            ctrl.revision = revisionInfo.revision;
            ctrl.revisionStatus = revisionInfo.revisionStatus;
            ctrl.isConfigured = ctrl.revisionStatus === EntaxyCICD.REVISION_STATUS.CONFIGURED;
        }

        ctrl.changeSelection = function (configurationId) {
            if (!ctrl.isConfigurationDirty || confirm(Entaxy.CONFIRMATION.UNSAVED_CHANGES)) {
                ctrl.configuration = configurationId;
            }
        }
    }
    EntaxyCICDRevisionController.$inject = ['workspace'];

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