<?php
include 'common.php';


//장착하기 전에 같은 파츠는 is_equiped = 0으로 세팅
function ResetIsEquiped($dbhandle,  $char_uid, $parts_type)
{
	$update_query = "update owned_item set is_equiped =0 WHERE parts_type=".$parts_type." and character_uid=".$char_uid;
	$result = @mysqli_query($dbhandle,$update_query);
	if($result)
	{
		return true;
		
	}
	else
	{
		return false;//$error = mysqli_error($dbhandle);
	}
	
	return true;
}

//equipment but not parts (ring, necklace)
function SetEquipOnEquipment($account_id, $char_uid, $parts_type, $parts_uid, $parts_inven_uid, $prev_parts_inven_uid, $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 = "SAVE_PARTS_FAILED";
	if(ResetIsEquiped($dbhandle,  $char_uid, $parts_type))
	{
		
		
		//$update_query = "update owned_item set is_equiped = CASE WHEN owned_item_uid=".$parts_inven_uid." THEN 1 WHEN owned_item_uid=".$prev_parts_inven_uid." THEN 0 else is_equiped END WHERE character_uid=".$char_uid;
		$update_query = "update owned_item set is_equiped = 1 where parts_type=". $parts_type." and owned_item_uid=".$parts_inven_uid." and character_uid=".$char_uid;
		
		$result = @mysqli_query($dbhandle,$update_query);
		if($result)
		{
			$response = "SAVE_PARTS_OK";
			$error = $update_query;//test
		}
		else
		{
			$error = mysqli_error($dbhandle);
		}
		
		//$error = $update_query;
	}
	else
	{
		$response = "SAVE_PARTS_FAILED";
		$error = "Reset IsEquiped of PrevParts  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));
	
	
}

function UpdateCurrentPartsSingle($account_id, $char_uid, $parts_type, $parts_uid, $parts_inven_uid, $prev_parts_inven_uid, $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;
	}
	
	
	
	
	$update_query = "";
	//ePT_HEAD = 1,ePT_WEAPON = 2, ePT_SHIELD = 4, ePT_QUIVER = 5, ePT_BODY = 6, ePT_ARM = 7,  ePT_LEG = 8,        ePT_SHOULDER = 9,
	switch($parts_type){
		case '1' :
			$update_query = "update character_list set parts_head_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
		case '2' :
			$update_query = "update character_list set parts_weapon_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
		case '4' :
			$update_query = "update character_list set parts_shield_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
		case '5' :
			$update_query = "update character_list set parts_quiver_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
		case '6' :
			$update_query = "update character_list set parts_body_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
		case '7' :
			$update_query = "update character_list set parts_arm_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
		case '8' :
			$update_query = "update character_list set parts_leg_uid=".$parts_uid." where user_id='".$account_id."' and character_uid=".$char_uid;
		break;
	}
	

	$response = "SAVE_PARTS_FAILED";
	$error = "";
	if(ResetIsEquiped($dbhandle,  $char_uid, $parts_type))
	{
		//Query Result
		$result = @mysqli_query($dbhandle,$update_query);
		if($result)
		{
			$response = "SAVE_PARTS_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 = "SAVE_PARTS_FAILED";
		$error = "Reset IsEquiped of PrevParts  Failed";
	}

	
	
	
	/*if($response == "SAVE_PARTS_OK")
	{
		$update_query = "update owned_item set is_equiped = 0 WHERE parts_type=".$parts_type." and character_uid=".$char_uid;
		$result = @mysqli_query($dbhandle,$update_query);
		
		if($result)
		{
			$response = "SAVE_PARTS_OK";
			$error = $update_query;//test
		}
		else
		{
			$error = mysqli_error($dbhandle);
		}
	}*/
	
	
	if($response == "SAVE_PARTS_OK")
	{
		$response = "SAVE_PARTS_FAILED";
		//update owned_item set is_equiped = CASE WHEN item_uid=15 THEN 1 WHEN item_uid=10 THEN 0 else is_equiped END WHERE character_uid=75
		$update_query = "update owned_item set is_equiped = CASE WHEN owned_item_uid=".$parts_inven_uid." THEN 1 WHEN owned_item_uid=".$prev_parts_inven_uid." THEN 0 else is_equiped END WHERE character_uid=".$char_uid;
		$result = @mysqli_query($dbhandle,$update_query);
		if($result)
		{
			$response = "SAVE_PARTS_OK";
			$error = "";
		}
		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));
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$char_uid = $json->char_uid;
$parts_type = $json->parts_type;
$parts_uid = $json->parts_uid;
$parts_inven_uid = $json->parts_inven_uid;
$prev_parts_inven_uid = $json->prev_parts_inven_uid;
$login_key = $json->login_key;



if(CheckValidatedServerIP())
{
	if($parts_type == '114' || $parts_type == '115' || $parts_type == '116' || $parts_type == '117' || $parts_type == '118' || $parts_type == '119' || $parts_type == '120' || $parts_type == '121' || $parts_type == '122') //ring, necklace
	{
		SetEquipOnEquipment($account_id, $char_uid, $parts_type,$parts_uid, $parts_inven_uid, $prev_parts_inven_uid, $login_key);
	}
	else
	{	
		UpdateCurrentPartsSingle($account_id, $char_uid, $parts_type,$parts_uid, $parts_inven_uid, $prev_parts_inven_uid, $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	));
}
?>