<?php
error_reporting(0);
include 'connect.php';
$json = file_get_contents('php://input');
$objdata = json_decode( $json );
header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");

$function = $objdata->{'function'};

try {

    switch ($function) {
    case 'updateUserAddressData':
    $response['result'] = updateUserAddressData($db_connection, $objdata, $projectname, $site_api_url_fetch, $user_key);
    break;
    default:
    # code...
    break;
    }

    $response['message'] = 'Success';
	$response['error'] = false;

} catch (UnexpectedValueException $e) {
	$response['message'] = "Invalid data.";
	$response['error'] = true;
} catch (Exception $e) {
	$response['message'] = "Error";
	$response['error'] = true;
}
echo json_encode($response);


function updateUserAddressData($db_connection, $objdata, $projectname, $site_api_url_fetch, $user_key) {

    $id = $objdata->{'id'};
    $user_id = $objdata->{'user_id'};
    $data = $objdata->{'data'};    

    $url = $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json',
    'x-api-key: '.$user_key
    ));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $addresult = curl_exec($ch);
    $addredata = json_decode($addresult);

    $metafileds_buid = '';
    $metafileds_flid = '';
    $metafileds_stid = '';
    $metafileds_adrid = '';
    if(sizeof($addredata->result) > 0){
        foreach($addredata->result as $mval){
            if($mval->key == 'building_name'){
                $metafileds_buid = $mval->id;
            }
            if($mval->key == 'floor'){
                $metafileds_flid = $mval->id;
            }
            if($mval->key == 'street'){
                $metafileds_stid = $mval->id;
            }
            if($mval->key == 'additional_direct'){
                $metafileds_adrid = $mval->id;
            }
        }
    }

    file_put_contents('data.txt', "metafileds_buid-".$metafileds_buid. "\n", FILE_APPEND);
    $obj = new stdClass();
    

    if(isset($data->building_name)){
        $obj->building_name = $data->building_name;
        if($metafileds_buid != ''){
            $postfields = array(
                "value" => $data->building_name,
                "value_type" => 'text',
                "key" => 'building_name'
            );
            $data_json = json_encode($postfields);
            $chp = curl_init();
            curl_setopt($chp, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields/'.$metafileds_buid);
            curl_setopt($chp, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chp, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chp, CURLOPT_CUSTOMREQUEST, 'PUT');
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chp, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chp);
            $resultd = json_decode($resultd);
            curl_close($chp);
            file_put_contents('data.txt', "resultup-".json_encode($resultd). "\n", FILE_APPEND);
        }else{

            $postfields = array(
                "value" => $data->building_name,
                "value_type" => 'text',
                "key" => 'building_name'
            );
            $data_json = json_encode($postfields);
            $chd = curl_init();
            curl_setopt($chd, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields');
            curl_setopt($chd, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chd, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chd, CURLOPT_POST, 1);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chd, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chd);
            $resultd = json_decode($resultd);
            curl_close($chd);
            file_put_contents('data.txt', "resultadd-".json_encode($resultd). "\n", FILE_APPEND);
        }
    }
    if(isset($data->floor)){
        $obj->floor = $data->floor;
        if($metafileds_flid != ''){
            $postfields = array(
                "value" => $data->floor,
                "value_type" => 'text',
                "key" => 'floor'
            );
            $data_json = json_encode($postfields);
            $chp = curl_init();
            curl_setopt($chp, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields/'.$metafileds_flid);
            curl_setopt($chp, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chp, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chp, CURLOPT_CUSTOMREQUEST, 'PUT');
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chp, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chp);
            $resultd = json_decode($resultd);
            curl_close($chp);
        }else{

            $postfields = array(
                "value" => $data->floor,
                "value_type" => 'text',
                "key" => 'floor'
            );
            $data_json = json_encode($postfields);
            $chd = curl_init();
            curl_setopt($chd, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields');
            curl_setopt($chd, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chd, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chd, CURLOPT_POST, 1);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chd, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chd);
            $resultd = json_decode($resultd);
            curl_close($chd);
        }
    }
    if(isset($data->street)){
        $obj->street = $data->street;
        if($metafileds_stid != ''){
            $postfields = array(
                "value" => $data->street,
                "value_type" => 'text',
                "key" => 'street'
            );
            $data_json = json_encode($postfields);
            $chp = curl_init();
            curl_setopt($chp, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields/'.$metafileds_stid);
            curl_setopt($chp, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chp, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chp, CURLOPT_CUSTOMREQUEST, 'PUT');
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chp, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chp);
            $resultd = json_decode($resultd);
            curl_close($chp);
        }else{

            $postfields = array(
                "value" => $data->street,
                "value_type" => 'text',
                "key" => 'street'
            );
            $data_json = json_encode($postfields);
            $chd = curl_init();
            curl_setopt($chd, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields');
            curl_setopt($chd, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chd, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chd, CURLOPT_POST, 1);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chd, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chd);
            $resultd = json_decode($resultd);
            curl_close($chd);
        }
    }
    if(isset($data->additional_direct)){
        $obj->additional_direct = $data->additional_direct;
        if($metafileds_adrid != ''){
            $postfields = array(
                "value" => $data->additional_direct,
                "value_type" => 'text',
                "key" => 'additional_direct'
            );
            $data_json = json_encode($postfields);
            $chp = curl_init();
            curl_setopt($chp, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields/'.$metafileds_adrid);
            curl_setopt($chp, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chp, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chp, CURLOPT_CUSTOMREQUEST, 'PUT');
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chp, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chp);
            $resultd = json_decode($resultd);
            curl_close($chp);
        }else{

            $postfields = array(
                "value" => $data->additional_direct,
                "value_type" => 'text',
                "key" => 'additional_direct'
            );
            $data_json = json_encode($postfields);
            $chd = curl_init();
            curl_setopt($chd, CURLOPT_URL, $site_api_url_fetch.'/users/'.$user_id.'/addresses/'.$id.'/metafields');
            curl_setopt($chd, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chd, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chd, CURLOPT_POST, 1);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chd, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chd);
            $resultd = json_decode($resultd);
            curl_close($chd);
        }
    }

  return $obj;

}

?>