<?php
session_start();

if (isset($_POST["lang"])) {
    $allowed_langs = ["en", "fr", "hr", "es", "hu"];

    if (in_array($_POST["lang"], $allowed_langs)) {
        $_SESSION["lang"] = $_POST["lang"];
    } else {
        $_SESSION["lang"] = "en"; 
    }
}

include "Connection.php";
include "Korisnik.php"; 

$db = new Database(); 
$con =  $db->connect();

$korisnik = new Korisnik($con); 

if(isset($_POST["flag"])){
    if($_POST["flag"] == 1){ // LOG IN
        $id = $korisnik->login($_POST);
        $id > 0 ? $_SESSION['id_user'] = $id : false; 
        echo $id;
    }else if($_POST["flag"] == 2){ // REGISTER
        $id = $korisnik->register($_POST);
        $id > 0 ? $_SESSION['id_user'] = $id : false; 
        echo $id;
    }else if($_POST["flag"] == 3){  
        $data = $korisnik->readProfile($_POST["id"]);
        echo json_encode($data); 
    }else if($_POST["flag"] == 4){
        $check_id = $korisnik->changePassword($_POST, $_SESSION["id_user"]);
        echo $check_id;
    }else if($_POST["flag"] == 5){ // Check if session is active //(HR) Provjera postoji li sesija
        $_SESSION["id_user"]= $_POST["id"];
        echo $_SESSION["id_user"];
    }
    else{
        echo "400"; 
    }
}else{
    echo "404"; 
}

$db->close($con);
?>