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();
};


/**
 * Pokkt Standard APIs
 **/

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

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

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

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

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

PokktExtension.prototype.updatePokktConfig = function (pokktConfig) {
    currentPokktConfig = pokktConfig;
    notifyNative('updatePokktConfig', pokktConfig);
};

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


/**
 * Session related APIs
 **/

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

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

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

PokktExtension.prototype.trackIAP= function (details) {
    notifyNative('trackIAP', details);
};


/**
 * Offerwall APIs
 **/

PokktExtension.prototype.getCoins = function (assetValue) {
    currentPokktConfig.offerwallAssetValue = assetValue;
    notifyNative('getCoins', currentPokktConfig);
};

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

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


/**
 * Ad related APIs
 **/

PokktExtension.prototype.cacheAd = function(adConfig) {
    notifyNative('cacheAd', adConfig);
};

PokktExtension.prototype.showAd = function(adConfig) {
    notifyNative('showAd', adConfig);};

PokktExtension.prototype.checkAdAvailability = function(adConfig) {
    notifyNative('checkAdAvailability', adConfig);
};

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

PokktExtension.prototype.createAdConfig = function () {
    return {
        screenName: '',
        isRewarded: false,

        defaultSkipTime: 5,
        shouldAllowSkip: true,
        shouldAllowMute: true,
        shouldConfirmSkip: true,
        skipConfirmMessage: 'Do you want to skip the video?',
        skipConfirmYesLabel: 'Okay',
        skipConfirmNoLabel: 'Nope',
        skipTimerMessage: 'Must watch for ## seconds',
        incentiveMessage: 'more seconds to go!'
    };
};

PokktExtension.prototype.createPokktConfig = function () {
    /*
         IntegrationType:
         OfferwallAndVideo - 0
         OfferwallOnly - 1
         VideoOnly - 2
     */
    return {
        // essential
        securityKey: '',
        applicationId: '',
        integrationType: 0,
        autoCaching: false,
        thirdPartyUserId: '',

        // offer wall specific
        offerwallAssetValue: 0,
        closeOnSuccessFlag: true,

        // optional
        name: '',
        age: '',
        sex: '',
        mobileNo: '',
        emailAddress: '',
        location: '',
        birthday: '',
        maritalStatus: '',
        facebookId: '',
        twitterHandle: '',
        education: '',
        nationality: '',
        employment: '',
        maturityRating: '',

        // analytics
        googleAnalyticsID: '',
        mixPanelProjectToken: '',
        flurryApplicationKey: '',
        selectedAnalyticsType: ''
    };
};


// utility
PokktExtension.prototype.parseBool = function (value) {
    return value === true || value === 'true';
};


// 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:JSON.stringify(params)
	};
	
	if (nativeExt != null) {
		var r = nativeExt.notifyNative(args);
	}
	else {
		Ti.API.info('failed to notify native, extenion not available!');
	}
}


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