/** 
 * @description 互推banner 单个item
 * @author kaka
 */
import JsbSystem from "../../../mk/system/JsbSystem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MoreGamePicItem extends cc.Component {

    @property(cc.Sprite)
    banner: cc.Sprite = null;

    @property(cc.Node)
    spRed: cc.Node = null;

    @property(cc.Sprite)
    spPro: cc.Sprite = null;

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

    @property(cc.Node)
    btnStart: cc.Node = null;

    data = null
    cool = false

    init(data) {
        this.data = data

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

        this.spPro.node.active = false
        this.btnStart.active = false
        this.labPro.string = ''
        //打开
        if (JsbSystem.ifCanStartGame(data.packageName, false)) {
            this.spRed.active = false
            this.btnStart.active = true
        } else {
            if (JsbSystem.isFileExist(data.nebulaAppId + ".apk")) {
                //安装
                this.spRed.active = false
                this.btnStart.active = true
            } else {
                //下载
                this.spRed.active = true
            }
        }
    }

    creatorDownLoadTask() {
        // UiM.Instance.moreGameNode.getComponent(MoreGameNode).hasClick = true
        if (this.cool) {
            return
        }

        this.cool = true
        this.scheduleOnce(() => {
            this.cool = false
        }, 3)

        let result = gData.moreGame.createNewTask(this.data)
        if (result != null) {
            this.schedule(() => {
                this.showDownloadProgress()
            }, 0.5)
        }
    }

    showDownloadProgress() {
        let info = gData.moreGame.getTaskInfo(this.data.nebulaAppId)
        if (info != null) {
            if (info.progress >= 0) {
                // LogUtil.logV("showDownloadProgress", "three")
                this.spPro.node.active = true
                let proNum = info.progress / info.totalBytesReceives
                let pro = (proNum * 100).toFixed(1)
                this.spPro.fillRange = 1 - proNum
                this.labPro.string = `${pro}%`
            } else {
                this.init(this.data)
            }
        } else {
            this.init(this.data)
        }
    }
}
