<?php
include_once 'server/config/config.php';
include_once 'server/lib/mod_dbm/package.dbm.php';

class Unit
{
    //put your code here

    private $machineId;
    private $dao;


    public function __construct(int $machineId)
    {
        $this->machineId = $machineId;
        $this->dao = new dbObject();
    }

    function getMileageOffset()
    {
        $q = sprintf("SELECT mileage_offset FROM machines WHERE machine_id = %d", $this->machineId);
        $res = $this->dao->fetchQuery($q);
        return (is_array($res) && count($res)) ? $res[0]['mileage_offset'] : 0;
    }

    function getHourOffset()
    {
        $q = sprintf("SELECT hour_offset FROM machines WHERE machine_id = %d", $this->machineId);
        $res = $this->dao->fetchQuery($q);
        return (is_array($res) && count($res)) ? $res[0]['hour_offset'] : 0;
    }

    public function getMaxAllowedSpeed()
    {
        $res = $this->dao->fetchQuery(sprintf("SELECT max_allowed_speed FROM machines WHERE machine_id = %d", $this->machineId));
        if (is_array($res) && count($res)) {
            $maxAllowedSpeed = $res[0]['max_allowed_speed'];
        } else {
            $maxAllowedSpeed = 1000;
        }
        return $maxAllowedSpeed;
    }

    public function getUnitData($user)
    {
        if ($user->hasMachine($this->machineId)) {
            $query = sprintf("SELECT  m.machine_id, m.machine_name as unit_name, m.id_combustible as tipo_combustible, "
                . "               m.metrica, m.current_mileage, m.current_hours, m.mileage_offset, m.hour_offset, "
                . "               m.consumo_esperado as kilometraje FROM machines m "
                . "               WHERE m.machine_id = %d", $this->machineId);
            $res = $this->dao->fetchQuery($query);
        } else {
            $res = null;
        }
        return $res;
    }

    public function getTotalMileage()
    {
        $q = sprintf("SELECT current_mileage FROM machines WHERE machine_id=%d", $this->machineId);
        $res = $this->dao->fetchQuery($q);
        if (is_array($res) && count($res)) {
            $totalMileage = $res[0]['current_mileage'];
        } else {
            $totalMileage = null;
        }
        return $totalMileage;
    }

    public function calculateKmOffset($actualMileage)
    {
        $gpsMileage = $this->getTotalMileage();
        $offset = ($gpsMileage === null) ? 0 : $actualMileage - $gpsMileage;
        return $offset;
    }

    public function getTotalHours()
    {
        $res = $this->dao->fetchQuery(sprintf("SELECT current_hours FROM machines m WHERE m.machine_id=%d", $this->machineId));
        if (is_array($res) && count($res)) {
            $totalHours = $res[0]['current_hours'];
        } else {
            $totalHours = null;
        }
        return $totalHours;
    }

    public function calculateHourOffset($actualHours)
    {
        $gpsHours = $this->getTotalHours();
        $offset = ($gpsHours === null) ? 0 : $actualHours - $gpsHours;
        return $offset;
    }

    // public function setUnitData($nombreComun, $matricula, $idModelo, $velMax, $costoHr, $costoKm, $costosMoneda, $actualMileage,$actualHours){
    //     $kmOffset = $this->calculateKmOffset($actualMileage);
    //     $hOffset = $this->calculateHourOffset($actualHours);
    //     $q = sprintf("UPDATE units SET nombre_comun='%s', matricula='%s', id_modelo='%d', max_allowed_speed='%d', costo_hr='%s', costo_km='%s', costo_moneda='%d', km_offset=%s, hour_offset=%s WHERE (unit_id='%s');", $nombreComun, $matricula, $idModelo, $velMax, $costoHr, $costoKm, $costosMoneda, $kmOffset, $hOffset, $this->unitId);
    //     return ($this->dao->execute($q)===1);
    // }
    public function setUnitData($nombreComun, $matricula, $idModelo, $velMax, $costoHr, $costoKm, $costosMoneda, $actualMileage, $actualHours, $consumoEsperado, $metrica, $notiMask, $idCombustible, $icono)
    {

        $cHr = ($costoHr) ? $costoHr : 0;
        $cKm = ($costoKm) ? $costoKm : 0;
        $vMax = ($velMax) ? $velMax : 0;
        $cEsperado = ($consumoEsperado) ? $consumoEsperado : 0;
        $cMoneda = ($costosMoneda) ? $costosMoneda : 1;
        $actMileage = ($actualMileage) ? $actualMileage : 0;
        $actHours = ($actualHours) ? $actualHours : 0;
        $notiMask = ($notiMask) ? $notiMask : 0;
        $icono = ($icono) ? $icono : 0;

        $kmOffset = $this->calculateKmOffset($actMileage);
        $hOffset = $this->calculateHourOffset($actHours);
        $q = sprintf("UPDATE machines SET machine_name='%s', machine_model_id=%d, matricula='%s', max_allowed_speed=%d, "
            . "costo_hr='%s', costo_km='%s', costo_moneda=%d, mileage_offset='%s', hour_offset='%s',"
            . "consumo_esperado=%s, metrica=%d, notification_mask=%d, id_combustible=%d, icon_id=%d "
            . "WHERE (machine_id='%d');", $nombreComun, $idModelo, $matricula, $vMax, $cHr, $cKm, $cMoneda, $kmOffset, $hOffset, $cEsperado, $metrica, $notiMask, $idCombustible, $icono, $this->machineId);
        return ($this->dao->execute($q) === 1);
    }

    public function getCostoKm()
    {
        $q = sprintf("SELECT costo_km FROM machines WHERE machine_id = %d", $this->machineId);
        $res = $this->dao->fetchQuery($q);
        if (is_array($res) && count($res)) {
            $costo = $res[0]['costo_km'];
        } else {
            $costo = 0;
        }
        return $costo;
    }

    public function getCostoHr()
    {
        $q = sprintf("SELECT costo_hr FROM machines WHERE machine_id = %d", $this->machineId);
        $res = $this->dao->fetchQuery($q);
        if (is_array($res) && count($res)) {
            $costo = $res[0]['costo_hr'];
        } else {
            $costo = 0;
        }
        return $costo;
    }
}
