var hours = 24;
var granularity = '5m';
var end = 0;
var retention = 'monthly';
var graphdata = "https://bastart.spoton.cz/data/solar_monitor?range=24h&granularity=5m&end=0h&retention=monthly";
sol = new Dygraph(
document.getElementById("solar"),
graphdata
,{
    axes : {
        x : {
            drawGrid: true,
            drawAxis : true
        },
        y : {
            drawGrid: false,
            drawAxis: true,
            valueRange: [44.5,55]
        },
        y2 : {
            drawGrid: false,
            drawAxis: true,
            independentTicks: true,
            customBars: true,
            valueRange: [0,1300]
        }
    },
    rollPeriod: 3,
    interactionModel: {},
    connectSeparatedPoints: true,
    series:{
        'P_solar': {
            axis: 'y2',
            color: '#ff5500'
        },
        'V_array': {
            axis: 'y',
            color: '#666'
        }
    },
        ylabel: '<span style="color:#666;">Battery [V]</span>',
        y2label: '<span style="color:#ff2200;">Power [W]</span>',
        labelsDiv: 'solar_labels',
        legend: 'always',
        customBars: true
    }
);


function refreshGraph(){
  graphdata = "https://bastart.spoton.cz/data/solar_monitor?range=" + hours + "h&granularity=" + granularity + "&end=" + end + "h&retention=" + retention;
  sol.updateOptions({'file': graphdata});
}

function setHours(hours_to_set){
  hours = hours_to_set;
  switch(hours){
      case '1':
        granularity = '10s';
        retention = 'monthly';
        break;
      case '6':
        granularity = '10s';
        retention = 'monthly';
        break;
      case '12':
        granularity = '2m';
        retention = 'monthly';
        break;
      case '24':
        granularity = '5m';
        retention = 'monthly';
        break;
      case '168':
        granularity = '15m';
        retention = 'monthly';
        break;
      case '720':
        granularity = '3h';
        retention = 'yearly';
        break;
      case '8760':
        granularity = '6h';
        retention = 'yearly';
        break;
      case '87600':
        granularity = '24h';
        retention = 'yearly';
        break;
      default:
        granularity = '5m';
        retention = 'monthly';
  }
  end = 0;
  //document.getElementById('xxx').innerHTML = graphdata;
  refreshGraph();
}

function setBack(){
    // range=1h -> range=2h&end=1h
    disp_range = hours*1 - end*1;
    hours = hours*1 + disp_range;
    end = end*1 + disp_range;
    //document.getElementById('xxx').innerHTML = graphdata;
    refreshGraph();
}

function setForth(){
    disp_range = hours*1 - end*1;
    hours = hours*1 - disp_range;
    if(hours < disp_range){
        hours = disp_range;
    }
    end = end*1 - disp_range;
    if(end < 0){
        end = 0;
    }
    //document.getElementById('xxx').innerHTML = graphdata;
    refreshGraph();
}

function getPageContents(callback,url,params) {
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey
        xmlhttp=new XMLHttpRequest();
    }
    else{
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(params!=null) {
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    } else {
        xmlhttp.open("GET", url, true);
    }
    xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            callback(xmlhttp.responseText);
        }
    }
    xmlhttp.send(params);
}

counter = 0;
function refreshValues(){
    fresh_vals_url = '/data/solar_realtime_data?type=json'
    getPageContents(function(result){freshVals=JSON.parse(result);},
                     fresh_vals_url)
    document.getElementById('timestamp').innerHTML = freshVals.time;                 
    document.getElementById('array_voltage').innerHTML = freshVals.V_array;
    document.getElementById('array_percent').innerHTML = freshVals.perc_array;
    document.getElementById('charge_current').innerHTML = freshVals.ChCurr;
    document.getElementById('solar_power').innerHTML = freshVals.Psol;
    document.getElementById('pmax_day').innerHTML = freshVals.Pmax_day;
    counter = counter + 5000;
    if(counter >= 360000){
        refreshGraph();
        document.getElementById('graph_timestamp').innerHTML = freshVals.time;        
        counter = 0;
    }
}



var intervalVal = setInterval(refreshValues, 5000);
