// Learn cc.Class:
//  - [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
var self = null;
cc.Class({
    extends: cc.Component,
    ctor(){
        self = this;
    },
    properties: {
        type: 1,//浮动跳转app的UI类型，1：左边；2：右边；3：底部的猜你喜欢
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    /**
     * 
     * @param {Number} type 浮动跳转app的UI类型，1：左边；2：右边；3：底部的猜你喜欢
     */
    init(type=1,parentNode){
        self.type = type?type:1;
        self.parentNode = parentNode;
        if(cc.HQ.GameMgr.navAppData){
            self.apply(type,parentNode);
        }else{
            cc.HQ.GameMgr.getNavAppData().then(res => {
                self.apply(type,parentNode);
            })
        }
    },
    /**
     * 渲染数据
     * @param {*} type 
     */
    apply(type,parentNode){
        // console.log('apply',self.type);
        if(self.type === 1){
            let widget = self.node.getComponent(cc.Widget);
            widget.target = cc.find('Canvas');
            widget.isAlignOnce =false;
            widget.isAlignLeft =true;
            widget.left = 20;
            widget.top = 450;
            widget.updateAlignment();
            let logo = self.node.getChildByName('logo').getComponent(cc.Sprite);
            self.appData = cc.HQ.GameMgr.getCurFloadAppData();
            sdk.createImage(logo,self.appData.iconImageUrl);
            self.addEvent(self.appData.appId,self.node);
        }else if(self.type === 2){
            let widget = self.node.getComponent(cc.Widget);
            widget.target = cc.find('Canvas');
            widget.isAlignRight =true;
            widget.right = 20;
            widget.top = 180;
            widget.updateAlignment();
            let logo = self.node.getChildByName('logo').getComponent(cc.Sprite);
            self.appData = cc.HQ.GameMgr.getCurFloadAppData();
            sdk.createImage(logo,self.appData.iconImageUrl);
            self.addEvent(self.appData.appId,self.node);
        }else if(self.type === 3){
            let children = self.node.children;
            let appDatas = cc.HQ.GameMgr.getCurLikeAppData();
            children.map((itemNode,index)=>{
                // console.log(itemNode,index);
                sdk.createImage(itemNode.getComponent(cc.Sprite),appDatas[index].iconImageUrl);
                self.addEvent(appDatas[index].appId,itemNode);
            })
        }
        self.timeoutId = setTimeout(()=>{
            self.apply();
        },30000)
        // self.scheduleOnce(self.timeCallback,6);
    },
    addEvent(appid,node){
        // console.log('appid',appid)
        //绑定事件
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = self.node;
        clickEventHandler.component = "floatBoxUI";
        clickEventHandler.handler = "onBtn";
        clickEventHandler.customEventData = appid;
        let btn = node.getComponent(cc.Button);
        if(btn.clickEvents.length>0){
            btn.clickEvents[0] = clickEventHandler;
        }else{
            btn.clickEvents.push(clickEventHandler);
        }
    },
    start () {

    },
    onDisable(){
        clearTimeout(self.timeoutId);
        self.node.destroy();
        // self.unschedule(self.timeCallback);//取消计时器
    },
    onBtn(event, appid){
        let appData = cc.HQ.GameMgr.getAppDataByAppid(appid);
        cc.HQ.GameMgr.onPlayingOtherGame(appData.appId,appData.path);
        //埋点
        let btnType = self.type === 1?'左边浮动跳小程序': self.type === 2?'右边浮动跳小程序': '猜你喜欢小程序'
        sdk.setSceneEvent(btnType,"点击",appData.name,{},null);
    }
    // update (dt) {},
});
