<?php
include 'common.php';
include 'server_exchange_common.php';


function ChangeIsCancelColumFromExchangeItem($dbhandle, $id, $character_uid)
{
	$stmt = $dbhandle->prepare("UPDATE exchange_item SET is_cancel=1 WHERE id=? AND owner_uid=?");
	$stmt->bind_param("ii",  $id, $character_uid);
	$result = $stmt->execute();
	$stmt->close();
	
	if($result)
	{
		return true;
	}
	return false;
}

function SendExchangeMail($dbhandle, $character_uid, $item_uid, $count, $enchant, $exchange_id)
{
	mysqli_query($dbhandle,"set session character_set_connection=utf8;");
	mysqli_query($dbhandle,"set session character_set_results=utf8;");
	mysqli_query($dbhandle,"set session character_set_client=utf8;");

	$now = new DateTime('now', new DateTimeZone('UTC'));
	$strTime =  date_format($now, "Y-m-d H:i:s");

	$type = 'Exchange_item';
	$title = '거래소';
	$content = '거래소 아이템 회수';
	$time_limit = 720;

	$stmt = $dbhandle->prepare("INSERT INTO mail_box_character (character_id, type, title, content, item_id, item_count, item_enchant, exchange_id, time_limit, time_send) values(?,?,?,?,?,?,?,?,?,?)");
	$stmt->bind_param("isssiiiiis", $character_uid, $type, $title, $content, $item_uid, $count, $enchant, $exchange_id, $time_limit, $strTime);
	$result = $stmt->execute();
	$stmt->close();

	if ($result)
		return true;
	return false;
}

function RecallExchangeItem($id, $account_id, $character_uid, $login_key)
{
	$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();
		mysqli_close($dbhandle);
		return;
	}


	$response = "RECALL_EXCHANGE_ITEM_FAILED";
	$error = "";

	if (CheckExchangeIsMaintenance($dbhandle))
	{
		$response = "EXCHANGE_UNDER_MAINTENANCE";
		$error = "";
	}
	else
	{
		$stmt = $dbhandle->prepare("SELECT * FROM exchange_item WHERE is_cancel=0 AND id=? AND owner_uid=?");	
		$stmt->bind_param("ii", $id, $character_uid);
		$stmt->execute();
		$result = $stmt->get_result();
		if ($result)
		{
			if($row = $result->fetch_assoc())
			{
				mysqli_stmt_free_result($stmt);
				if (ChangeIsCancelColumFromExchangeItem($dbhandle, $id, $character_uid))
				{
					if (SendExchangeMail($dbhandle, $character_uid, $row['item_uid'], $row['count'], $row['enchant'], $id))
					{
						$response = "RECALL_EXCHANGE_ITEM_OK";
					}
					else
					{
						$error = "Send Exchange Mail Fail";
					}
				}
				else
				{
					$error = "Change Iscancel Exchange Item Fail";
				}
			}
			else
			{
				$error = "No Exist Exchange Item";
			}
		}
		else
		{
			$error = mysqli_error($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,'error'=>$error));
		
	mysqli_close($dbhandle);
}

$id = $_GET['id'];
$account_id = $_GET['account_id'];
$character_uid = $_GET['character_uid'];
$login_key = $_GET['login_key'];

if(CheckValidatedServerIP())
{
	RecallExchangeItem($id, $account_id, $character_uid, $login_key);
}
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	));
}

?>