<?php
/**
 * Created by PhpStorm.
 * User: freuddebian
 * Date: 15/10/18
 * Time: 13:39
 */


include '../database/Requester.php';

$tag="";

$postdata = file_get_contents("php://input");
$request=null;
if(isset($postdata)) {
    $request = json_decode($postdata);
}

if($postdata!=null && isset($request->tag)){
    $tag=$request->tag;
    switch ($tag) {
        case 'avail_countries':
            getAvailCountries();
            break;
        case 'listnetworkscountryindicatif':
            getAllCountryNetwork($request);
            break;
        case 'geolocated':

            if(isset($request->type)) {
                $type = $request->type;
                switch ($type) {
                    case 'listNetwork':
                        getAllNetworkGeo($request);
                        break;
                }
            }
            break;
        case 'supervisor':
            if(isset($request->type)) {
                $type = $request->type;
                switch ($type) {
                    case 'listFreeNetwork':
                        getAllFreeNetworkForSupervisor($request);
                        break;
                }
            }
            break;
        default:
            echo json_encode(['error'=>4,'error_msg'=>'tag manquant']);

    }
}else{
    $res=[];
    $res['error']=-4;
    $res['msg']='aucun tag n a ete defini';
    $res['data']=$request;
    echo json_encode($res);
}
function getAvailCountries(){
    $req=new Requester(null);

    echo     $req->getActiveCountries();
}

function getAllFreeNetworkForSupervisor($request){
    if(isset($request->code_parrain)){
        $req=new Requester($request->code_parrain);
        echo json_encode($req->listFreeNetworksForSuper());
    }else
        echo json_encode(['error'=>'code parrain est absent']);

}
function getAllNetworkGeo($request){
    if(isset($request->id)){
        $req=new Requester($request->id );
        echo json_encode($req->listNetworksGeo());

    }else
        echo json_encode(['error'=>'l\'id est absent']);


}
function getAllCountryNetwork($request){
    if(isset($request->indicatif)){
        $req=new Requester(null);
        echo json_encode($req->getCountryNetWork($request->indicatif));
    }
}