/*-
 * ~~~~~~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('entaxyInnerObjectInput', {
        bindings: {
            model: '=',
            mode: '<',
            type: '<',
            objectFactoryFilter: '<',
            fieldName: '<',
            readonly: '<',
            setFocused: '<',
            formController: '<'
        },
        template:
        `
          <div class="input-with-button">
            <input type="text" ng-model="$ctrl.info" ng-class="{'form-control': true}"
                    ng-keydown="$ctrl.handleEnter($event)" tabindex="-1" readonly>
            <span class="pficon pficon-delete" ng-click="$ctrl.clear()" ng-if="$ctrl.model"></span>
            <button class="btn-std entaxy-view-resources-form" ng-click="$ctrl.openModal()"
                    ng-if="!$ctrl.readonly || $ctrl.model" set-focused="$ctrl.setFocused">
              {{$ctrl.buttonTitle}}
            </button>
          </div>
        `,
        controller: entaxyInnerObjectInputController
    })
    .name;

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

        let ctrl = this;

        ctrl.$onInit = function () {

            if (ctrl.readonly) {
                ctrl.mode = Entaxy.MODAL_MODES.VIEW;
            }

            if (ctrl.model) {
                let factory = entaxyService.getFactoryByTitle(ctrl.model.factoryId);
                ctrl.info = factory.displayName;
                ctrl.buttonTitle = ctrl.mode === Entaxy.MODAL_MODES.VIEW ?
                    Entaxy.MODAL_MODES.VIEW : Entaxy.MODAL_MODES.EDIT;
            } else {
                ctrl.mode = Entaxy.MODAL_MODES.ADD;
                ctrl.buttonTitle = Entaxy.MODAL_MODES.ADD;
            }
        }

        ctrl.openModal = function() {
            $uibModal.open({
                component: 'entaxyInnerObjectInputModal',
                resolve: {
                    type: () => ctrl.type,
                    mode: () => ctrl.mode,
                    model: () => ctrl.model,
                    fieldName: () => ctrl.fieldName,
                    objectFactoryFilter: () => ctrl.objectFactoryFilter,
                    outerFormController: () => ctrl.formController
                },
                backdrop: 'static',
                size: 'xl',
                windowTopClass: 'modal-top-margin-override'
            })
            .result.then(args => {
                changeModelFromArgs(args);
            });
        }

        function changeModelFromArgs(args) {
            if (ctrl.model) {
                if (ctrl.model.factoryId === args.factoryId.name) {
                    args.fields.forEach(field => {
                        if (field.changed) {
                            ctrl.model.properties[field.name] = field.value;
                        }
                    });
                } else {
                    ctrl.model.factoryId = args.factoryId.name;
                    ctrl.model.properties = {};
                    args.fields.forEach(field => {
                        ctrl.model.properties[field.name] = field.value;
                    });
                }
            } else {
                ctrl.model = {};
                ctrl.model.factoryId = args.factoryId.name;
                ctrl.model.scope = Entaxy.SCOPE.PRIVATE;
                ctrl.model.type = ctrl.type;
                ctrl.model.properties = {};
                args.fields.forEach(field => {
                    ctrl.model.properties[field.name] = field.value;
                });

                ctrl.mode = Entaxy.MODAL_MODES.EDIT;
                ctrl.buttonTitle = Entaxy.MODAL_MODES.EDIT;
            }
            ctrl.info = args.factoryId.displayName;
        }

        ctrl.handleEnter = function (event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                if (!ctrl.readonly || ctrl.model) {
                    ctrl.openModal();
                }
            }
        }

        ctrl.clear = function () {
            ctrl.model = undefined;
            ctrl.info = undefined;
            ctrl.mode = Entaxy.MODAL_MODES.ADD;
            ctrl.buttonTitle = Entaxy.MODAL_MODES.ADD;
        }
    }
    entaxyInnerObjectInputController.$inject = ['workspace', 'entaxyService', '$uibModal'];

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