<?php
include 'common.php';


function IsAlreadyConnected($dbhandle, $wallet_id)
{
	$stmt = $dbhandle->prepare("select count(*) as cnt from account where wallet_id=?");
	$stmt->bind_param("s", $wallet_id);
	$stmt->execute();
	$result = $stmt->get_result();
	$row = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	if( !$result )
	{
		return true;
	}
	
	if($row["cnt"] > 0 )
		return true;   //yes already connected
	else
		return false;
}

function IsAccountAlreadyHasWalletId($dbhandle, $account_id)
{
	$stmt = $dbhandle->prepare("select count(*) as cnt from account where user_id=? and wallet_id <> null");
	$stmt->bind_param("s", $account_id);
	$stmt->execute();
	$result = $stmt->get_result();
	$row = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	if( !$result )
	{
		return true;
	}
	
	if($row["cnt"] > 0 )
		return true;   //yes already connected
	else
		return false;
}

function ConnectWalletId($account_id, $login_key, $wallet_id, $wallet_address)
{
	$dbhandle = DB_Connect();
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	
	if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	{
		PrintLoginKeyError();
		return;
	}
	$response = "WALLET_ID_CONNECTION_FAILED";	
	$error = "";
	$connected_wallet_id = "";
	
	if(IsAlreadyConnected($dbhandle, $wallet_id))
	{
		$error = "The wallet id is already connected.";
	}
	else
	{
		if(IsAccountAlreadyHasWalletId($dbhandle, $account_id))
		{
			$error = "Your account already has other wallet id";
		}
		else
		{
			$stmt = $dbhandle->prepare("update account set wallet_id=? where user_id=? and login_key=?");
			$stmt->bind_param("sss", $wallet_id, $account_id, $login_key);
			$result = $stmt->execute();
			$stmt->close();
			
			
			
			if($result)
			{				
				$connected_wallet_id = $wallet_id;
				$response = "WALLET_ID_CONNECTION_OK";
				// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
				//mysqli_free_result($result); 
			}
			else
			{
				$error = mysqli_error($dbhandle);
			}
		}
	}
	
	
	
	
	
	 mysqli_close($dbhandle);
 
    $isXmlHttpRequest = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ?
    (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false: false;
    ($isXmlHttpRequest) ? header('Content-type: application/json') : header('Content-type: text/plain');
 
    echo json_encode(array('result'=>$response, 'connected_wallet_id'=>$connected_wallet_id,'error'=>$error));
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$login_key = $json->login_key;
$wallet_id = $json->wallet_id;
$wallet_address = $json->wallet_address;
$checksum = $json->checksum;
		
$check = $wallet_id.$wallet_address;

if(CheckSum($account_id,$check,$checksum))
{
	ConnectWalletId($account_id, $login_key, $wallet_id, $wallet_address);
}



	

?>