<?php
/**
 * Created by PhpStorm.
 * User: freuddebian
 * Date: 05/08/18
 * Time: 06:28
 */
require_once ('../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 'location':
            $type=$request->type;
            switch ($type){
                case 'all':
                    getAllpoint($request);
                    break;
                case 'all_network':
                    getSuperviseurNetwork($request);
                    break;
                case 'around':
                    getPointAround($request);
                    break;
                case 'all_network_point':
                    getPointNetwork($request);
                    break;
                default:
                    echo json_encode(['error'=>3,'error_msg'=>'type manquant']);
                    break;
            }
            break;
        case 'all_towns_by_indicatif':
            getListTownsCountry($request);
            break;
        case 'town_information_name':
            getTownInfoByName($request);
            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 getTownInfoByName($request){
    if(isset($request->name)){
        $req=new Requester(null);
        echo json_encode($req->getTownInfoByName($request->name));
    }
}
function getListTownsCountry($request){
    if(isset($request->indicatif)){
        $req=new Requester(null);
        echo json_encode($req->getListTownsCountry($request->indicatif));
    }
}

function getAllPoint($request){
    $result=[];
    $requester=new Requester(null);
    if(isset($request->country)) {
        $country = $request->country;
        echo json_encode($requester->getAllCountryPoint($country));
    }else{
        echo json_encode(['error'=>2,'error_msg'=>' code pays manquant']);
    }
}
function getSuperviseurNetwork($request)
{
    $result = [];
    $requester = new Requester(null);
    if (isset($request->codeparrain)) {
        $codeparrain = $request->codeparrain;
        echo json_encode($requester->getSuperviseurNetwork($codeparrain));
    } else {
        echo json_encode(['error' => 2, 'error_msg' => ' code parrain manquant']);
    }
}
function getPointAround($request){
        if(isset($request->id)){
        $requester=new Requester($request->id);
            if(isset($request->distance)) {
                $distance =$request->distance;
                if(isset($request->network)) {
                    $reseau=$request->network;
                    if (isset($request->position)) {
                        $position = $request->position;
                        $page=isset($request->page)?$request->page:0;
                        echo $requester->getPointAroundKm($reseau,$position, $distance,$page);
                    } else
                        echo json_encode(['error' => -3, 'error_msg' => 'position user missing',
                            "position" => isset($request->position)]);
                }else echo json_encode(['error' => -7, 'error_msg' => 'reseau user missing',
                    "position" => isset($request->reseau)]);
            }else echo json_encode(['error'=>-2,'error_msg'=>'distance manquante']);
        }else echo json_encode(['error'=>-1,'error_msg'=>'user id manquant']);

    }


    function getPointNetwork($request)
    {
        if (isset($request->id)) {
            $requester = new Requester($request->id);
            $network=$request->network;
            if(isset($network)){
                echo $requester->getNetworkPoint($network);
            }else echo json_encode(['error'=>-1,'error_msg'=>'missing network']);
        } else echo json_encode(['error' => -1, 'error_msg' => 'user id manquant']);
    }

