/** 
 * @description 互推界面常规item
 * @author kaka
 */
import JsbSystem from "../../../mk/system/JsbSystem";


const { ccclass, property } = cc._decorator;

@ccclass
export default class MoreGameNormalItem extends cc.Component {

    @property(cc.Sprite)
    iconNode: cc.Sprite = null;
    @property(cc.Label)
    progressLabel: cc.Label = null;

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

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

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

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

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

    private data = null

    /**
     * list_data
     * @param data item数据
     */
    public async setItemData(bData) {
        this.data = bData.item_data;
        if (cc.sys.os != cc.sys.OS_WINDOWS) {
            cc.loader.load(this.data.icon, (err, res) => {
                if (err) {
                    console.log('err:', err);
                    return;
                }
                if (cc.isValid(this.iconNode)) {
                    let tex: cc.Texture2D = res as cc.Texture2D;
                    this.iconNode.spriteFrame = new cc.SpriteFrame(tex);
                }
            })
        }

        this.labName.string = this.data.title
        this.labDes.string = this.data.introduction
        this.spRed.active = false

        if (JsbSystem.ifCanStartGame(this.data.packageName, false)) {
            this.btnDown.active = false
            this.btnOpen.active = true
        } else {
            if (JsbSystem.isFileExist(this.data.nebulaAppId + ".apk")) {
                this.btnDown.active = false
                this.btnOpen.active = true
            } else {
                this.btnDown.active = true
                this.btnOpen.active = false
                // this.spRed.active = true

                let info = gData.moreGame.getTaskInfo(this.data.nebulaAppId)
                if (info != null) {
                    this.schedule(() => {
                        this.showDownloadProgress()
                    }, 0.5)
                } else {
                    this.progressLabel.string = "下载"
                }
            }
        }
    }

    cool = false
    creatorDownLoadTask() {
        mk.audio.playEffect('button');
        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)
        } else {
            this.progressLabel.string = ""
        }
    }

    showDownloadProgress() {
        let info = gData.moreGame.getTaskInfo(this.data.nebulaAppId)
        if (info != null) {
            if (info.progress >= 0) {
                // LogUtil.logV("showDownloadProgress", "three")
                this.progressLabel.string = ((info.progress / info.totalBytesReceives) * 100).toFixed(1) + "%"
            } else {
                this.setItemData(this.data)
            }
        } else {
            this.setItemData(this.data)
        }
    }
}
