import React from 'react';
import {
NativeModules
} from 'react-native';
var PokktExtension = function () {};

/**
 * Pokkt Standard APIs
 **/
 
PokktExtension.prototype.setPokktConfig = function (appId, securityKey) {
    notifyNative('setPokktConfig', undefined, JSON.stringify({"appId":appId,"securityKey":securityKey}));
};

PokktExtension.prototype.setDataAccessConsent = function (GDPRApplicable, GDPRConsentAvailable) {
    notifyNative('setDataAccessConsent', undefined, JSON.stringify({"GDPRApplicable":GDPRApplicable,"GDPRConsentAvailable":GDPRConsentAvailable}));
};

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

PokktExtension.prototype.getSDKVersion = function (callback) {
    notifyNative('getSDKVersion', callback);
};

PokktExtension.prototype.setAdViewConfig = function(adViewConfig) {
    notifyNative('setAdViewConfig', undefined, adViewConfig);
};

PokktExtension.prototype.setUserDetails = function(userDetails) {
    notifyNative('setUserDetails', undefined, userDetails);
};
/**
 * Values that developers need in the POKKT server callback
 */
PokktExtension.prototype.setCallbackExtraParams = function(extraParams) {
    notifyNative('setCallbackExtraParams', undefined, extraParams);
};

/**
 * Debugging related APIs
 **/
 
PokktExtension.prototype.setDebug = function (debug) {
    notifyNative('Debugging_shouldDebug', undefined, debug);
};

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

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

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

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


/**
 * Video Ad related APIs
 **/

PokktExtension.prototype.cacheRewardedVideoAd = function(screenName) {
    notifyNative('VideoAd_cacheRewarded', undefined, screenName);
};

PokktExtension.prototype.cacheNonRewardedVideoAd = function(screenName) {
    notifyNative('VideoAd_cacheNonRewarded', undefined, screenName);
};

PokktExtension.prototype.showRewardedVideoAd = function(screenName) {
    notifyNative('VideoAd_showRewarded', undefined, screenName);
};

PokktExtension.prototype.showNonRewardedVideoAd = function(screenName) {
    notifyNative('VideoAd_showNonRewarded', undefined, screenName);
};

PokktExtension.prototype.isVideoAdCached = function(screenName,isRewarded, callback) {
    notifyNative('VideoAd_isAdCached', callback, JSON.stringify({"screenName":screenName,"isRewarded":isRewarded}));
};

/**
 * Interstitial related APIs
 **/

PokktExtension.prototype.cacheRewardedInterstitial = function(screenName) {
    notifyNative('Interstitial_cacheRewarded', undefined, screenName);
};

PokktExtension.prototype.cacheNonRewardedInterstitial = function(screenName) {
    notifyNative('Interstitial_cacheNonRewarded', undefined, screenName);
};

PokktExtension.prototype.showRewardedInterstitial = function(screenName) {
    notifyNative('Interstitial_showRewarded', undefined, screenName);
};

PokktExtension.prototype.showNonRewardedInterstitial = function(screenName) {
    notifyNative('Interstitial_showNonRewarded', undefined, screenName);
};

PokktExtension.prototype.isInterstitialAdCached = function(screenName,isRewarded, callback) {    
    notifyNative('Interstitial_isAdCached', callback, JSON.stringify({"screenName":screenName,"isRewarded":isRewarded}));
};
/**
 * Banner
 */
PokktExtension.prototype.loadBanner = function(screenName, position) {
    notifyNative('Banner_loadBanner', undefined, JSON.stringify({"screenName":screenName,"bannerPosition":position}));
};

PokktExtension.prototype.loadBannerWithRect = function(screenName, width, height) {
    notifyNative('Banner_loadBannerWithRect', undefined, JSON.stringify({"screenName":screenName,"width":width,"height":height}));
};

PokktExtension.prototype.shouldAutoRefresh = function(autoRefresh) {
    notifyNative('Banner_shouldAutoRefresh', undefined, autoRefresh);
};

PokktExtension.prototype.destroyBanner = function(screenName) {
    var param = {
    "screenName" : screenName,
    "bannerPosition" : '8'
    }
    notifyNative('Banner_destroyBanner', undefined, JSON.stringify(param));
};

/**
 * Analytics related APIs
 **/

PokktExtension.prototype.setAnalyticsDetails = function(analyticsDetails) {
    notifyNative('Analytics_setAnalyticsDetails', undefined, analyticsDetails);
};

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

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

/**
 * createAdPlayerViewConfig Method
 **/

PokktExtension.prototype.createAdViewConfig = function () {
    return {
        backButtonDisabled:true,
        shouldAllowSkip: true,
        defaultSkipTime: 5,
        skipConfirmMessage: 'Do you want to skip the video?',
        shouldAllowMute: true,
        shouldSkipConfirm: true,
        skipConfirmYesLabel: 'Okay',
        skipConfirmNoLabel: 'Nope',
        skipTimerMessage: 'Must watch for ## seconds',
        incentiveMessage: '## more seconds to go!',
        shouldCollectFeedback: true,
        isAudioEnabled:true
    };
};

/**
 * createUserDetails Method
 **/

PokktExtension.prototype.createUserDetails = function () {
    return {
     	name: '',
        age: '',
        sex: '',
        mobileNo: '',
        emailAddress: '',
        location: '',
        birthday: '',
        maritalStatus: '',
        facebookId: '',
        twitterHandle: '',
        education: '',
        nationality: '',
        employment: '',
        maturityRating: ''
    };
};

PokktExtension.prototype.createAnalyticsDetails = function () {
    return {
     	googleAnalyticsID: '',
        mixPanelProjectToken: '',
        flurryApplicationKey: '',
        selectedAnalyticsType: ''
		};
};

/**
 * InAppPurchase Method
 **/
PokktExtension.prototype.createInAppPurchaseDetail = function () {
    return {
        productId: 'xxx',
        price: 10,
        currencyCode: 'INR',
        title: 'Buying Life',
        description: 'Buying Life to continue play',
        purchaseData: '',
        purchaseSignature: '',
        purchaseStore: 'GOOGLE'//NONE,GOOGLE,IOS,AMAZON;
    };
};
// ------- NATIVE NOTIFIERS -------

function notifyNative(operation, successCallback, param) {
    if (successCallback == null || successCallback == undefined) {
        successCallback = function (result) {
            console.log(operation + ' was successful! result: ' + result);
        };
    }

    var errorCallback = function(error) {
        console.log(operation + ' failed! error: ' + error);
    };

    console.log('executing operation: ' + operation + ' with param: ' + param);
    NativeModules.PokktPlugin.execute(operation,param,successCallback);
}


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

if (!window.plugins) {
    window.plugins = {};
}

if (!window.plugins.pokktExtension) {
    window.plugins.pokktExtension = new PokktExtension();
}

if (typeof module != 'undefined' && module.exports) {
    module.exports = PokktExtension;
}
// ------------------------------
