window.Toast = {
    LENGTH_LONG: 3.5,
    LENGTH_SHORT: 2,
    CENTER: 0,
    TOP: 1,
    TOP_LEFT: 2,
    LEFT: 3,
    BOTTOM_LEFT: 4,
    BOTTOM: 5,
    BOTTOM_RIGHT: 6,
    RIGHT: 7,
    TOP_RIGHT: 8,
};

Toast.makeText = function (text, duration) {
    var _text;
    var _duration;
    var _gravity;
    var _x = 0;
    var _y = 0;
    var ToastObject = function (tText, tDuration) {
        _text = tText;
        _duration = tDuration;
        //
        this.setGravity = function (gravity, x, y) {
            _gravity = gravity;
            _x = x;
            _y = y;
        }
        //
        this.show = function () {
            // 加载背景纹理
            if (Toast.bgSpriteFrame === undefined) {
                self = this;
                (function () {
                    cc.loader.load({ 'uuid': 'b43ff3c2-02bb-4874-81f7-f2dea6970f18' },
                        function (error, result) {
                            if (error) {
                                cc.error(error);
                                return;
                            }
                            Toast.bgSpriteFrame = new cc.SpriteFrame(result);
                            Toast.bgSpriteFrame.insetTop = 3;
                            Toast.bgSpriteFrame.insetBottom = 3;
                            Toast.bgSpriteFrame.insetLeft = 4;
                            Toast.bgSpriteFrame.insetRight = 4;
                            //加载完再调用
                            self.show();
                        })
                })();
                return;
            }
            // canvas
            var canvas = cc.director.getScene().getComponentInChildren(cc.Canvas);
            var width = canvas.node.width;
            var height = canvas.node.height;
            if (_duration === undefined) {
                _duration = LENGTH_SHORT;
            }
            // 背景图片设置
            var bgNode = new cc.Node();
            // 背景图片透明度
            bgNode.opacity = 200;
            var bgSprite = bgNode.addComponent(cc.Sprite);
            bgSprite.type = cc.Sprite.Type.SLICED;
            var bgLayout = bgNode.addComponent(cc.Layout);
            bgLayout.resizeMode = cc.Layout.ResizeMode.CONTAINER;

            // Lable文本格式设置
            var textNode = new cc.Node();
            var textLabel = textNode.addComponent(cc.Label);
            textLabel.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
            textLabel.verticalAlign = cc.Label.VerticalAlign.CENTER;
            textLabel.fontSize = 30;
            textLabel.string = _text;

            //背景图片与文本内容的间距
            var hPadding = textLabel.fontSize / 8;
            var vPadding = 2;
            bgLayout.paddingLeft = hPadding;
            bgLayout.paddingRight = hPadding;
            bgLayout.paddingTop = vPadding;
            bgLayout.paddingBottom = vPadding;

            // 当文本宽度过长时，设置为自动换行格式
            if (_text.length * textLabel.fontSize > width / 3) {
                textNode.width = width / 3;
                textLabel.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
            }

            bgNode.addChild(textNode);
            if (Toast.bgSpriteFrame) {
                bgSprite.spriteFrame = Toast.bgSpriteFrame;
            }
            // gravity 设置Toast显示的位置
            if (_gravity == Toast.BOTTOM) {
                textNode.y = textNode.y - (height / 5) * 2;
            } else if (_gravity == Toast.TOP) {
                textNode.y = textNode.y + (height / 5) * 2;
            } else if (_gravity == Toast.TOP_LEFT) {
                textNode.y = textNode.y + (height / 5) * 2;
                textNode.x = textNode.x + (width / 5);
            } else if (_gravity == Toast.LEFT) {
                textNode.x = textNode.x + (width / 5);
            } else if (_gravity == Toast.BOTTOM_LEFT) {
                textNode.y = textNode.y - (height / 5) * 2;
                textNode.x = textNode.x + (width / 5);
            } else if (_gravity == Toast.BOTTOM) {
                textNode.y = textNode.y - (height / 5) * 2;
            } else if (_gravity == Toast.BOTTOM_RIGHT) {
                textNode.y = textNode.y - (height / 5) * 2;
                textNode.x = textNode.x - (width / 5);
            } else if (_gravity == Toast.RIGHT) {
                textNode.x = textNode.x - (width / 5);
            } else if (_gravity == Toast.TOP_RIGHT) {
                textNode.y = textNode.y + (height / 5) * 2;
                textNode.x = textNode.x - (width / 5);
            } else {
                // 默认情况 Center
                textNode.y = 0;
                textNode.x = 0;
            }
            textNode.x = textNode.x + _x;
            textNode.y = textNode.y + _y;

            canvas.node.addChild(bgNode);

            var finished = cc.callFunc(function (target) {
                bgNode.destroy();
            }, self);
            var action = cc.sequence(cc.moveBy(_duration,cc.p(0,0)),cc.fadeOut(0.3), finished);
            bgNode.runAction(action);
        }
    }

    return new ToastObject(text, duration);
};

Toast.showText = function (text, duration) {
    Toast.makeText(text, duration).show();
};
Toast.Show_My_Text = function(text){
    var toast = cc.find('Canvas/Toast');
    toast.getComponent(cc.Label).string = text;
    toast.getComponent(cc.Label).unscheduleAllCallbacks();//取消全部定时器，防止多次点击
    toast.getComponent(cc.Label).scheduleOnce(function() {
        // 这里的 this 指向 component
        toast.getComponent(cc.Label).string = '';
    }, 2);
};
Toast.GetCoins = function(){
    Storage.ChangeCoin(40);
    Toast.Show_My_Text('Add 40 Coins!');
    if(Global.Video_Rewords_style == 1){
        if(Global.Level_Now > Global.Level_Pass){
            Storage.Add_Level_Pass(Global.Level_Pass);
        }
        //获得奖励之后直接到下一关了
        Global.Level_Now ++;
        
        cc.find('Canvas/Main').active = true;//还需要调用Main的初始化
        // self.canvas.getChildByName('Store').active = false;
        cc.find('Canvas/Pop').active = false;

        //self.Level_number = Global.Level_Info.length-1;

        if(Global.Level_Now == Global.Level_Info.length){
            Toast.Show_My_Text('All level passed!');
            return;
        }else{
            var mainlogo = cc.find('Canvas/Main/Main');
            mainlogo.getComponent('Game_chouse_logo').init();
        }
    }
    
    //cc.find('Canvas/Pop/Pass').active = false;
    //播放音频
    //cc.find('Canvas/Pop/Pass/GetCoins').getComponent(cc.AudioSource).play();
    //归0
    Global.AD_IsLoad = false;
    Global.Coin_Double = false;
}

//导出为引用模块
module.exports = Toast;