<?php
/**
 * Created by PhpStorm.
 * User: freuddebian
 * Date: 08/08/18
 * Time: 16:40
 */
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 'member':
            $type=$request->type;
            switch ($type){
                case 'all_geolocated':
                    getAllMembersGeolocated($request);
                    break;
                case 'deleteAgent':
                    deleteAgent($request);
                    break;
                case 'all_supervisor':
                    getSuperviseurNetwork($request);
                    break;
                case 'create_user':
                    createSimpleUser($request);
                    break;
                case 'get_category_child':
                    getCagetoryChild($request);
                    break;
                case 'credit_demand':
                    makeCreditRequest($request);
                    break;
                case 'validate_agent':
                     validate_agentAccount($request);
                     break;
                case "valider_simple":
                    validateUserSimple($request);
                    break;
                case 'create_supervisor':
                case 'create_geolocated_user':
                    createAgent($request);
                    break;
                case 'list_demande_adhesion':
                    listDemandAdhesion($request);
                    break;
                case 'activeSupervisor':
                    activeSupervisor($request);
                    break;
                case 'generateNetworkAgentForGeo':
                    generateNetworkForGeo($request);
                    break;
                case 'assignNetworkAgentToGeo':
                    assignNetworkAgentToGeo($request);
                    break;
                case 'update_position':
                    updatePosition($request);
                    break;
                case 'agen_info_code':
                    getAgentInfoByCode($request);
                    break;
                default:
                    echo json_encode(['error'=>3,'error_msg'=>'type manquant',"data"=>$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 deleteAgent($request){
    if(isset($request->code)){
        $requester=new Requester(null,$request->lang);
        $code=$request->code;
        echo $requester->deleteAgent($code);
    }
    else echo json_encode(['error'=>-3,'data'=>$request]);
}
function getAgentInfoByCode($request){
    if(isset($request->code)){
        $requester=new Requester(null,$request->lang);
        $code=$request->code;
        echo $requester->getAgentInfoByCode($code);
    }
    else echo json_encode(['error'=>-3,'data'=>$request]);

}
function updatePosition($request){
    if(isset($request->agentId)){
          $requester=new Requester(null,"fr");
          $code=$request->agentId;
          $long=$request->longitude;
          $lat=$request->latitude;
          echo $requester->updatePosition($code,$long,$lat);
    }else echo json_encode(['error'=>-3,'data'=>$request]);
}
function assignNetworkAgentToGeo($request){
    if(isset($request->agentId)){
        if(isset($request->code_parrain)){
            $requester=new Requester(null,$request->lang);
            echo $requester->assignNetworkToAgent($request->agentId,$request->code_parrain,$request->phone,$request->provider_class_id ?? null);

        }else{
            echo json_encode(['error'=>-2,'error_message'=>'validation code manquant']);
        }

    }else{
        echo json_encode(['error'=>-1,'error_message'=>'code_membre manquant']);
    }
}
function generateNetworkForGeo($request){
    if(isset($request->phone)){
        if(isset($request->code_parrain)){
            $requester=new Requester(null,$request->lang);
            echo $requester->generateEmptyAgentNetworkForAgent($request->phone,$request->code_parrain);

        }else{
            echo json_encode(['error'=>-1,'error_message'=>'parrain manquant']);
        }
    }else{
        echo json_encode(['error'=>-1,'error_message'=>'phone manquant']);
    }
}
function activeSupervisor($request){
    $requester=new Requester(0,$request->lang);
    if(isset($request->code)){
        echo $requester->activeSupervisorAdhesion($request->code,$request->phone);
    }else{
     echo json_encode(['error'=>'code membre manquant']);
    }
}
function validateUserSimple($request){
    $requester=new Requester(0,$request->lang);
        if(isset($request->phone)){
            echo json_encode($requester->validateUser($request->phone));
    }
}

function validate_agentAccount($request){
    $requester=new Requester(0,$request->lang);
    if(isset($request->code_validation)){
        if(isset($request->phone)){
             echo json_encode($requester->validateAgent($request->phone,$request->code_validation,$request->nbre_code,$request->nbre_code_superviseur));
        }
    }

}
function getCagetoryChild($request){
    $codeparrain=$request->codeparrain;
    $requester=new Requester(0);
    if(isset($request->codeparrain)){
        echo json_encode($requester->getChildCode($codeparrain));
    }
    else{
        echo json_encode(['error'=>2,'error_msg'=>' code parrain manquant']);
    }
}
function listDemandAdhesion($request){
    $result=[];
    $requester=new Requester(null);
    if(isset($request->codeparrain)) {
        $codeparrain = $request->codeparrain;
        echo json_encode($requester->getSupervisorAdhesionList($codeparrain));
    }
    else{
        echo json_encode(['error'=>2,'error_msg'=>' code parrain manquant']);
    }
    }

function getAllMembersGeolocated($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 network(){
    $r=new Requester(null);
    echo $r->getNetwork();
}
function makeCreditRequest($request){

}

function createAgent($request){
    // Register Geolocated
    // Request type is Register new user
    $requester=new Requester(null,$request->lang);
    $req=[];
    if(isset($request->address)) {
        if(isset( $request->lastname)){
            if(isset( $request->email)){
                    if(isset($request->phone)){
                        if(isset($request->password)){
                            if(isset( $request->town)){
                                if(isset($request->network)){
                                if(isset($request->latitude) && isset($request->longitude)){
                                  if(isset($request->member)){
                                      if(isset($request->category)){
                                          $request->phone = str_replace(' ', '', $request->phone);
                                          $request->phone_transaction = str_replace(' ', '', $request->phone_transaction);
                                          echo $requester->registerGeolocated($request);
                                      }else{
                                          $req['error']=-10;
                                          $req['msg']='missing categorie';
                                      }
                                  }else {
                                      $req['error']=-9;
                                      $req['msg']='missing member';
                                  }
                                } else{
                                    $req['error']=-8;
                                    $req['msg']='missing position';
                                }
                                }else{
                                    $req['error']=-7;
                                    $req['msg']='missing network';

                                }
                            }else{
                                $req['error']=-6;
                                $req['msg']='missing country';

                            }
                        }   else{
                            $req['error']=-5;
                            $req['msg']='missing password';
                        }
                    }   else{

                        $req['error']=-4;
                        $req['msg']='missing phone';

                    }
            }else {

                $req['error']=-3;
                $req['msg']='missing email';

            }
        }else {
            $req['error']=-2;
            $req['msg']='missing last name';

        }
    }else {
        $req['error']=-1;
        $req['msg']='missing address';
    }
}
function createSimpleUser($request)
{
    $requester=new Requester(null);
    $req=[];
    if(isset($request->address)) {
        if(isset( $request->lastname)){
            if(isset( $request->email)){
                if(isset($request->phone)){
                    if(isset($request->password)){
                        if(isset( $request->town)) {
                            if (isset($request->network)) {
                                $request->phone = str_replace(' ', '', $request->phone);
                                $usr=$requester->registerUser($request);
                                echo $usr;
                            } else {
                                $req['error'] = -7;
                                $req['msg'] = 'missing network';

                            }
                        }else{
                            $req['error']=-6;
                            $req['msg']='missing country';

                        }
                    }   else{
                        $req['error']=-5;
                        $req['msg']='missing password';
                    }
                }   else{

                    $req['error']=-4;
                    $req['msg']='missing phone';

                }
            }else {

                $req['error']=-3;
                $req['msg']='missing email';

            }
        }else {
            $req['error']=-2;
            $req['msg']='missing last name';

        }
    }else {
        $req['error']=-1;
        $req['msg']='missing address';
    }

}
