<?php

include_once 'server/lib/mod_dbm/package.dbm.php';
/*
 * ROMAN DIGITAL - Autor: Américo Patetta
 */

/**
 * Description of HeatmapManager
 *
 * @author pc
 */
class HeatmapManager {
    
    public $TIMEPAD = 300;
    
    
    
    public function getHeatmap($inicio, $fin, $machineId,$n=0,$s=0,$w=0,$e=0) {

        
        
        
        $geoConstraint = ($n || $s || $w || $e)?sprintf(" AND ((lat <= %s) AND (lat>=%s) AND (lng>=%s) AND (lng <=%s)) ",$n,$s,$w,$e):"";

        $dao = new dbObject();

        // Obteniendo el conjunto P      
        $pQuery = sprintf("SELECT DISTINCT gps_timestamp,lat, lng from log_positions WHERE (gps_timestamp >= %d AND gps_timestamp <= %d) AND machine_id = %d %s ORDER BY gps_timestamp", $inicio, $fin, $machineId,$geoConstraint);
        $P = $dao->fetchQuery($pQuery);
        
        if ($P && is_array($P) && count($P)) {
            
            // 
            //$inicio = ($geoConstraint)?max($P[0]['gps_timestamp']-$this->TIMEPAD,$inicio):$inicio;              //Orig: 1664334000     //Calc: 1664359206      //P: 1664365861     
            //$fin = ($geoConstraint)?min($P[count($P)-1]['gps_timestamp']+$this->TIMEPAD,$fin):$fin;                                 //Calc: 1664359206      //Orig: 1664420400
            
            $inicio = ($geoConstraint)?max($P[0]['gps_timestamp']-$this->TIMEPAD,$inicio):$inicio;              //Orig: 1664334000     //Calc: 1664359206      //P: 1664365861     
            $fin = ($geoConstraint)?min($P[count($P)-1]['gps_timestamp']+$this->TIMEPAD,$fin):$fin;                                 //Calc: 1664359206      //Orig: 1664420400
            
            
            for ($i = 0; $i < count($P); $i++) {
                $ti = ($i == 0) ? $inicio : ($P[$i]['gps_timestamp'] - ($P[$i]['gps_timestamp'] - $P[$i - 1]['gps_timestamp']) / 2);
                $tf = ($i == count($P) - 1) ? $fin : ($P[$i]['gps_timestamp'] + ($P[$i + 1]['gps_timestamp'] - $P[$i]['gps_timestamp']) / 2);
                // Obteniendo el subConjunto H' de H
                $hQuery = sprintf("SELECT COUNT(DISTINCT s.stem_id) as stems, AVG(log_length) as largo_medio, SUM(log_length) as height, MIN(dia_top_ob) as dia_min, MAX(dia_butt_ob) as dia_max, SUM(vol_price) as tot_price, SUM(vol_sub) as tot_sub, SUM(vol_sob) as tot_sob 
                                   FROM stems s JOIN logs l ON s.stem_id = l.stem_id WHERE (s.harvest_date >= %d AND s.harvest_date %s %d) AND s.machine_id = %d", $ti, ($i==count($P)-1)?"<=":"<", $tf, $machineId);
                $Hprima = $dao->fetchQuery($hQuery);
                if(is_array($Hprima) && count($Hprima)){
                    if($Hprima[0]['stems']){
                        $P[$i]['totPrice'] = $Hprima[0]['tot_price'];
                        $P[$i]['totSub'] = $Hprima[0]['tot_sub'];
                        $P[$i]['totSob'] = $Hprima[0]['tot_sob'];
                        $P[$i]['stems'] = ($Hprima[0]['stems'] != null)?$Hprima[0]['stems']:0;
                        $P[$i]['largoMedio'] = $Hprima[0]['largo_medio'];
                        $P[$i]['height'] = $Hprima[0]['height'];
                        $P[$i]['diaMin'] = $Hprima[0]['dia_min'];
                        $P[$i]['diaMax'] = $Hprima[0]['dia_max'];
                    } else {
                        $P[$i]['totPrice'] = 0;
                        $P[$i]['totSub'] = 0;
                        $P[$i]['totSob'] = 0;
                        $P[$i]['stems'] = 0;
                        $P[$i]['largoMedio'] = 0;
                        $P[$i]['height'] = 0;
                        $P[$i]['diaMin'] = 0;
                        $P[$i]['diaMax'] = 0;
                    }
                }
            }
        }
        $Pprima=array();
        if(is_array($P)){
            for($i=1;$i<(count($P)-1);$i++){
                if($P[$i]['stems']){
                    $Pprima[]=$P[$i];
                }
            }
        }
        
        return $Pprima;
    }
}
