<?php
include 'common.php';


function LogMail($dbhandle, $mail_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;");
	
	$stmt = $dbhandle->prepare("SELECT * FROM mail_box_character WHERE id=?");	
	$stmt->bind_param("i", $mail_id);
	$stmt->execute();
	$result = $stmt->get_result();	
	if($result)
	{		
		if($row = $result->fetch_assoc())
		{
			mysqli_stmt_free_result($stmt);

			$now = new DateTime('now', new DateTimeZone('UTC'));
			$strNow = $now->format('Y-m-d H:i:s');

			$query = "INSERT INTO mail_log (mail_type, user_id, type, title, content, item_id, item_count, item_enchant, exchange_id, time_limit, send_time, action, action_time)
			 VALUES ('CHARACTER_MAIL','".$row["character_id"]."','".$row["type"]."','".$row["title"]."','".$row["content"]."',".$row["item_id"].",".$row["item_count"].",".$row["item_enchant"].",".$row["exchange_id"].",".$row["time_limit"].",'".$row["time_send"]."','REMOVE','".$strNow."')";
			
			$result2 = @mysqli_query($dbhandle, $query);
			if($result2 != 1)
			{		
				return false;
			}
			return true;
		}
	}
}


function DeleteCharacterMail($dbhandle, $group_mail_id)
{
	$split = ",";
	$arrMailIds = explode($split, $group_mail_id);
	for($i=0;$i< sizeof($arrMailIds);$i++)
	{
		//Delete Mail
		LogMail($dbhandle, $arrMailIds[$i]);
		$query = "DELETE from mail_box_character where id=".$arrMailIds[$i];
		$result = @mysqli_query($dbhandle,$query);
		if($result != 1)
		{		
			return false;
		}
	}
	
	return true;
}

function RemoveCharacterMail($group_mail_id, $account_id, $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 = "REMOVE_MAIL_FAILED";
	$error = "";

	$delete_mail = DeleteCharacterMail($dbhandle, $group_mail_id);
	if ($delete_mail)
	{
		$response = "REMOVE_MAIL_OK";
	}
	else
	{
		//$error = mysqli_error($dbhandle);
		$error = "Delete Mail Failed";
	}
	
	 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,'error'=>$error,'list_mail_id'=>$group_mail_id));
}

$json = json_decode(file_get_contents('php://input'));
$group_mail_id = $json->group_mail_id;
$account_id = $json->account_id;
$login_key = $json->login_key;



if(CheckValidatedServerIP())
{
	RemoveCharacterMail($group_mail_id, $account_id, $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	));
}

?>