// (C) Copyright 2020 Hewlett-Packard Enterprise Company, L.P.
/**
 * @type {MapNode}
 */
define(['hp/model/map/MapLink', 'hp/core/StatusState'],
function (MapLink, statusState) { "use strict";

    var MapNode = ( function() {

        function MapNode() {

            var self = this;
            var resource;
            var row;
            var selected = false;
            var selectionRelated = false;
            var highlighted = false;
            var summary;
            var links = [];
            
            this.id = function () {
                if (resource) {
                    return resource.uri;
                } else {
                    return summary.category;
                }
            };
            
            this.resource = function () {
                return resource;
            };
            
            this.row = function () {
                return row;
            };
            
            this.linkedTo = function (node) {
                var length, i;
                length = links.length;
                for (i=0; i<length; i++) {
                    if (links[i].hasNodeById(node.id())) {
                        return true;
                    }
                }
                return false;
            };
            
            this.links = function () {
                return links;
            };
            
            this.addLink = function (link) {
                links.push(link);
            };
            
            this.removeLink = function (link) {
                var index = links.indexOf(link);
                if (index !== -1) {
                    links.splice(index, 1);
                }
            };
            
            this.isSelected = function () {
                return selected;
            };
            
            this.setSelected = function (selectedArg) {
                var length, i;
                selected = selectedArg;
                length = links.length;
                for (i=0; i<length; i++) {
                    links[i].setSelectionRelated(selectedArg);
                }
            };
            
            this.isSelectionRelated = function () {
                return selectionRelated;
            };
            
            this.setSelectionRelated = function (selectionRelatedArg) {
                selectionRelated = selectionRelatedArg;
            };
            
            this.isHighlighted = function () {
                return highlighted;
            };
            
            this.setHighlighted = function (highlightedArg) {
                highlighted = highlightedArg;
            };
            
            this.summary = function () {
                return summary;
            };
            
            this.hasUri = function (uri) {
                if (resource) {
                    return (resource.uri === uri);
                } else {
                    return (summary.uriIndex[uri]);
                }
            };
            
            this.toString = function () {
                if (resource) {
                    return resource.uri;
                } else {
                    return summary.associationParams[0];
                }
            };
            
            this.init = function(resourceArg, rowArg) {
                var hasStatus,
                    key,
                    prevValue;
                resource = resourceArg;
                row = rowArg;
                
                if (resource.total) {
                    // if the resource has a 'total' property, then this is a summary node
                    summary = {uriIndex: {}, associationParams: []};
                    summary.total = resource.total;
                    summary.category = rowArg.category();
                    hasStatus = resource.status && 
                    $.map(resource.status, function(n, i) { return i; }).length > 0;
                    if (hasStatus) {
                        summary.status = {};
                        $.each(resource.status, function(status, val) {
                            key = statusState.getHealthStatus(status);
                            // if it already has a value, add to it. This is mainly for
                            // all status that maps to "Unknown".
                            prevValue = summary.status[key] ? summary.status[key] : 0;
                            summary.status[key] = prevValue + val;
                        });
                    }
                    
                    if (resource.association) {
                        summary.associationParams = ['f_an=' + resource.association.name, 
                                                     'f_suri=' + resource.association.parentUri];
                    }
                }
            };
            
            this.destroy = function() {
                resource = null;
                row = null;
            };
            
            this.isDifferent = function (otherMapNode) {
               return (this.id() !== otherMapNode.id());
            };
        }

        return MapNode;
    }());

    return MapNode;
});
