<?php
include_once("server/class/GeoUtils.php");
include_once("server/lib/mod_dbm/package.dbm.php");

/**
 * Description of Delivery
 *
 * @author pc
 */
class CoordsManager {

    private $dao;
            
    public function __construct() {
        $this->dao = new dbObject();
    }
    
    public function addPosition($deliveryId,$lat,$lng,$timestamp,$speed,$azimuth){
        
        $statement = $this->dao->connection->prepare("INSERT INTO delivery_positions_log (delivery_id, lat, lng, gps_timestamp, speed, azimuth) VALUES (?,?,?,?,?,?);");
        $statement->bind_param("sddiii",$deliveryId,$lat,$lng,$timestamp,$speed,$azimuth);
        $statement->execute();
       
        return $statement->affected_rows;
    }
    
    public function getIdDeliveryFromToken($token){
        
        $statement = $this->dao->connection->prepare("SELECT id_delivery FROM deliverys WHERE current_token = ?");
        $statement->bind_param("s", $token);
        $statement->execute();
        
        $result = $statement->get_result();
        if($d = $result->fetch_array(MYSQLI_BOTH)){
            $this->idDelivery = $d['id_delivery'];
        } else {
            $this->idDelivery = -1;
        }
        return $this->idDelivery;
    }
    
    public function loginLocal($username,$idLocal){
            
    }
    
    public function updateToken($token){

        $statement = $this->dao->connection->prepare("UPDATE deliverys SET current_token = ? WHERE id_delivery = ?");
        $statement->bind_param("ss",$token, $this->idDelivery);
        $statement->execute();
    }
}
