define([
    'jquery',
    'underscore',
    'knockout',
    'knockout-mapping',
    'views/list',
    'viewmodels/function',
    'bindings/chosen',
    'templates/views/components/functions/datos-iniciales.htm'], 
function($, _, ko, koMapping, ListView, FunctionViewModel, chosen, DatosInicialesTemplate) {   
    return ko.components.register('views/components/functions/datos-iniciales', {
        viewModel:  function(params) {
            FunctionViewModel.apply(this, arguments);
            var self = this;
            this.nodesLugar = ko.observableArray();
            this.nodesLugarAmplio = ko.observableArray();
            this.nodesAlcance = ko.observableArray();
            this.nodesETA = ko.observableArray();
            this.nodesMeta = ko.observableArray();
            this.nodesCodigo = ko.observableArray();
            this.nodesNombre = ko.observableArray();

            this.lugar_node = params.config.lugar.ndeId;
            this.lugar_amplio_node = params.config.lugar_amplio.ndeId;
            this.alcance_node = params.config.alcance.ndeId;
            this.eta_node = params.config.eta.ndeId;
            this.codigo_node = params.config.codigo.ndeId;
            this.nombre_node = params.config.nombre.ndeId;
            this.meta_ubicacion = params.config.meta_ubicacion.ndeId;
            this.meta_identificacion = params.config.meta_identificacion.ndeId;
            this.meta_marco_legal = params.config.meta_marco_legal.ndeId;
            this.meta_caracteristicas = params.config.meta_caracteristicas.ndeId;

            this.triggering_nodegroups = params.config.triggering_nodegroups;

            this.lugar_node.subscribe(function(ng){
                self.nodesLugar().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ng === node.nodeid) {
                            self.triggering_nodegroups.push(node.nodegroup_id);
                            params.config.lugar.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.lugar_amplio_node.subscribe(function(ngx){
                self.nodesLugarAmplio().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngx === node.nodeid) {
                            self.triggering_nodegroups.push(node.nodegroup_id);
                            params.config.lugar_amplio.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.alcance_node.subscribe(function(nng){
                self.nodesAlcance().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (nng === node.nodeid) {
                            self.triggering_nodegroups.push(node.nodegroup_id);
                            params.config.alcance.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.eta_node.subscribe(function(ngg){
                self.nodesETA().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngg === node.nodeid) {
                            params.config.eta.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.meta_ubicacion.subscribe(function(ngg){
                self.nodesMeta().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngg === node.nodeid) {
                            params.config.meta_ubicacion.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.meta_identificacion.subscribe(function(ngg){
                self.nodesMeta().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngg === node.nodeid) {
                            params.config.meta_identificacion.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.meta_marco_legal.subscribe(function(ngg){
                self.nodesMeta().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngg === node.nodeid) {
                            params.config.meta_marco_legal.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.meta_caracteristicas.subscribe(function(ngg){
                self.nodesMeta().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngg === node.nodeid) {
                            params.config.meta_caracteristicas.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.nombre_node.subscribe(function(ngn){
                self.nodesNombre().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngn === node.nodeid) {
                            if (!self.triggering_nodegroups().includes(node.nodegroup_id)) {
                                self.triggering_nodegroups.push(node.nodegroup_id);
                            }
                            params.config.nombre.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });
            this.codigo_node.subscribe(function(ngc){
                self.nodesCodigo().filter(node => node.datatype !== "semantic")
                    .map(node => {
                        if (ngc === node.nodeid) {
                            if (!self.triggering_nodegroups().includes(node.nodegroup_id)) {
                                self.triggering_nodegroups.push(node.nodegroup_id);
                            }
                            params.config.codigo.ndegrpId = node.nodegroup_id;
                        }        
                    });
            });

            this.graph.nodes.forEach(function (node) {
                if (node.datatype != 'semantic'){
                    if (node.config.rdmCollection === "4059c810-2401-4114-a721-93ce74e002aa" && node.datatype === 'concept'){
                        this.nodesLugar.push(node);
                    }
                    if (node.config.rdmCollection === "4059c810-2401-4114-a721-93ce74e002aa" && node.datatype === 'concept-list'){
                        this.nodesLugarAmplio.push(node);
                    }
                    else if (node.config.rdmCollection === "7c4407d5-38bf-4c08-8fae-cae1dbd1c854") {
                        this.nodesAlcance.push(node);
                    }
                    else if (node.alias === "codigo_principal") {
                        this.nodesCodigo.push(node);
                    }
                    else if (node.alias === "entidad_administrativa") {
                        this.nodesETA.push(node);
                    }
                    else if (node.datatype === "non-localized-string") {
                        this.nodesMeta.push(node);
                    }
                    else if (node.alias === "identificacion" || node.alias === "titulo" || node.alias === "nominacion_actual" || node.alias === "denominacion") {
                        this.nodesNombre.push(node);
                    }
                }
            }, this);

            window.setTimeout(function(){$("select[data-bind^=chosen]").trigger("chosen:updated")}, 300);
        },
        template: DatosInicialesTemplate
    });
});