// (C) Copyright 2020 Hewlett-Packard Enterprise Company, L.P.
/**
 * @type (NodeView)
 * This is a view class for Baselines
 * Provides functions for showing master pane and its action.
 */
define(['hpsum/model/node/NodeResource',
	'hp/core/Router',
	'hp/view/MasterPaneView',
	'hpsum/presenter/node/NodePresenter',
	'hp/core/Localizer',
    'jquery',
	'hp/lib/jquery.hpStatus',
    'hp/lib/jquery.hpUtilizationMeter',
    'hp/lib/jquery.hpSafeClone'],
function(resource,router, MasterPaneView, presenter, localizer) {"use strict";

    var NodeView = ( function() {
		
		var TYPE = 'node';
		var PAGE = '#hpsum-nodes-page';

        /**
         * @constructor
         * @type {NodeView}
         */
        function NodeView() {

			var masterPaneView = new MasterPaneView();
			var template = null;
			var types = [],
				types_existing =[];
			
			function renderer(indexResult) {
               var item = template.hpSafeClone();
                $('.hp-status', item).hpStatus(indexResult.status, indexResult.changing);
                $('.hp-master-item-name', item).html(indexResult.name);
                $('.hpsum-node-type',item).html(localizer.getString('hpsum.common.type') + ':' + indexResult.type);
                $('.hpsum-node-status',item).html(localizer.getString('hpsum.common.statusStr') + ':' +indexResult.node_status);
                return item;
            }
			
			function getItemUrl(oObj){

				var resource = router.location().split('/');
                var urlHead = resource[1];
	    		
	    		return oObj.aData.name;
	    		
	    		//Commented the following lines to avoid opening the node in another tab in the browser                                    
	    		/*
	    		return '<a class="hpsum-node-class" href="#/' + urlHead + '/show/r' + oObj.aData.uri + 
                                    '">' + oObj.aData.name +
                                    '</a>';
                */
			}
			
            /**
             * @public
             */
            this.init = function() {
				presenter.init();
                
                template = $('#hpsum-node-item-template').detach().show().attr('id', '');
                
             
                presenter.setFilter("");
                masterPaneView.init({
                    routePrefix: '/' + TYPE,
                    resource: presenter.resource(),
                    page: $(PAGE),
                    multiSelect: false,
                    addLink: presenter.getAddLink("addNode"),
                    dataTableOptions:  {
                        aoColumns : [
                            {fnRender: function (oObj) {
                                return '<div>' + oObj.aData.status +
                                    (oObj.aData.changing ? ' *' : '') +
                                    '</div>';
                            }, mDataProp: 'status'},
                            {fnRender: getItemUrl, mDataProp: 'name'} //,
                            //{mDataProp: 'attributes.model'}
                        ],
                        aaSorting: [[ 1, "asc" ]],
                        fnRowCallback : function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                            $('td:eq(0) > div', nRow).hpStatus();
                            return nRow;
                        },
                        fnCreatedRow :function(nRow, aData, iDisplayIndex){
						},
						
						fnDrawCallback :function(nRow, aData, iDisplayIndex){
						var selectedType = '';
						if($('#types').find('.hp-selected')[0] != undefined)
						{
							selectedType = $('#types').find('.hp-selected')[0].textContent;
						}
						if(selectedType == localizer.getString('hpsum.common.all') || selectedType == localizer.getString('hpsum.common.allTypes'))
						{
							var html = '<li data-id="all" class="hp-selected">' + localizer.getString('hpsum.common.all') + '</li>';	
							var types = [];
							for(var row = 0; row < nRow.aoData.length; row ++)
							{
								if($.inArray(nRow.aoData[row]._aData.type,types) == -1)
								{
									types.push(nRow.aoData[row]._aData.type,types);
								html += '<li data-id="' + nRow.aoData[row]._aData.type + '">' + nRow.aoData[row]._aData.type  + '</li>';	
								}
							}
							$('#types', PAGE).html(html);
						}
						}
                    },
                    gridItemRenderer: renderer
                });
  
            };

            /**
             * @public
             */
            this.pause = function() {
				masterPaneView.pause();
            };

            /**
             * @public
             */
            this.resume = function() {
				masterPaneView.resume();
				presenter.setFilter("");
            };
        }

        return new NodeView();
    }());

    return NodeView;
});
