<?php
/**
 * Created by PhpStorm.
 * User: freuddebian
 * Date: 09/10/18
 * Time: 12:21
 */
require_once 'database/DataBaseConnector.php';
$params=$_POST["param"];
if(isset($params)){
    switch ($params){
        case "UpdateInformations":
            generateHyperVisor();
            break;
        case "listnetwork":
            listnetwork();
            break;
        case "insertnetwork":
            insertNetwork();
            break;
        case "listagentnetwork":
            listagentNetwork();
            break;
        case "listcountry":
            listcountry();
            break;
        case "listcodegenerer":
            listcode();
            break;
        case "listusers":
            listUsers();
            break;
        case "listoldnetworks":
            listOldNetwork();
            break;
        case "listnumero":
            listPhonenum();
            break;
        case "testquery":
            testQuery();
            break;
        case "testphone":
            testPhoneNumber();
            break;
    }

}else{
    echo "no param";
    var_dump($_POST);
            generateHyperVisor();

}
function testPhoneNumber(){
    $phone=$_POST["phone"];
    $messenger=new Messenger();

    try {
        $phone_number = $messenger->client->lookups->v1->phoneNumbers($phone)
            ->fetch();
        echo json_encode($phone_number);
    } catch(Exception $ex){
        echo json_encode($ex);
    }
}
function testQuery(){
    $con = mysqli_connect(DB_HOST, DB_USER
        , DB_PASSWORD, DB_DATABASE);
    $page=($_POST['page']-1)*50;
    $res= mysqli_query($con,"SELECT ag.longitude as longitude,
ag.adresse,
 ag.latitude as latitude,na.transactionNumber as phoneTransaction,
 ag.firstname as firstname,ag.lastname as lastname, ag.email as email,na.phone as phone,cg.code_membre as code_membre,
 nt.name as network,ct.name as country, na.etat as etat
  FROM agents ag INNER JOIN networks_agents na ON na.agent_id=ag.id  INNER JOIN
   networks nt ON na.network_id=nt.id INNER JOIN countries ct ON ct.id=nt.country_id 
   INNER JOIN codeGenerer cg ON na.codeGenerer_id=cg.id WHERE
    cg.category='geolocated' AND na.etat=1 LIMIT 51 OFFSET $page");
    if($res){
        $li=["page"=>$_POST["page"],"count"=>0,"total"=>($_POST["page"]-1)*51];
        while ($r=mysqli_fetch_array($res,MYSQLI_ASSOC)){
            $li["items"][]=$r;
            $li["count"]=$li["count"]+1;
        }
        $li["total"]=$li["total"]+$li["count"];
        echo json_encode($li);
    }else echo mysqli_error($con);
}
function listPhonenum(){
    try {
        $con = mysqli_connect(DB_HOST, DB_USER
            , DB_PASSWORD, DB_DATABASE);
        if($con){
            $net=mysqli_query($con,"select * from networks");
            if($net){
                while($row=mysqli_fetch_array($net, MYSQLI_ASSOC )) {

                    $rows[] = $row;

                }
                echo json_encode($rows);
            }else{
                echo json_encode(mysqli_error($con));
            }
        }else{
            var_dump(mysqli_connect_error());
        }

    }catch (Exception $e){
        echo 'error';
        echo $e->getMessage();
    }
}
function listOldNetwork(){
    $con = mysqli_connect(DB_HOST, DB_USER
        , DB_PASSWORD, "ilink");
    if($con){
        $net=mysqli_query($con,"select * from network WHERE reseau='KENYA'");
        if($net){
            $rows=[];
            while ($row=mysqli_fetch_array($net,MYSQLI_ASSOC)){

                $filt=[];
                foreach($row as $key => $value) {
                   if(strlen($value)>0){
                       $filt[$key]=$value;
                   }
                }
                $rows[]=$filt;
            }
            $connexion = mysqli_connect(DB_HOST, DB_USER
                , DB_PASSWORD, DB_DATABASE);
            if($connexion) {
                foreach ($rows as $row) {
                    foreach ($row as $key=>$value)
                        if(strtoupper($key)!='RESEAU')
                            mysqli_query($connexion,"INSERT INTO networks(country_id,name) VALUES('114','$value')");
                }
            }
            listnetwork();
        }else{
            echo json_encode(mysqli_error($con));
        }
    }else{
        var_dump(mysqli_connect_error());
    }
}
function listUsers(){
    $con = mysqli_connect(DB_HOST, DB_USER
        , DB_PASSWORD, DB_DATABASE);
    if($con){
        $net=mysqli_query($con,"select * from users");
        if($net){
            $rows=[];
            while ($row=mysqli_fetch_array($net,MYSQLI_ASSOC)){
                $rows[]=$row;
            }

            echo json_encode($rows);
        }else{
            echo json_encode(mysqli_error($con));
        }
    }else{
        var_dump(mysqli_connect_error());
    }
}
function listagentNetwork(){
    try {
        $con = mysqli_connect(DB_HOST, DB_USER
            , DB_PASSWORD, DB_DATABASE);
        if($con){
            $net=mysqli_query($con,"select * from networks_agents na INNER JOIN codeGenerer cg ON na.codeGenerer_id=cg.id");
            if($net){
                while ($row=mysqli_fetch_array($net,MYSQLI_ASSOC)){
                    $rows[]=$row;
                }

                echo json_encode($rows);
            }else{
                echo json_encode(mysqli_error($con));
            }
        }else{
            var_dump(mysqli_connect_error());
        }

    }catch (Exception $e){
        echo 'error';
        echo $e->getMessage();
    }
}
function insertNetwork(){
    $country=$_POST['country'];
    $name=$_POST['network'];
    try {
        $con = mysqli_connect(DB_HOST, DB_USER
            , DB_PASSWORD, DB_DATABASE);
        if($con){
            $net=mysqli_query($con,"INSERT INTO networks(country_id,name) VALUES('$country','$name')");
            if($net){
                echo json_encode('success');
            }else{
                echo json_encode(mysqli_error($con));
            }
        }else{
            var_dump(mysqli_connect_error());
        }

    }catch (Exception $e){
        echo 'error';
        echo $e->getMessage();
    }

}
function listcountry(){
    try {
        $con = mysqli_connect(DB_HOST, DB_USER
            , DB_PASSWORD, DB_DATABASE);
        if($con){
            $net=mysqli_query($con,"select * from countries");
            if($net){
                echo json_encode(mysqli_fetch_all($net));
            }else{
                echo json_encode(mysqli_error($con));
            }
        }else{
            var_dump(mysqli_connect_error());
        }

    }catch (Exception $e){
        echo 'error';
        echo $e->getMessage();
    }

}
function listcode(){
    try {
        $con = mysqli_connect(DB_HOST, DB_USER
            , DB_PASSWORD, DB_DATABASE);
        if($con){
            $net=mysqli_query($con,"select * from codeGenerer");
            if($net){
                while($row=mysqli_fetch_array($net,MYSQLI_ASSOC)){
                    $rows[]=$row;
                }
                echo json_encode($rows);
            }else{
                echo json_encode(mysqli_error($con));
            }
        }else{
            var_dump(mysqli_connect_error());
        }

    }catch (Exception $e){
        echo 'error';
        echo $e->getMessage();
    }

}
function listnetwork(){
        try {
            $con = mysqli_connect(DB_HOST, DB_USER
                , DB_PASSWORD, DB_DATABASE);
            if($con){
                $net=mysqli_query($con,"select * from networks");
                if($net){
                    while($row=mysqli_fetch_array($net, MYSQLI_ASSOC )) {

                        $rows[] = $row;

                    }
                    echo json_encode($rows);
                }else{
                    echo json_encode(mysqli_error($con));
                }
            }else{
                var_dump(mysqli_connect_error());
            }

        }catch (Exception $e){
            echo 'error';
            echo $e->getMessage();
        }
    };
function generateHyperVisor(){
   $random= generateRandomString();
    $con = mysqli_connect(DB_HOST, DB_USER
        , DB_PASSWORD, DB_DATABASE_TEST);
    $insert=mysqli_query($con,"insert INTO codeGenerer (code_parrain,code_membre,category) VALUES ('$random','$random','hyper')");
    if($insert){
        var_dump($insert);
    }else {
        echo mysqli_error($con);
    }
}
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}