<?php
include 'common.php';


function GetItemCount($dbhandle, $char_uid, $owned_uid)
{
	$count = 0;
	
	
	$stmt = $dbhandle->prepare("select count from owned_item where character_uid=? and owned_item_uid=?");	
	$stmt->bind_param("ii", $char_uid,$owned_uid);
	$stmt->execute();
	$result = $stmt->get_result();	
	if($result)
	{		
		if($row = $result->fetch_assoc())
		{
			$count = $row["count"];
			mysqli_stmt_free_result($stmt);
		}
	}
	else{
		$error = mysqli_error($dbhandle);
	}
	
	
	return $count;
}

function AddItemCount(
$char_uid, 
$item_count, 
$owned_uid, 
$is_viewed, 
$account_id, 
$login_key, 
$gold_price, 
$crystal_price, 
$opened_box_inven_uid, 
$box_count_update,
$item_uid,     //log
$drop_method,//log
$pay_type )   //log
{
	$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;
	}
	
	
	if($gold_price > 0) //case of Purchase
	{
		_ConsumeGold($dbhandle, $account_id, $char_uid, $gold_price);
	}
	
	if($crystal_price > 0) //case of Purchase
	{
		_ConsumeCrystal($dbhandle, $account_id, $crystal_price);
	}
	
	
	
	$response = "UPDATE_COUNT_FAILED";
	$error = "";
	
	
	if(_UpdateBox($dbhandle, $char_uid, $opened_box_inven_uid, $box_count_update))
	{
		
		$current_count = GetItemCount($dbhandle, $char_uid, $owned_uid);
		$current_count += $item_count;
		
		if($current_count <=0)
		{
			$query = "delete from owned_item where owned_item_uid=".$owned_uid." and character_uid='".$char_uid."'";
		}
		else
		{
			$query = "update owned_item set count=".$current_count.", is_viewed=".$is_viewed." where owned_item_uid=".$owned_uid." and character_uid='".$char_uid."'";
		}
		
		

		//Query Result
		$result = @mysqli_query($dbhandle,$query);
		
		if($result)
		{
			$response = "UPDATE_COUNT_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);
		}
	}
	else
	{
		$response = "UPDATE_BOX_FAILED&UPDATE_COUNT_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,'current_count'=>$current_count,'error'=>$error));
}




if(CheckValidatedServerIP())
{
	AddItemCount($_GET['char_uid'], 
	$_GET['item_count'], 
	$_GET['owned_uid'], 
	$_GET['is_viewed'], 
	$_GET['account_id'], 
	$_GET['login_key'], 
	$_GET['gold_price'], 
	$_GET['crystal_price'], 
	$_GET['opened_box_inven_uid'], 
	$_GET['box_count_update'],
	$_GET['item_uid'],
	$_GET['drop_method'],
	$_GET['pay_type']);
}
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	));
}

?>