<?php
include 'common.php';



function InsertInvenItem($account_id, $char_uid, $item_uid,$parts_type, $item_count, $is_viewed, $optType1, $optValue1, $optType2, $optValue2, $optType3, $optValue3, $optType4, $optValue4, $login_key, $gold_price, $crystal_price, $wp_price)
{
	$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 = "INSERT_INVEN_FAILED";
	$error = "";
	
	
	$can_insert_item = true;
	
	if($gold_price > 0) //case of Purchase
	{
		$can_insert_item = _ConsumeGold($dbhandle, $account_id, $char_uid, $gold_price);
	}
	
	if($crystal_price > 0) //case of Purchase
	{
		$can_insert_item = _ConsumeCrystal($dbhandle, $account_id, $crystal_price);
	}
	
	if($wp_price > 0)
	{
		$can_insert_item = _ConsumeWP($dbhandle, $account_id, $char_uid, $wp_price);
	}
	
	if($can_insert_item)
	{
		$insert_query = "INSERT INTO owned_item (character_uid, item_uid, count, is_viewed, is_equiped, parts_type, optType1, optValue1, optType2, optValue2, optType3, optValue3, optType4, optValue4) VALUES (".$char_uid.",".$item_uid.",".$item_count.",".$is_viewed.",0,".$parts_type.",'".$optType1."',".$optValue1.",'".$optType2."',".$optValue2.",'".$optType3."',".$optValue3.",'".$optType4."',".$optValue4.")";

		//Query Result
		$result = @mysqli_query($dbhandle,$insert_query);
		
		if($result)
		{
			$response = "INSERT_INVEN_OK";
			// don't use for upate/insert query, because these query result is true/false, not mysqli_result object
			//mysqli_free_result($result); 
			
			//$error = $insert_query ;
		}
		else
		{
			
			$error = mysqli_error($dbhandle);
			//$error = $insert_query ;
		}
	}
	else
	{
		$response = "INSERT_INVEN_FAILED";
		$error = "Consume 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,'item_uid'=>$item_uid,'parts_type'=>$parts_type,'item_count'=>$item_count, 'gold_price'=>$gold_price, 'crystal_price'=>$crystal_price, 'wp_price'=>$wp_price, 'error'=>$error));
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$char_uid = $json->char_uid;
$item_uid = $json->item_uid;
$parts_type = $json->parts_type;
$item_count = $json->item_count;
$is_viewed = $json->is_viewed;
$optType1 = $json->optType1;
$optValue1 = $json->optValue1;
$optType2 = $json->optType2;
$optValue2 = $json->optValue2;
$optType3 = $json->optType3;
$optValue3 = $json->optValue3;
$optType4 = $json->optType4;
$optValue4 = $json->optValue4;
$login_key = $json->login_key;
$gold_price = $json->gold_price;
$crystal_price = $json->crystal_price;
$wp_price = $json->wp_price;


if(CheckValidatedServerIP())
{
	if($item_count > 0)
	{
		InsertInvenItem($account_id, $char_uid, $item_uid, $parts_type, $item_count, $is_viewed, $optType1, $optValue1, $optType2, $optValue2, $optType3, $optValue3, $optType4, $optValue4, $login_key, $gold_price, $crystal_price, $wp_price);
	}

}
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	));
}

?>