<?php
include 'common.php';

function UpdateGold_ChangeOption($dbhandle, $current_gold, $char_uid)
{
	if($current_gold <0) $gold_price = 0;
	
	$update_query = "update character_list set gold=".$current_gold." where character_uid='".$char_uid."'";

	//Query Result
	$result = @mysqli_query($dbhandle,$update_query);
	if($result)
	{
		return true;
	}
	
	return false;
}

function UpdateChangeOptionScroll($dbhandle, $char_uid, $scroll_inven_uid, $scroll_update_count)
{
	if($scroll_update_count >0)
		$query = "update owned_item set count=".$scroll_update_count." where owned_item_uid=".$scroll_inven_uid." and character_uid='".$char_uid."'";
	else	
		$query = "delete from owned_item where owned_item_uid=".$scroll_inven_uid." and character_uid='".$char_uid."'";

	//Query Result
	$result = @mysqli_query($dbhandle,$query);
	if($result)
	{
		return true;
	}
	
	return false;
}

function SetItemOption($account_id, $char_uid, $inven_uid, $pickedType1,$pickedValue1, $pickedType2,$pickedValue2, $pickedType3,$pickedValue3, $pickedType4,$pickedValue4, $login_key, $scroll_inven_uid, $scroll_update_count, $current_gold)
{
	$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;
	}
	
	$error = "";
	$response = "ITEM_OPTION_FAILED";
	
	if(UpdateChangeOptionScroll($dbhandle, $char_uid, $scroll_inven_uid, $scroll_update_count) && UpdateGold_ChangeOption($dbhandle, $current_gold, $char_uid))
	{
		$update_query = "update owned_item set optType1='".$pickedType1."' , optValue1=".$pickedValue1.", optType2='".$pickedType2."' , optValue2=".$pickedValue2.", optType3='".$pickedType3."' , optValue3=".$pickedValue3.", optType4='".$pickedType4."' , optValue4=".$pickedValue4." where owned_item_uid=".$inven_uid." and character_uid='".$char_uid."'";

		//Query Result
		$result = @mysqli_query($dbhandle,$update_query);			
		$error = "";
		if($result)
		{
			$response = "ITEM_OPTION_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);
		}
	}
	
	 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));
}




if(CheckValidatedServerIP())
{
	SetItemOption($_GET['account_id'], $_GET['char_uid'], $_GET['inven_uid'], $_GET['pickedType1'], $_GET['pickedValue1'], $_GET['pickedType2'], $_GET['pickedValue2'], $_GET['pickedType3'], $_GET['pickedValue3'], $_GET['pickedType4'], $_GET['pickedValue4'], $_GET['login_key'], $_GET['scroll_inven_uid'], $_GET['scroll_update_count'], $_GET['current_gold']);
}
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	));
}


?>