<?php
include 'common.php';


function AlreadyExistsOwnedPet($dbhandle,$char_uid, $pet_uid)
{
	$strQuery = "select count(*) from owned_pet where character_uid=".$char_uid." and pet_uid=".$pet_uid;
	if($query_result = mysqli_query($dbhandle, $strQuery)){
		$data = mysqli_fetch_row($query_result);
		if( $data[0] > 0 )
		{
			return true;
		}
	}
	
	return false;
}


function SaveOwnedPet($account_id, $char_uid, $pet_uid,  $card_inven_uid, $card_update_count, $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_PET_FAILED";
	
	if(AlreadyExistsOwnedPet($dbhandle, $char_uid, $pet_uid))
	{
		$response = "SAVE_OWNED_PET_FAILED";
		$error = "AlreadyExistsOwnedPet";
	}
	else
	{
        if(UpdatePetCardCount($dbhandle, $char_uid, $card_inven_uid, $card_update_count ))
		{
			$insert_query = "INSERT INTO owned_pet (character_uid, pet_uid, level) VALUES (".$char_uid.",".$pet_uid.", 1)";

			//Query Result
			$result = @mysqli_query($dbhandle,$insert_query);
			$response = "SAVE_OWNED_PET_FAILED";
			$error = "";
			if($result)
			{
				$response = "SAVE_OWNED_PET_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_PET_CARD_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,'pet_uid'=>$pet_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;
$pet_uid = $json->pet_uid;
$card_inven_uid = $json->card_inven_uid;
$card_update_count = $json->card_update_count;
//$gold_update = $json->gold_update;
//$is_passive = $json->is_passive;




if(CheckValidatedServerIP())
{
	  SaveOwnedPet($account_id, $char_uid, $pet_uid, $card_inven_uid, $card_update_count,$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	));
}



?>