/*-
 * ~~~~~~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('entaxyCiCdProjects', {
        template:
        `
          <div class="entaxy-cicd-content-with-title-container">
            <h2>
              Projects
            </h2>

            <entaxy-ci-cd-applications-table type="Project" create-project="$ctrl.showCreateProjectModal">
            </entaxy-ci-cd-applications-table>
          </div>
        `,
        controller: EntaxyCICDProjectsController
    })
    .name;

    function EntaxyCICDProjectsController(workspace, $uibModal, entaxyCICDService, $scope, $route) {
        'ngInject';

        let ctrl = this;

        const isStorageSelected = EntaxyCICD.isStorage(workspace.selection);

        ctrl.$onInit = function() {
        }

        ctrl.showCreateProjectModal = function(items) {
            $uibModal.open({
                component: 'entaxyCiCdSimpleFieldsModal',
                resolve: {
                    modalTitle: () => 'Create Project',
                    formFields: () => getFormFields(items)
                },
                backdrop: 'static'
            })
            .result.then(args => {
                createProject(args);
            },
            reason => {
                if (reason && reason !== Entaxy.MODAL_CANCEL_REASON.ESCAPE_KEY_PRESS) {
                    Entaxy.notificationError(reason);
                }
            });
        }

        function getFormFields(items) {
            return [
                {
                    name: 'name',
                    label: 'Name',
                    type: 'text',
                    required: true,
                    validation: {
                        checkUniqueness: true,
                        values: items.map(project => project.name),
                        content: {
                            regex: '^[a-zA-Z0-9-_]+$',
                            errorMessage: 'Value must contain only latin letters, numbers, hyphens and underscores'
                        },
                        length: {
                            min: 3
                        }
                    },
                    setFocused: true
                },
                {
                    name: 'group',
                    label: 'Group',
                    type: 'text',
                    validation: {
                        content: {
                            regex: '^[a-zA-Z0-9-_]*$',
                            errorMessage: 'Value must contain only latin letters, numbers, hyphens and underscores'
                        }
                    },
                },
                {
                    name: 'storage',
                    label: 'Storage',
                    type: 'string',
                    value: isStorageSelected ? workspace.selection.title : undefined,
                    enum: isStorageSelected ? undefined : true,
                    values: isStorageSelected ? undefined : workspace.selection.children.map(child => child.title),
                    required: true,
                    readOnly: isStorageSelected ? true : undefined
                },
                {
                    name: 'version',
                    label: 'Version',
                    type: 'string',
                    value: '1.0.0',
                    required: true,
                    validation: {
                        content: {
                            regex: '^([1-9][0-9]*|0)\.([1-9][0-9]*|0)\.([1-9][0-9]*|0)$',
                            errorMessage: 'Value must match the pattern: X.X.X, where X represents the number'
                        }
                    }
                }
            ];
        }

        function createProject(args) {
            let projectName = args[1] ? args[1] + '/' + args[0] : args[0];
            entaxyCICDService.executeOperation('createProject', [projectName, args[2], args[3]])
            .then(result => {
                Entaxy.notificationSuccess(result);
                entaxyCICDService.treeUpdated();
            })
            .catch(error => {
                Entaxy.notificationError(error);
                EntaxyCICD.log.error(error);
            });
        }
    }
    EntaxyCICDProjectsController.$inject = ['workspace', '$uibModal', 'entaxyCICDService', '$scope', '$route'];

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