/* -----------------------------------------------------------------------------
	nEngine - GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "squirrel.h"
		#include "script_squirrel/legacy/binding_helpers.h"
		#include "licensing/licensing.h"
		#include "analytics/analytics.h"
		#include "billing/billing.h"
		#include "locale/country.h"
		#include "platform.h"
		#include "log/log.h"

		using namespace GS;


//------------------------------------------------------------------------------
SQInteger			PlatformAnalyticsLogEvent(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETSTRING(name)
	if	(Platform::Get().analytics.IsValid())
		Platform::Get().analytics->logEvent(name);
	__SQ_GETEND
	__SQ_RETURN
}
SQInteger			PlatformAnalyticsServeFullscreenAd(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETSTRING(location)
	if	(Platform::Get().analytics.IsValid())
		Platform::Get().analytics->serveFullscreenAd(location);
	__SQ_GETEND
	__SQ_RETURN
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PlatformBillingConfirmEvent(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETSTRING(id)
	if	(Platform::Get().billing.IsNull())
		return sq_throwerror(vm, "No billing system on this platform.");
	Platform::Get().billing->confirmPurchase(id);
	__SQ_GETEND
	__SQ_RETURN
}
SQInteger			PlatformBillingRestorePurchases(HSQUIRRELVM vm)
{
	if	(Platform::Get().billing.IsNull())
		return sq_throwerror(vm, "No billing system on this platform.");
	Platform::Get().billing->restorePurchases();
	__SQ_RETURN
}
SQInteger			PlatformBillingRequestPurchase(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETSTRING(id)
	if	(Platform::Get().billing.IsNull())
		return sq_throwerror(vm, "No billing system on this platform.");
	Platform::Get().billing->requestPurchase(id);
	__SQ_GETEND
	__SQ_RETURN
}
SQInteger			PlatformBillingIsSupported(HSQUIRRELVM vm)
{	__SQ_RETURNBOOL(Platform::Get().billing.IsValid())	}
SQInteger			PlatformGetLocale(HSQUIRRELVM vm)
{	__SQ_RETURNSTRING(Platform::Get().GetLocale())	}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PlatformLicensingIsSupported(HSQUIRRELVM vm)
{
	__SQ_RETURNBOOL(Platform::Get().licensing.IsValid())
}
SQInteger			PlatformLicensingCheck(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETSTRING(id)
	if	(Platform::Get().licensing.IsNull())
		return sq_throwerror(vm, "No licensing system on this platform.");
	Platform::Get().licensing->updateLicence(id);
	__SQ_GETEND
	__SQ_RETURNBOOL(true)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PlatformGetName(HSQUIRRELVM vm)
{	__SQ_RETURNSTRING(Platform::Get().GetName())	}
SQInteger			PlatformGetDeviceName(HSQUIRRELVM vm)
{	__SQ_RETURNSTRING(Platform::Get().GetDeviceName())	}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PlatformOpenURL(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETSTRING(url)
	bool r = Platform::Get().OpenURL(url);
	__SQ_GETEND
	__SQ_RETURNBOOL(r)
}
SQInteger			PlatformSendToBackground(HSQUIRRELVM vm)
{
	__SQ_GETSTART(1)
	__SQ_GETBOOL(kill)
	__SQ_GETEND
	__SQ_RETURNBOOL(Platform::Get().SendToBackground(asbool(kill)))
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PlatformGetUserPath(HSQUIRRELVM vm)
{
	String path;
	if	(!Platform::Get().GetUserDir(path))
		return sq_throwerror(vm, "Failed to get user path.");
	__SQ_RETURNSTRING(path)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			PlatformOpenAppPage(HSQUIRRELVM vm)
{
	Platform::Get().OpenAppPage();
	__SQ_RETURN
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void				RegisterPlatformBinding(HSQUIRRELVM vm)
{
	using namespace GS::Script;

/*#
	Section: SystemMisc
	Desc: Miscellaneous
#*/
	/*#
		Func: PlatformOpenURL
		Proto: bool:String id
		Desc: Open an URL using the platform browser if available.
	#*/
	sq_register(vm, PlatformOpenURL, "PlatformOpenURL", _SC(".s"));
	/*#
		Func: PlatformSendToBackground
		Proto: bool:bool kill
		Desc: Send the current application to background, optionally try to kill it.
	#*/
	sq_register(vm, PlatformSendToBackground, "PlatformSendToBackground", _SC(".b"));

	/*#
		Func: PlatformGetUserPath
		Proto: String:
		Desc: Return the current user path.
	#*/
	sq_register(vm, PlatformGetUserPath, "PlatformGetUserPath", _SC("."));

/*#
	Section: SystemVisibility
	Desc: Platform visibility
#*/
	/*#
		Func: PlatformOpenAppPage
		Proto: void:
		Desc: Open the platform specific application page, use this to send the user to your rating page.
	#*/
	sq_register(vm, PlatformOpenAppPage, "PlatformOpenAppPage", _SC("."));

/*#
	Section: SystemLicensing
	Desc: Licensing
#*/
	/*#
		Func: PlatformLicensingCheck
		Proto: void:String license_info
		Desc: Launch an asynchronous licensing request, the result will be sent to your script global 'OnLicensingEvent(String event)' function.
	#*/
	sq_register(vm, PlatformLicensingCheck, "PlatformLicensingCheck", _SC(".s"));
	/*#
		Func: PlatformLicensingIsSupported
		Proto: bool:
		Desc: Returns true if the current platform supports app license verification.
	#*/
	sq_register(vm, PlatformLicensingIsSupported, "PlatformLicensingIsSupported", _SC("."));

/*#
	Section: SystemBilling
	Desc: In-app billing
#*/
	/*#
		Func: PlatformBillingConfirmEvent
		Proto: void:String id
		Desc: Confirm a billing event (like a purchase or a refund).
	#*/
	sq_register(vm, PlatformBillingConfirmEvent, "PlatformBillingConfirmEvent", _SC(".s"));
	/*#
		Func: PlatformBillingRestorePurchases
		Proto: void:
		Desc: Restore all managed purchases. The billing event callback will be called for each purchase ever made by the current user with the event string "Restored".
	#*/
	sq_register(vm, PlatformBillingRestorePurchases, "PlatformBillingRestorePurchases", _SC("."));
	/*#
		Func: PlatformBillingRequestPurchase
		Proto: void:String id
		Desc: Launch a purchase intent for the specified virtual good identifier. Returns true of the intent was successfully launched. Results will be transmitted to you through the global 'OnBillingEvent(String event, String item)' script callback.
	#*/
	sq_register(vm, PlatformBillingRequestPurchase, "PlatformBillingRequestPurchase", _SC(".s"));
	/*#
		Func: PlatformBillingIsSupported
		Proto: bool:
		Desc: Returns true if the current platform supports in-app billing (in-app purchase).
	#*/
	sq_register(vm, PlatformBillingIsSupported, "PlatformBillingIsSupported", _SC("."));
	/*#
		Func: PlatformGetLocale
		Proto: String:
		Desc: Return the current platform locale ISO2 code string (ISO 3166 two-character alphabetic code).
	#*/
	sq_register(vm, PlatformGetLocale, "PlatformGetLocale", _SC("."));

/*#
	Section: SystemAnalytics
	Desc: Analytics
#*/
	/*#
		Func: PlatformAnalyticsLogEvent
		Proto: void:String name
		Desc: Log a named analytics event.
	#*/
	sq_register(vm, PlatformAnalyticsLogEvent, "PlatformAnalyticsLogEvent", _SC(".s"));
	/*#
		Func: PlatformAnalyticsServeFullscreenAd
		Proto: void:String location
		Desc: Serve a fullscreen ad, the location string is used as a marker to track the location of the display.
	#*/
	sq_register(vm, PlatformAnalyticsServeFullscreenAd, "PlatformAnalyticsServeFullscreenAd", _SC(".s"));

/*#
	Section: SystemHardware
	Desc: Host device
#*/
	/*#
		Func: PlatformGetName
		Proto: String:
		Desc: Return the platform name (eg. "Win32", "iOS", "Android", ...).
		Note: A platform may run on several devices.
fd		See: PlatformGetDeviceName
	#*/
	sq_register(vm, PlatformGetDeviceName, "PlatformGetDeviceName", _SC("."));
	/*#
		Func: PlatformGetDeviceName
		Proto: String:
		Desc: Return the device name the platform is currently running on (eg. "iPad", "iPhone").
	#*/
	sq_register(vm, PlatformGetDeviceName, "PlatformGetDeviceName", _SC("."));
}
//------------------------------------------------------------------------------
