<?php
// Author: Sergi Diaz Planas
/*
Functions responible for translating the client's messages in understandable requests are
packed here, as well as formatting answers to comply to a defined format. The idea is to make 
the server logic independent to the format of the messages exchanged between client and server.
*/

class Protocol
{
    // Names for the parameters in a request
    public static $requestName = "REQ";
    public static $imageName   = "IMG";
    public static $dataName    = "ADD";

    // Request types
    public static $referenceRequest  = "REF";
    public static $verificationRequest = "VAL";


    // Default error message
    public static $defaultErrorMsg = array("ANS" => "FALSE", "MSG" => "UNDEFINED_ERROR");


    // Retrieve the information from a data array and return it in a way that its understandable for 
    // the server logic.
    public static function getRectangles($array)
    {
        /*
            The protocol defines the format of the data as:

                PX, PY, PW, PH, DX, DY, DW, DH, NX, NY, NW, NH
        
            where P* are the (x,y) origin, the width and the height of the photo rectangle,
            D* are the same parameters for the DNI number rectangle and N* those for the names
            rectangle.
        */

        if(!$array) { return false; } 

        $photo_rectangle = array("x" => -1, "y" => -1, "w" => -1, "h" => -1);
        $dni_rectangle = array("x" => -1, "y" => -1, "w" => -1, "h" => -1);
        $names_rectangle = array("x" => -1, "y" => -1, "w" => -1, "h" => -1); 
        $surnames_rectangle = array("x" => -1, "y" => -1, "w" => -1, "h" => -1);

        // Try to read data, return false if data does not comply to the format.
        if(!$array["PX"] && $array["PX"] != 0) { print "px "; return false; } else { $photo_rectangle["x"] = $array["PX"]; }
        if(!$array["PY"] && $array["PY"] != 0) { print "py "; return false; } else { $photo_rectangle["y"] = $array["PY"]; }
        if(!$array["PW"] && $array["PW"] != 0) { print "pw "; return false; } else { $photo_rectangle["w"] = $array["PW"]; }
        if(!$array["PH"] && $array["PH"] != 0) { print "ph "; return false; } else { $photo_rectangle["h"] = $array["PH"]; }
        if(!$array["DX"] && $array["DX"] != 0) { print "dx "; return false; } else { $dni_rectangle["x"] = $array["DX"]; }
        if(!$array["DY"] && $array["DY"] != 0) { print "dy "; return false; } else { $dni_rectangle["y"] = $array["DY"]; }
        if(!$array["DW"] && $array["DW"] != 0) { print "dw "; return false; } else { $dni_rectangle["w"] = $array["DW"]; }
        if(!$array["DH"] && $array["DH"] != 0) { print "dh "; return false; } else { $dni_rectangle["h"] = $array["DH"]; }
        if(!$array["NX"] && $array["NX"] != 0) { print "nx "; return false; } else { $names_rectangle["x"] = $array["NX"]; }
        if(!$array["NY"] && $array["NY"] != 0) { print "ny "; return false; } else { $names_rectangle["y"] = $array["NY"]; }
        if(!$array["NW"] && $array["NW"] != 0) { print "nw "; return false; } else { $names_rectangle["w"] = $array["NW"]; }
        if(!$array["NH"] && $array["NH"] != 0) { print "nh "; return false; } else { $names_rectangle["h"] = $array["NH"]; }
        if(!$array["SX"] && $array["SX"] != 0) { print "sx "; return false; } else { $names_rectangle["x"] = $array["SX"]; }
        if(!$array["SY"] && $array["SY"] != 0) { print "sy "; return false; } else { $names_rectangle["y"] = $array["SY"]; }
        if(!$array["SW"] && $array["SW"] != 0) { print "sw "; return false; } else { $names_rectangle["w"] = $array["SW"]; }
        if(!$array["SH"] && $array["SH"] != 0) { print "sh "; return false; } else { $names_rectangle["h"] = $array["SH"]; }

        $rectangles = array("photo" => $photo_rectangle,
                            "dni" => $dni_rectangle,
                            "names" => $names_rectangle,
                            "surnames" => $surnames_rectangle);
        return $rectangles;
    }

    // Return an error sequence containing the given message
    public static function errorMessage($message)
    {
        $errorMessage = Protocol::$defaultErrorMsg;
        $errorMessage["MSG"] = $message;

        return $errorMessage;
    }

    // Return a response to a successful reference request containing the given data
    public static function responseToReference($idNumber, $names, $surnames)
    {
        // Default array
        $response = array("ANS" => "TRUE", "DNI" => $idNumber, "Name1" => "", "Name2" => "", "Name3" => "");

        if ($names[0]) { $response["Name1"] = $names; }
        if ($names[1]) { $response["Name2"] = $surnames; }
        if ($names[2]) { $response["Name3"] = ""; }
        
        return $response;
    }

    // Return a response to a successful verification request 
    public static function responseToVerification()
    {
        return array("ANS" => "TRUE");
    }
}

?>