//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    properties:{
       
    },

    onLoad: function () {
        //Add this line to onLoad
        this.admobInit();
        console.log('加载admob');
        //this.showBanner();
    },


    admobInit: function() {
        if(cc.sys.isMobile) {
            var self = this
            sdkbox.PluginAdMob.setListener({
                adViewDidReceiveAd: function(name) {
                    console.log('adViewDidReceiveAd name=' + name);
                    self.showBanner();
                    //self.VideoIsOk(name);
                },
                adViewDidFailToReceiveAdWithError: function(name, msg) {
                    console.log('adViewDidFailToReceiveAdWithError name=' + name + ' msg=' + msg);
                    self.cacheAd(name);
                },
                adViewWillPresentScreen: function(name) {
                    console.log('adViewWillPresentScreen name=' + name);
                },
                adViewDidDismissScreen: function(name) {
                    console.log('adViewDidDismissScreen name=' + name);
                },
                adViewWillDismissScreen: function(name) {
                    console.log('adViewWillDismissScreen=' + name);
                },
                adViewWillLeaveApplication: function(name) {
                    console.log('adViewWillLeaveApplication=' + name);
                },
                reward(name, currency, amount){
                    console.log('adViewWillreward=' + name+currency+amount);
                    var coin = Storage.Get_storage('coin')
                    if(cc.find('GameOvre').active){
                        Storage.Change_storage('coin',coin+Global.merge_earn_coin);
                    }else{
                        Storage.Change_storage('coin',coin+400);
                    }
                }
            });
            sdkbox.PluginAdMob.init();
        }
    },

    cacheAd: function(name) {
        if(cc.sys.isMobile) {
            sdkbox.PluginAdMob.cache(name);
        }
    },

    showBanner: function() {
        if(cc.sys.isMobile) {
            sdkbox.PluginAdMob.show('home');
        }
    },
    showInterstitial: function() {
        if(cc.sys.isMobile) {
            sdkbox.PluginAdMob.show('gameover');
        }
    },
    showRewardVideo: function() {
        if(cc.sys.isMobile) {
            sdkbox.PluginAdMob.show('rewarded');
        }
    },
    VideoIsOk:function(name){
        if(name == 'rewarded'){
            Global.AD_IsLoad = true;
        }
    }

});