/**
 * @description 更多游戏按钮
 * @author kaka
 */

const { ccclass, property } = cc._decorator;

@ccclass
export default class BtnMoreGame extends cc.Component {
    @property(cc.Sprite)
    sp_moregame: cc.Sprite = null;

    @property(cc.Label)
    lab_moregame: cc.Label = null;

    update() {
        if (gData.moreGame.init_moreGameList) {
            this.setMoreGameSp(gData.moreGame.moreGameList.normal);
        }
    }

    lateUpdate() {
        gData.moreGame.init_moreGameList = false;
    }

    /** 设置更多游戏图片 */
    setMoreGameSp(arr) {
        let len = arr.length
        let index = mk.math.random(0, len - 1)
        let data = arr[index]
        this.showMoreGameSp(data)

        this.schedule(() => {
            index++
            index = (index % len)
            // LogUtil.logV('setMoreGameSp  ', 'index ' + index)
            data = arr[index]
            this.showMoreGameSp(data)
        }, 10)
    }

    /** 切换图片 */
    showMoreGameSp(data) {
        //pc不加载，加载会报错
        if (cc.sys.os != cc.sys.OS_WINDOWS) {
            cc.loader.load(data.icon, (err, res) => {
                if (err) {
                    console.log('err:', err);
                    return;
                }
                if (this.sp_moregame) {
                    let tex: cc.Texture2D = res as cc.Texture2D;
                    this.sp_moregame.spriteFrame = new cc.SpriteFrame(tex);
                }
            })
        }

        this.lab_moregame.string = data.title
    }
}
