<?php
include("../module/database.php");
$bdd = Database::getInstance("localhost", "root", "", "web");
$type = $_GET['type'];

if ($type == "tryConnect") {

    $request = $bdd->Query("Select * from accounts");
    $existAccount = false;
    foreach ($request as $row) {
        if ($row['email'] == $_GET['email'] && $row['password'] == $_GET['mdp']) {
            $existAccount = true;
        }
    }

    if ($existAccount)
        echo "loggingOk";
    else
        echo "loggingError";
}

if ($type == "addSessionAccount") {
    session_start();
    $request = $bdd->Query("Select * from accounts");
    $account = null;
    foreach ($request as $row) {
        if ($row['email'] == $_GET['email'] && $row['password'] == $_GET['mdp']) {
            $account = $row;
        }
    }


    $info = $account['pseudo'] . ';' . $account['email'] . ';' . $account['password'] . ';' . $account['ogrines'].";".$account['cadeaux'].";".$account['jetons'].";".$account['votes'];
    $_SESSION['accountInformation'] = $info;

    echo $info;
}


if ($type == "disconnect") {
    session_start();
    $_SESSION = array();
    session_destroy();
}

if ($type == "getPseudoAvailable") {
    $request = $bdd->Query("Select * from accounts");
    $account = null;
    foreach ($request as $row) {
        if ($row['pseudo'] == $_GET['pseudo']) {
            $account = $row;
        }
    }
    if ($account == null)
        echo "true";
    else
        echo "false";
}
if ($type == "getUnusedMail") {
    $request = $bdd->Query("Select * from accounts");
    $account = null;
    foreach ($request as $row) {
        if ($row['email'] == $_GET['email']) {
            $account = $row;
        }
    }
    if ($account == null)
        echo "true";
    else
        echo "false";
}


if($type == "addAccount")
{
    $xd = "'0','0','0','0'";
    $query ='INSERT INTO `accounts` (`pseudo`, `email`, `password`,`ogrines`,`cadeaux`,`jetons`,`votes`) VALUES ('."'".$_GET['pseudo']."',"."'".$_GET['mail']."', '".$_GET['mdp']."', '"."0"."', '"."0"."', '"."0"."', '"."0"."'".     ')';

    $bdd->Query($query);
}
?>