﻿/*#region license
//	GNU General Public License (GNU GPLv3)

//	Copyright © 2016 Odense Bys Museer

//	Author: Andriy Volkov

//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.

//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.

//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion*/

app.directive('xfield2', function ($compile) {
    return {
        scope:{
            xfield: "@field2b"
        },
        controller: function ($scope, $rootScope) {
            $scope.setupmode=false;
            $scope.settings={};
            $scope.created = false;

            $scope.field = JSON.parse($scope.xfield);
            $scope.field.key = $scope.field.DISPLAY_NAME;
            $scope.field.options = readField($scope.currentSrc, $scope.field.key);
            $scope.field.operations = readOperations(options, $scope);
            //$scope.IsEntity = $scope.field.IsAssociation;
            $scope.checked = !$scope.field.IsAssociation;

            $scope.toggleSettings = function ($evt) {
                var target = $($evt.target);
                $scope.setupmode = !$scope.setupmode;
                target.closest('.x-field2').find('.foption-box:first').toggleClass('settings-mode');
                $rootScope.updateSchema();
            }

            $scope.toggleChecked = function ($evt, caller) {
                var target = $($evt.target);
                var elem = target.closest('.j-schema.checkable');
                printLine('target j-schema : [' + elem.attr('aliace') + ']['+caller+']');
                $scope.checked = !$scope.checked;
                if (!$scope.checked) { $scope.toggleSettings($evt); }
                if ($scope.field.IsAssociation && !$scope.created) {
                    $scope.created = true;
                    angular.element(elem).append($compile("<jschemabase2><jschemabase2>")($scope));
                }
                $rootScope.updateSchema();
            }
        },
        controllerAs: 'XField2Ctrl',
        templateUrl: 'xfield2.tpl'
    };
});

function readOperations(field_options, $scope) {
    var operations = {};
    try {
        if ($scope.jfield2.IsAssociation || (!$scope.IsEmpty(field_options) && (!field_options.IsRequired || field_options.IsAssociation))) {
            operations.exists = { selected: false, key: 'exists', value: 'exists', type: 'bool', toggleSelected: function () { this.selected = !this.selected; if ($rootScope.updateSchema) { $rootScope.updateSchema.apply(); } } };
            operations.empty = { selected: false, key: 'empty', value: 'empty', type: 'bool', toggleSelected: function () { this.selected = !this.selected; if ($rootScope.updateSchema) { $rootScope.updateSchema.apply(); } } };
        }
    } catch (e) { }
    return operations;
}
function readField(src, key) {
    var field = null;
    try {
        if (src) {
            $.each(src.Params, function (i, param) {
                if (!field && ParamIsLike(param, key)) { field = param; }
            });
            if (!field) {
                //printLine('Failed read ['+key+'] from src['+JSON.stringify(src)+']');
            }
        }
    } catch (e) { }
    return field ? field : {};
}