type = ['','info','success','warning','danger'];

dashboard = {
    initPickColor: function(){
        $('.pick-class-label').click(function(){
            var new_class = $(this).attr('new-class');
            var old_class = $('#display-buttons').attr('data-class');
            var display_div = $('#display-buttons');
            if(display_div.length) {
            var display_buttons = display_div.find('.btn');
            display_buttons.removeClass(old_class);
            display_buttons.addClass(new_class);
            display_div.attr('data-class', new_class);
            }
        });
    },

    initChartist: function(){
        var getData = $.get('/1/app/usage/trend');

        getData.done(function(results) {
            var data = JSON.parse(results)
            var node = data['apps@michaels-MBP'];
            var app = node['chrome'];
            var cpu = app['cpu'];
            var memory_used = app['memory_used'];
            console.log('data...',data)
            console.log('node...',node)
            console.log('app...',app)
            console.log('cpu...',cpu)
            console.log('memory_used...', memory_used)


// monitoring apps chart
        var dataChart = {
          labels: ['24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6','5','4','3','2','1' ],
          series: [cpu, memory_used, [1,2,3,5], ] // TODO: Check the order, the graph is by index not name.
        };

        dataChartArray = dataChart.series
        var maxlist = dataChartArray.map(dataChartArray => Math.max.apply(null, dataChartArray));
        maxNum = Math.max.apply(null, maxlist)

        var optionsChart = {
          lineSmooth: false,
          low: 0,
          high: maxNum + 1,
          showArea: true,
          height: "245px",
          axisX: {
            showGrid: false,
          },
          lineSmooth: Chartist.Interpolation.simple({
            divisor: 1
          }),
          showLine: true,
          showPoint: false,
        };

        var responsiveChart = [
          ['screen and (max-width: 640px)', {
            axisX: {
              labelInterpolationFnc: function (value) {
                return value[0];
              }
            }
          }]
        ];

        Chartist.Line('#chartHours', dataChart, optionsChart, responsiveChart);

// cpu and memory --------------------------
        var data = {
          labels: ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
          series: [cpu, memory_used]

        };

        var options = {
            seriesBarDistance: 10,
            axisX: {
                showGrid: false
            },
            height: "245px"
        };

        var responsiveOptions = [
          ['screen and (max-width: 640px)', {
            seriesBarDistance: 5,
            axisX: {
              labelInterpolationFnc: function (value) {
                return value[0];
              }
            }
          }]
        ];

        Chartist.Line('#chartActivity', data, options, responsiveOptions);

        var getStatus = $.get('1/get/summary/app_status');
        getStatus.done(function(results) {
            var data = JSON.parse(results)
            let getStatusList = []
            getStatusList.push(data.idle)
            getStatusList.push(data.crash)
            getStatusList.push(data.running)
            Chartist.Pie('#chartPreferences', {
                labels: getStatusList,
                series: getStatusList
            },{donut:true});
        }) /// end getStatus
    })
},
// End chartist function

    initGoogleMaps: function(){
        var myLatlng = new google.maps.LatLng(40.748817, -73.985428);
        var mapOptions = {
          zoom: 13,
          center: myLatlng,
          scrollwheel: false, //we disable de scroll over the map, it is a really annoing when you scroll through page
          styles: [{"featureType":"water","stylers":[{"saturation":43},{"lightness":-11},{"hue":"#0088ff"}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"hue":"#ff0000"},{"saturation":-100},{"lightness":99}]},{"featureType":"road","elementType":"geometry.stroke","stylers":[{"color":"#808080"},{"lightness":54}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ece2d9"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#ccdca1"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#767676"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]},{"featureType":"landscape.natural","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#b8cb93"}]},{"featureType":"poi.park","stylers":[{"visibility":"on"}]},{"featureType":"poi.sports_complex","stylers":[{"visibility":"on"}]},{"featureType":"poi.medical","stylers":[{"visibility":"on"}]},{"featureType":"poi.business","stylers":[{"visibility":"simplified"}]}]

        }
        var map = new google.maps.Map(document.getElementById("map"), mapOptions);

        var marker = new google.maps.Marker({
            position: myLatlng,
            title:"Hello World!"
        });

        // To add the marker to the map, call setMap();
        marker.setMap(map);
    },

	showNotification: function(from, align){
    	color = Math.floor((Math.random() * 4) + 1);

    	$.notify({
        	icon: "ti-comment",
        	message: "Message."

        },{
            type: type[color],
            timer: 4000,
            placement: {
                from: from,
                align: align
            }
        });
	},

}


/**
 * Global information about the dashboard
 * @TODO: Add socket handling ... it would make non-blocking updating information
 */

var g = {}
g.summary = {}
/**
 * Initializing the top section of the dashboard (apps and folders)
 */
g.summary.factory = function (url,pointer) {
    var object = {}
    object.url = url
    var observer = null
    var TIME_ELLAPSED = 2000 ;
    object.callback = function (r) {
        r = JSON.parse(r.responseText)
        pointer(r)

        //observer.notify()

    }
    object.init = function (observer) {
        observer = observer

        var httpclient = HttpClient.instance()
        //httpclient.setAsync(false)
        httpclient.get(this.url, this.callback)
        setTimeout(function(){
            observer.notify()
        },TIME_ELLAPSED) ;

        //observer.notify()
    }
    return object

}
