<?php
include 'common.php';


function PrintDebug($response, $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));
	exit; 
}

function Available_PurchaseTotalLimit($dbhandle,$account_id, $char_uid, $item_uid, $limit_count)
{
	$stmt = $dbhandle->prepare("select sum(item_count) as total from purchase_log where item_uid=? and user_id=? and character_uid=?");	
	$stmt->bind_param("isi",$item_uid, $account_id, $char_uid);
	$stmt->execute();
	$result = $stmt->get_result();
	$num = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	
	if($num["total"] >= $limit_count)
	{				
		return false;
	}
	else
	{
		return true;
	}
	
	
		
	
}


function Available_PurchaseDailyLimit($dbhandle,$account_id, $char_uid, $item_uid, $daily_limit)
{
	$now = new DateTime();
	$now->setTimezone(new DateTimeZone('UTC'));
	$log_time =  date_format($now, "Y-m-d");
		
		
	$stmt = $dbhandle->prepare("select count(*) as cnt from purchase_log where item_uid=? and user_id=? and character_uid=? and DATE_FORMAT(log_time,'%Y-%m-%d') =?");	
	$stmt->bind_param("isis",$item_uid, $account_id, $char_uid, $log_time);
	$stmt->execute();
	$result = $stmt->get_result();
	$num = $result->fetch_assoc();
	
	mysqli_stmt_free_result($stmt);
	
	if($num["cnt"] >= $daily_limit)
	{				
		//PrintDebug("account_id:".$account_id."char_uid:". $char_uid.",item_uid:".$item_uid.",log_time:".$log_time, "");
		return false;
	}
	else
	{
		
		//PrintDebug("Available_PurchaseDailyLimit == true", "");
		return true;
	}
	
	
		
	
}





function CheckPurchaseLimit($account_id, $login_key, $char_uid, $item_uid, $item_count){
	$dbhandle = DB_Connect();
	
	//Select DB
	$db_selected = mysqli_select_db($dbhandle, DB_NAME)
	or die("Could not select db");
	
	$response = "CHECK_PURCHASE_LIMIT_OK";
	$error = "";
	
	$total_count_limit = 0;
	$daily_limit  =0;
	$reason_type = 0;
	
	if($item_uid == 30110) //SkillBook
	{		
		$total_count_limit = 1000;
		$daily_limit  =1;
		
	}
	else if($item_uid == 30200)// option change
	{
		$total_count_limit = 1000;
		$daily_limit  =1;
	}
	else if($item_uid == 52210)// option change
	{
		$total_count_limit = 3000;
		$daily_limit  =3;
	}
	else
	{
		PrintDebug("Purchase limt Error","Purchase limt Error");
	}
	
	if(!Available_PurchaseDailyLimit($dbhandle,$account_id, $char_uid,$item_uid, $daily_limit))
	{
		$response = "CHECK_PURCHASE_LIMIT_FAILED";
		$error = "Daily Limit";
		$reason_type = 1;
	}
	else if(!Available_PurchaseTotalLimit($dbhandle,$account_id, $char_uid, $item_uid, $total_count_limit))
	{
		$response = "CHECK_PURCHASE_LIMIT_FAILED";
		$error = "Total Limit";
		$reason_type = 2;
	}
	else{
		
	}
	
	

    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,'reason_type'=>$reason_type, 'daily_limit'=>$daily_limit,'total_count_limit'=>$total_count_limit,'error'=>$error));

}



//if(CheckValidatedServerIP())
//{
	CheckPurchaseLimit($_GET['account_id'], $_GET['login_key'], $_GET['char_uid'], $_GET['item_uid'], $_GET['item_count']);
//}
//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	));
//}



?>