// 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: 3,//浮动跳转app的UI类型，1：左边；2：右边；3：底部的猜你喜欢
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    /**
     * 
     * @param {Number} type 浮动跳转app的UI类型，1：左边；2：右边；3：底部的猜你喜欢
     */
    init(type=3,parentNode){
        self.node.active =true;
        self.type = type?type:3;
        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);
        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);
            if(cc.HQ.CacheManager.getObject(appDatas[index].iconImageUrl) != null){
                // 如果有缓存
                console.log("get from cache");
                itemNode.getComponent(cc.Sprite).spriteFrame = cc.HQ.CacheManager.getObject(appDatas[index].iconImageUrl);
            }else{
                // 否则
                console.log("get from server");
                sdk.createImage(itemNode.getComponent(cc.Sprite), appDatas[index].iconImageUrl, (spriteFrame) => {
                    console.log("set to cache");
                    cc.HQ.CacheManager.setObject(appDatas[index].iconImageUrl, spriteFrame);
                });
            }

            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 = "guessYouLikeUI";
        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 = '猜你喜欢小程序'
        sdk.setSceneEvent(btnType,"点击",appData.name,{},null);
    }
    // update (dt) {},
});
