/*
 * File: app/controller/MapDataController.js
 *
 * This file was generated by Sencha Architect version 3.0.4.
 * http://www.sencha.com/products/architect/
 *
 * This file requires use of the Ext JS 4.2.x library, under independent license.
 * License of Sencha Architect does not include license for Ext JS 4.2.x. For more
 * details see http://www.sencha.com/license or contact license@sencha.com.
 *
 * This file will be auto-generated each and everytime you save your project.
 *
 * Do NOT hand edit this file.
 */

Ext.define('SSMeasureViewer.controller.MapDataController', {
    extend: 'Ext.app.Controller',

    refreshMapData: function(store) {
        this.clearLayers();

        var markerLayers = [];

        store.data.items.forEach(function(e) {
            //console.log(e);
            if(e.raw.latitude.length > 0 && parseFloat(e.raw.longitude) !== 0 && e.raw.value > -10 && e.raw.value < 45){
            var min = window.selectedmeasuremean - window.selectedmeasurestddev;
            var max = window.selectedmeasuremean + window.selectedmeasurestddev;
            var halfmax = (max-min)/2;
            var blue = Math.floor(Math.max(0, 255*(1 - (e.raw.value/halfmax))));
            var red  = Math.floor(Math.max(0, 255*((e.raw.value/halfmax) - 1)));
            var green = 255 - blue - red;
            var rgb = blue | (green << 8) | (red << 16);
            var strrgb = rgb.toString(16);
                while(strrgb.length < 6){
                    strrgb = '0'+strrgb;
                }
            strrgb = '#' + strrgb;
            //console.log(strrgb);
            //console.log('rgb('+red.toString()+','+green.toString()+','+blue.toString()+')');

           // L.mapbox.featureLayer({
            markerLayers.push({
            // this feature is in the GeoJSON format: see geojson.org
            // for the full specification
            type: 'Feature',
            geometry: {
                type: 'Point',
                // coordinates here are in longitude, latitude order because
                // x, y is the standard for GeoJSON and many formats
                coordinates: [
                  parseFloat(e.raw.longitude),
                  parseFloat(e.raw.latitude)
                ]
            },
            properties: {
                title: window.selectedmeasure.charAt(0).toUpperCase() + window.selectedmeasure.substring(1,100),
                description: e.raw.value.toFixed(1).toString()+window.selectedmeasuresufix,
                // one can customize markers by adding simplestyle properties
                // https://www.mapbox.com/foundations/an-open-platform/#simplestyle
                'marker-size': 'small',
                'marker-color': strrgb
                //text: e.raw.value.toFixed(1).toString()+window.selectedmeasuresufix
            }
            });

            //addressPoints.push(new L.LatLng(e.raw.latitude, e.raw.longitude));


            }


        });
           var featureLayer = L.mapbox.featureLayer({
                type: "FeatureCollection",
                features: markerLayers
                });


        window.featureLayer = featureLayer;
            map.addLayer(window.featureLayer);
        map.on('popupopen',function(e){
        });
            //var heat = L.heatLayer(addressPoints, {maxZoom: 18}).addTo(map);

    },

    requestData: function(measure, date) {
        var store = Ext.data.StoreManager.lookup('measureStore');

        store.load({
        params:{'measure': measure,
                'time': date}
        });
    },

    clearLayers: function() {
        window.map.eachLayer(function (layer) {
            if(!(layer.feature === undefined)){
                window.map.removeLayer(layer);
            }
        });

    },

    init: function(application) {
        window.selecteddate="2014-07-18 19:00:00";
        window.selectedmeasure="temperature";
        window.selectedmeasuresufix=" ºC";
        window.selectedmeasuremean=22.0;
        window.selectedmeasurestddev=27.0;
        console.log("launchh");
        console.log(window);
    }

});
