import { GameTools } from "./GameTools";
import { ZSSdk } from "../Net/ZSSdk";
import { SDKZS } from "../Net/SDKZS";


// import GamePools from "../script/GamePools";
// import { Tools } from "../script/Tools";
// import { FYHD_Sdk } from "../Net/FYHD_Sdk";

export default class ListItem extends Laya.Script {
   
    constructor() { super(); }
    private data: any;

    private self: Laya.Box;
    private btn: Laya.Button;
    private icon: Laya.Image;
    private name: Laya.Text;
    private bottom:Laya.Image;
    private remen:Laya.Image;

    private type: string;
    private _tip: Laya.Text;
    private diandian: Laya.Image;
    onAwake() {
        this.self = this.owner as Laya.Box;
        this.icon = this.self.getChildByName("icon") as Laya.Image;
        this.btn = this.self.getChildByName("btn") as Laya.Button;
        this.name = this.self.getChildByName("name") as Laya.Text;
        this._tip = this.self.getChildByName("tip") as Laya.Text;
        this.bottom = this.self.getChildByName("bottom") as Laya.Image;
        this.remen = this.self.getChildByName("remen") as Laya.Image; 

        this.diandian = this.icon.getChildAt(0) as Laya.Image;
        this.btn.clickHandler =new Laya.Handler(this, this.clickAd);
    }

    Refresh(type?) {
        this.data = this.self.dataSource;
        this.type = type;
        if (this.name) {
            this.name.text = this.data.app_title;
        }
        if (this._tip) {
            this._tip.visible = true;
            let rnd = GameTools.RandomANumber(1,200000);
            let text = rnd.toString() + "人玩过";
            if (rnd > 9999) {
                text = Math.floor(rnd / 10000) + "W人玩过";
            }
            this._tip.text = text;
        }
        if (this.diandian) {
            this.diandian.visible = false;
        }
        if(this.bottom){
            let num = GameTools.RandomANumber(1,10);
            this.bottom.skin = "Game/res/bottom/"+ num +".png";
        }
        if(this.remen){
            this.remen.visible = Math.random()<0.2?true:false;
        }
        this.icon.skin = this.data.app_icon;
    }

    clickAd() {
        SDKZS.navigate2Mini(this.data);
    }
    onDestroy() {
        this.btn.clickHandler = null;
    }
}