<?php

$states = explode(',',"RS,SC,PR,SP,RJ,ES,MG,MS,MT,GO,DF,AM,PA,TO,RO,RR,AC,AP,BA,PI,MA,RN,PB,PE,AL,SE,CE");
$total = explode(',', "16422,19178,19959,128464,77600,15153,95758,15578,14048,24351,5406,20217,19245,13258,10638,9772,5520,8772,44681,10120,24948,3892,12616,18828,17448,14424,14214");
$totalSum = 680510;
$row = 1;
$time = (isset($_GET['h']) && isset($_GET['m'])) ? $_GET['h'].":".$_GET['m'] : date("H:i");

$timeArray = explode(':',$time);
$h = $timeArray[0];
$m = $timeArray[1];
//$handleC1 = fopen ("c1.csv","r");
$fileC1 = new SplFileObject('c1.csv');
$fileC2 = new SplFileObject('c2.csv');
$fileC3 = new SplFileObject('c3.csv');

$multiplier = intval($h) - 14;
$line = (60 * $multiplier + intval($m)) + 1;
$fileC1->seek($line);
$fileC2->seek($line);
$fileC3->seek($line);

$c1 = explode(',',$fileC1->current());
$c2 = explode(',',$fileC2->current());
$c3 = explode(',',$fileC3->current());

$ret = new stdClass();
$ret->states = array();
$ret->country = array();
$c1Sum = 0;
$c2Sum = 0;
$c3Sum = 0;
$totalSum = 0;
foreach ($states as $k => $state) {
	$ret->states[$state] = array();
	$ret->states[$state]['c1'] = $c1[$k+1];
	$ret->states[$state]['c2'] = $c2[$k+1];
	$ret->states[$state]['c3'] = $c3[$k+1];
	$ret->states[$state]['read'] = $c1[$k+1] + $c2[$k+1] + $c3[$k+1];
	$ret->states[$state]['total'] = $total[$k];
	$c1Sum += $c1[$k+1];
	$c2Sum += $c2[$k+1];
	$c3Sum += $c3[$k+1];
}
$ret->country['c1'] = $c1Sum;
$ret->country['c2'] = $c2Sum;
$ret->country['c3'] = $c3Sum;
$ret->country['read'] = $c1Sum + $c2Sum + $c3Sum;
$ret->country['total'] = $totalSum;

debug($ret);

function debug ($var) {
	echo "<pre>".print_r($var)."</pre>";
}