<?php
include 'common.php';


function AlreadyExistsOwnedSkill($dbhandle,$char_uid, $skill_uid)
{
	$strQuery = "select count(*) from owned_skill where character_uid=".$char_uid." and skill_uid=".$skill_uid;
	if($query_result = mysqli_query($dbhandle, $strQuery)){
		$data = mysqli_fetch_row($query_result);
		if( $data[0] > 0 )
		{
			return true;
		}
	}
	
	return false;
}


function SaveOwnedSkill($account_id, $char_uid, $skill_uid,  $book_inven_uid, $book_update_count, $gold_update, $is_passive, $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_OWNED_SKILL_FAILED";
	
	if(AlreadyExistsOwnedSkill($dbhandle, $char_uid, $skill_uid))
	{
		$response = "SAVE_OWNED_SKILL_FAILED";
		$error = "AlreadyExistsOwnedSkill";
	}
	else
	{
		
		if(UpdateSkillBookCount($dbhandle, $char_uid, $book_inven_uid, $book_update_count ) && UpdateGold($dbhandle, $gold_update, $char_uid))
		{
			$insert_query = "INSERT INTO owned_skill (character_uid, skill_uid, level, is_passive) VALUES (".$char_uid.",".$skill_uid.", 1,".$is_passive.")";

			//Query Result
			$result = @mysqli_query($dbhandle,$insert_query);
			$response = "SAVE_OWNED_SKILL_FAILED";
			$error = "";
			if($result)
			{
				$response = "SAVE_OWNED_SKILL_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_SKILL_BOOK_COUNT_FAILED";
			$error = "DB Error";
		}
		
	}
	
	
	
	 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,'skill_uid'=>$skill_uid,'error'=>$error));
}

$json = json_decode(file_get_contents('php://input'));
$account_id = $json->account_id;
$char_uid = $json->char_uid;
$login_key = $json->login_key;
$skill_uid = $json->skill_uid;
$book_inven_uid = $json->book_inven_uid;
$book_update_count = $json->book_update_count;
$gold_update = $json->gold_update;
$is_passive = $json->is_passive;




if(CheckValidatedServerIP())
{
	SaveOwnedSkill($account_id, $char_uid, $skill_uid, $book_inven_uid, $book_update_count, $gold_update, $is_passive, $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	));
}



?>