<?php
include 'common.php';



function AddGuildGold($dbhandle, $guild_uid, $gold)
{
	$stmt = $dbhandle->prepare("UPDATE guild_insidedata SET gold=gold+? WHERE guild_uid=?");
	$stmt->bind_param("ii", $gold, $guild_uid);
	$result = $stmt->execute();
	$stmt->close();
			
	if($result)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function LogDonationList($dbhandle, $guild_uid, $character_uid, $gold)
{
	$now = new DateTime();
	$now->setTimezone(new DateTimeZone('UTC'));
	$curdate =  date_format($now, "Y-m-d H:i:s");

	$stmt = $dbhandle->prepare("INSERT INTO guild_asset_log (guild_uid, character_uid, asset_type, amount, type, date) VALUES(?,?,'gold',?,'DONATION',?)");
	$stmt->bind_param("iiis", $guild_uid, $character_uid, $gold, $curdate);
	$result = $stmt->execute();
	$stmt->close();	
	
	if($result)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function DonationGold($guild_uid, $account_id, $character_uid, $login_key, $gold)
{
	//Connect DB
	$dbhandle = DB_Connect();

	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");

	if(!CheckLoginKey($dbhandle, $account_id, $login_key))
	{
		PrintLoginKeyError();
		mysqli_close($dbhandle);
		return;
	}

	if (GetGold($dbhandle, $account_id, $character_uid) < $gold)
	{
		$return = "LACK_OF_GOLD";
		$error = "";
		$response = array('result'=>$return,'error'=>$error);

		EchoResponse($response);
		mysqli_close($dbhandle);
		return;
	}

	if (_ConsumeGold($dbhandle, $account_id, $character_uid, $gold))
	{
		if(AddGuildGold($dbhandle, $guild_uid, $gold))
		{
			if (LogDonationList($dbhandle, $guild_uid, $character_uid, $gold))
			{
				$return = "DONATION_GOLD_OK";
				$error = "";
				$response = array('result'=>$return,'error'=>$error);
	
				EchoResponse($response);
			}
			else
			{
				$return = "DONATION_GOLD_OK_LOG_ERROR";
				$error = "";
				$response = array('result'=>$return,'error'=>$error);
	
				EchoResponse($response);
			}
			
		}
		else
		{
			$return = "DONATION_GOLD_FAIL";
			$error = "";
			$response = array('result'=>$return,'error'=>$error);

			EchoResponse($response);
		}
	}
	else
	{
		$return = "CONSUME_GOLD_FAIL";
		$error = "";
		$response = array('result'=>$return,'error'=>$error);

		EchoResponse($response);
	}

	mysqli_close($dbhandle);
}

function EchoResponse($response)
{
	$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($response);
}


$guild_uid = $_GET['guild_uid'];
$account_id = $_GET['account_id'];
$character_uid = $_GET['character_uid'];
$login_key = $_GET['login_key'];
$gold = $_GET['gold'];

if(CheckValidatedServerIP())
{
	DonationGold($guild_uid, $account_id, $character_uid, $login_key, $gold);
}
else
{
	$response = "INVALIDATED_SERVER";
	$error = "CheckValidatedServerIP ERROR";
	$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,'error'=>$error	));
}


?>