var nativeExt = null;

var PokktExtension = function () {
	Ti.API.info('acquring native extension for ' + Ti.Platform.name);
	if (Ti.Platform.name == 'android') {
		nativeExt = require('com.pokkt.titaniumandroid');
	}
	else if (Ti.Platform.name == 'iPhone OS') {
		nativeExt = require('com.pokkt.titaniumios');
	}
	
	registerListener();
};


// Utility Methods

PokktExtension.prototype.setDebug = function (debug) {
    notifyNative('setDebug', debug);
};

PokktExtension.prototype.showLog = function (message) {
    notifyNative('showLog', message);
};

PokktExtension.prototype.showToast = function (message) {
    notifyNative('showToast', message);
};

PokktExtension.prototype.exportLog = function () {
    notifyNative('exportLog');
};

PokktExtension.prototype.setThirdPartyUserId = function (id) {
	notifyNative('setThirdPartyUserId', id);
};


// Essential Methods

PokktExtension.prototype.setSecurityKey = function (securityKey) {
	notifyNative('setSecurityKey', securityKey);
};

PokktExtension.prototype.setApplicationId = function (applicationId) {
	notifyNative('setApplicationId', applicationId);
};

PokktExtension.prototype.setIntegrationType = function (integrationType) {
	notifyNative('setIntegrationType', integrationType);
};

PokktExtension.prototype.setAutoCaching = function (autoCaching) {
	notifyNative('setAutoCaching', autoCaching);
};

PokktExtension.prototype.initPokkt = function () {
    notifyNative('initPokkt');
};

PokktExtension.prototype.sendAppInfo = function () {
    notifyNative('sendAppInfo');
};


// Session Methods

PokktExtension.prototype.startSession = function () {
    notifyNative('startSession');
};

PokktExtension.prototype.endSession = function () {
    notifyNative('endSession');
};


// OfferWall Methods

PokktExtension.prototype.getCoins = function (assetValue) {
    if (assetValue != undefined || assetValue  != null)
        this.setOfferwallAssetValue(assetValue);
    notifyNative('getCoins');
};

PokktExtension.prototype.getPendingCoins = function () {
    notifyNative('getPendingCoins');
};

PokktExtension.prototype.checkOfferWallCampaign = function () {
    notifyNative('checkOfferWallCampaign');
};

PokktExtension.prototype.setOfferwallAssetValue = function (assetValue) {
	notifyNative('setOfferwallAssetValue', assetValue);
};

PokktExtension.prototype.setCloseOnSuccessFlag = function (flag) {
	notifyNative('setCloseOnSuccessFlag', flag);
};


// Video Methods

PokktExtension.prototype.getVideo = function (screenName) {
    //this.setScreenName(screenName);
    //this.setIncentivised(true);
    notifyNative('getVideo', screenName);
};

PokktExtension.prototype.getVideoNonIncent = function (screenName) {
    //this.setScreenName(screenName);
    //this.setIncentivised(false);
    notifyNative('getVideo', screenName);
};

PokktExtension.prototype.cacheVideoCampaign = function () {
    notifyNative('cacheVideoCampaign');
};

PokktExtension.prototype.setSkipEnabled = function (value) {
	notifyNative('setSkipEnabled', value);
};

PokktExtension.prototype.setDefaultSkipTime = function (value) {
	notifyNative('setDefaultSkipTime', value);
};

PokktExtension.prototype.setCustomSkipMessage = function (message) {
	notifyNative('setCustomSkipMessage', message);
};

// PokktExtension.prototype.setScreenName = function (screenName) {
	// notifyNative('setScreenName', screenName);
// };
// 
// PokktExtension.prototype.setIncentivised = function (incentivised) {
	// notifyNative('setIncentivised', incentivised);
// };

PokktExtension.prototype.setBackButtonDisabled = function (value) {
	notifyNative('setBackButtonDisabled', value);
};


// Optional Methods

PokktExtension.prototype.setName = function (name) {
	notifyNative('setName', name);
};

PokktExtension.prototype.setAge = function (age) {
	notifyNative('setAge', age);
};

PokktExtension.prototype.setSex = function (sex) {
	notifyNative('setSex', sex);
};

PokktExtension.prototype.setMobileNo = function (mobileNo) {
	notifyNative('setMobileNo', mobileNo);
};

PokktExtension.prototype.setEmailAddress = function (email) {
	notifyNative('setEmailAddress', email);
};

PokktExtension.prototype.setLocation = function (location) {
	notifyNative('setLocation', location);
};

PokktExtension.prototype.setBirthday = function (birthday) {
	notifyNative('setBirthday', birthday);
};

PokktExtension.prototype.setMaritalStatus = function (maritalStatus) {
	notifyNative('setMaritalStatus', maritalStatus);
};

PokktExtension.prototype.setFacebookId = function (fbId) {
	notifyNative('setFacebookId', fbId);
};

PokktExtension.prototype.setTwitterHandle = function (handle) {
	notifyNative('setTwitterHandle', handle);
};

PokktExtension.prototype.setEducation = function (education) {
	notifyNative('setEducation', education);
};

PokktExtension.prototype.setNationality = function (nationality) {
	notifyNative('setNationality', nationality);
};

PokktExtension.prototype.setEmployment = function (employment) {
	notifyNative('setEmployment', employment);
};

PokktExtension.prototype.setMaturityRating = function (rating) {
	notifyNative('setMaturityRating', rating);
};


// Analytics Methods
    
PokktExtension.prototype.setSelectedAnalyticsType = function (selectedAnalyticsType) {
	notifyNative('setSelectedAnalyticsType', selectedAnalyticsType);
};

PokktExtension.prototype.setGoogleAnalyticsID = function (googleAnalyticsID) {
	notifyNative('setGoogleAnalyticsID', googleAnalyticsID);
};

PokktExtension.prototype.setMixPanelProjectToken = function (mixPanelProjectToken) {
	notifyNative('setMixPanelProjectToken', mixPanelProjectToken);
};

PokktExtension.prototype.setFlurryApplicationKey = function (flurryApplicationKey) {
	notifyNative('setFlurryApplicationKey', flurryApplicationKey);
};


// ------------------------------

PokktExtension.prototype.isVideoAvailable = function () {
	return nativeExt.isVideoAvailable();
};

PokktExtension.prototype.getVideoVc = function () {
    return nativeExt.getVideoVc();
};

PokktExtension.prototype.getPokktSDKVersion = function (callback) {
	return nativeExt.getPokktSDKVersion();
};


// native listner
function registerListener() {
	Ti.API.info('rgistering listener with extension...');
	if (nativeExt != null) {
		nativeExt.registerCallbacks({
			notifyJS:listenJSEvents
		});	
	}
	else {
		Ti.API.info('failed to register listener, extenion not available!');
	}
}

function listenJSEvents(args) {
	var operation = args.operation;
	var params = args.params;
	Ti.API.info("notified of " + operation + ", with params: " + params);
	
	Ti.App.fireEvent(operation, { params: params });
}


// native extension

function notifyNative(operation, params) {
	// in case of params not available, set it with empty string
	if (params == undefined || params == null) {
		params = '';
	}
	
	var args = {
		operation:operation,
		params:params
	};
	
	if (nativeExt != null) {
		nativeExt.notifyNative(args);
	}
	else {
		Ti.API.info('failed to notify native, extenion not available!');
	}
}


// ------------------------------
var pe = new PokktExtension();
module.exports = pe;
// ------------------------------
