<?php
include_once("server/class/User.php");

class AlertsManager{

    static function saveAlert($alertData) {
        $resultAlert = false;
        $dao = new dbObject();
        if (!isset($alertData['alert_id'])) {
            $q = sprintf("INSERT INTO alerts (alert_type_id, id_organizacion, popup, email, telefono, enviar_email, enviar_mensaje_cel) values ('%s',%d,%d,'%s','%s',%d,%d)",
                    $alertData['alert_type_id'], $alertData['org_id'], $alertData['notiPopup'], $alertData['emails'], $alertData['telefonos'], $alertData['notiEmail'], $alertData['notiTelefono']);
            $dao->execute($q);
            $key = $dao->getAutoInc();
            if ($key) {
                
                AlertsManager::saveAlertUnits($key, $alertData['unidades']);

                switch ($alertData['alert_type_id']) {
                    case 'GEO':
                        $resultAlert = AlertsManager::saveAlertGeo($key, $alertData['areaId'], $alertData['entrada'], $alertData['salida']);
                        break;
                    case 'SPD':
                        $resultAlert = AlertsManager::saveAlertExce($key, $alertData['velocidad'], $alertData['periodo']);
                        break;
                    case 'VAN':
                        $resultAlert = true;
                        break;
                    case 'VOL':
                        $resultAlert = AlertsManager::saveAlertVol($key, $alertData['minimo'], $alertData['maximo']);
                        break;
                    case 'SOS':
                        $resultAlert = true;
                        break;
                    case 'REM':
                        $resultAlert = true;
                        break;
                    default:
                        break;
                }
            }
        } else {
            $q = sprintf("UPDATE alerts set alert_type_id = '%s', id_organizacion=%d, popup=%d, email='%s', telefono='%s', enviar_email=%d, enviar_mensaje_cel=%d WHERE alert_id = %d", $alertData['alert_type_id'], $alertData['org_id'], $alertData['notiPopup'], $alertData['emails'], $alertData['telefonos'], $alertData['notiEmail'], $alertData['notiTelefono'], $alertData['alert_id']);
            $resultAlert = $dao->execute($q);
            // if($dao->execute($q)){
            //    $result = AreasManager::saveAreaCoords($alertData['areaId'], $dao, $alertData['coords']); 
            // }
        }
        return $resultAlert;
    }

    static function saveAlertGeo($alert_id, $area_id, $entrada, $salida){
        $dao = new dbObject();
        $q = sprintf("INSERT INTO alert_geo (alert_id, id_area, entrada, salida, activa) VALUES (%d, %d, %d, %d, %d)", $alert_id, $area_id, $entrada, $salida, 1);
        return $dao->execute($q);
    }

    static function saveAlertExce($alert_id, $velocidad, $periodo){
        $dao = new dbObject();
        $q = sprintf("INSERT INTO alert_speed (alert_id, speed_threshold, periodo, activa) VALUES (%d, %d, %d, %d)", $alert_id, $velocidad, $periodo, 1);
        return $dao->execute($q);
    }

    static function saveAlertVol($alert_id, $minimo, $maximo){
        $dao = new dbObject();
        $q = sprintf("INSERT INTO alert_battery (alert_id, min, max, activa) VALUES (%d, %d, %d, %d)", $alert_id, $minimo, $maximo, 1);
        return $dao->execute($q);
    }

    static function saveAlertUnits($alert_id, $unit_id){
        for ($i=0; $i < count($unit_id); $i++) { 
            $dao = new dbObject();
            $q = sprintf("INSERT INTO alerts_units (alert_id, unit_id) VALUES (%d, '%s')", $alert_id, $unit_id[$i]);
            $dao->execute($q);
        }

    }


}


?>