<?php

include_once "server/lib/mod_dbm/package.dbm.php";
include_once "server/class/PositionsManager.php";

Class kml {

    private $machineId;
    private $from;
    private $to;
    private $startIcon;
    private $endIcon;
    private $icon;
    private $lowSpeedColor;
    private $midSpeedColor;
    private $highSpeedColor;
    private $lowSpeedLimit;
    private $middleSpeedLimit;
    private $highSpeedLimit;
    private $points;
    private $unitName;
    private $coordinates;
    
    public $color = "FFF9B30F";
    public $width = 10;
    
    public function __construct() {
        $this->dao = new dbObject();
    }

    public function loadDataset($user) {
        $pm = new PositionsManager($this->machineId,$user);
        $this->points = $pm->getPositionsLog($this->from, $this->to);
    }
    
    public function render() {     
        $kmlContent = sprintf('<?xml version="1.0" encoding="UTF-8"?>
        <kml xmlns="http://www.opengis.net/kml/2.2">
        <Document>
            <Style id="ricon">
            <IconStyle>
               <scale>1.0</scale>
               <hotSpot x="0.5" y="1" xunits="fraction" yunits="fraction" />
               <Icon>
                <href>http://gps.protracking.com.uy/img/ico/kml/stMarkIcon.png</href>
               </Icon>
            </IconStyle>
            </Style>
            %s
            %s
         </Document>
         </kml>
                ', $this->renderPlaceMarks($this->points), $this->renderPolyline());

        return $kmlContent;
    }
    
    
    
    private function getNameTagContent(){
        return "Archivo de exportación de recorridos de Protracking";
    }
    
    private function renderPlaceMarks($points) {
        $placemarks = "";
        $coordinates = "";
        if (is_array($points) && count($points)) {
            foreach ($points['nodes'] as $point) {

                $id = $point['id'];
                //$subMsg = $point['subMsg'];
                $timestamp = $point['ts'];
                $lat = $point['lat'];
                $lng = $point['lng'];
                $speed = $point['spd'];
                $az = $point['az'];
                //$hr = $point['hr'];
                //$mil = $point['mil'];
                $motionStatus = $point['motion_status'];
                
                $placemarks .= $this->renderPlacemark($this->getName($point), $this->getDescription($point), $lat, $lng, 0, $speed, $az,$motionStatus,$timestamp,$id);
                $coordinates .= sprintf("\t%s,%s\n",$lng,$lat);
                
            }
        }
        $this->coordinates = $coordinates;
        
        return $placemarks;
    }
    
    function renderPolyline(){
       
       return sprintf("<Placemark><name>Recorrido</name><Style><LineStyle><color>%s</color><width>%d</width></LineStyle></Style><LineString><coordinates>%s</coordinates></LineString></Placemark>",$this->color,$this->width,$this->coordinates);
    }

    function getName($point) {
        return sprintf("#Id: %d", $point['id']);
    }

    function getDescription() {
        // Implementa el código necesario para obtener la descripción del punto.
        // Este método debe retornar un string.
        return ""; //sprintf("<description>Unidad: %s desde %s hasta %s</description>", $this->unitName, date("d/m/y h:i", $this->from), date("d/m/y h:i", $this->to));
    }

    private function renderPlaceMark($name, $description, $lat, $lng, $altitude, $spd, $az,$motionStatus,$timestamp) {
        $pMark = sprintf('
                <Placemark>
                    <name></name>
                    <description>%s (%s,%s)</description>
                    <styleUrl>#ricon</styleUrl>
                    <Point>
                        <coordinates>%s,%s,0</coordinates>
                    </Point>
                </Placemark>',  date("d/m H:i",$timestamp), $lng, $lat,$lng, $lat);
                        //', $name, $lng, $lat, $altitude, "<StyleUrl>regularIcon</StyleUrl>", $spd, $this->getEstadoDinamico($motionStatus),$az,date("H:i:s",$timestamp));
        return $pMark;
    }
    
    /*
     *                             <ExtendedData>
                                <Data name="velocidad">
                                    <value>%s</value>
                                </Data>
                                <Data name="Estado dinámico">
                                    <value>%s</value>
                                </Data>
                                <Data name="rumbo">
                                    <value>%s</value>
                                </Data>
                                <Data name="Hora">
                                    <value>%s</value>
                                </Data>
                            </ExtendedData>
     */

    private function getEstadoDinamico($motionStatus){
        return $motionStatus;
    }
    
    private function getStyle($spd, $az) {
        return "";
    }

    public function getMachineId() {
        return $this->machineId;
    }

    public function setMachineId($machineId) {
        $this->machineId = $machineId;
    }

    public function getFrom() {
        return $this->from;
    }

    public function setFrom($from) {
        $this->from = $from;
    }

    public function getTo() {
        return $this->to;
    }

    public function setTo($to) {
        $this->to = $to;
    }

    public function getStartIcon() {
        return $this->startIcon;
    }

    public function setStartIcon($startIcon) {
        $this->startIcon = $startIcon;
    }

    public function getEndIcon() {
        return $this->endIcon;
    }

    public function setEndIcon($endIcon) {
        $this->endIcon = $endIcon;
    }

    public function getIcon() {
        return $this->icon;
    }

    public function setIcon($icon) {
        $this->icon = $icon;
    }

    public function getLowSpeedColor() {
        return $this->lowSpeedColor;
    }

    public function setLowSpeedColor($lowSpeedColor) {
        $this->lowSpeedColor = $lowSpeedColor;
    }

    public function getMidSpeedColor() {
        return $this->midSpeedColor;
    }

    public function setMidSpeedColor($midSpeedColor) {
        $this->midSpeedColor = $midSpeedColor;
    }

    public function getHighSpeedColor() {
        return $this->highSpeedColor;
    }

    public function setHighSpeedColor($highSpeedColor) {
        $this->highSpeedColor = $highSpeedColor;
    }

    public function getLowSpeedLimit() {
        return $this->lowSpeedLimit;
    }

    public function setLowSpeedLimit($lowSpeedLimit) {
        $this->lowSpeedLimit = $lowSpeedLimit;
    }

    public function getMiddleSpeedLimit() {
        return $this->middleSpeedLimit;
    }

    public function setMiddleSpeedLimit($middleSpeedLimit) {
        $this->middleSpeedLimit = $middleSpeedLimit;
    }

    public function getHighSpeedLimit() {
        return $this->highSpeedLimit;
    }

    public function setHighSpeedLimit($highSpeedLimit) {
        $this->highSpeedLimit = $highSpeedLimit;
    }
    
    public function setUnitName($unitName){
        $this->unitName = $unitName;
    }

}
