// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    private list: Array<{ img, name, id }>;
    //TODO:入口方法
    public setData(val) {
        this.list = [val.left, val.right];
        this.initView();
    }
    //TODO:点击事件
    public onTouchItem(e) {
        let data = e.target.data;
        console.log(data);
        let event = new cc.Event.EventCustom('page1_btn_click', true);
        event.setUserData(data);
        this.node.dispatchEvent(event);
    }

    private layout: cc.Node;
    onLoad() {
        this.layout = this.node.getChildByName("layout");
    }
    initView() {
        let children = this.layout.children;
        this.list.forEach((data, index) => {
            let item = children[index];
            this.updateItem(item, data);
        })
    }
    updateItem(item: any, data) {
        item.active = true;
        item.data = data;
        let icon = item.getChildByName("icon")
        let bg = item.getChildByName("bg")
        let title = item.getChildByName("title")
        this.setNetImg(icon, data.img, { w: 320, h: 370 })
        title.getComponent(cc.Label).string = data.name;
        item.on(cc.Node.EventType.TOUCH_END, this.onTouchItem, this);
    }


    setNetImg(item, url, { w, h }) {
        return new Promise((resolve, reject) => {
            if (!item) return console.log("图片更换失败,传入了错误的item");
            let node = item.node ? item.node : item;
            if (!url) {
                if (node) node.active = false;
                return console.log("图片更换失败,传入了错误的res");
            }
            if (!node) return console.log("图片更换失败,传入了错误的item");
            if (node.net_url == url) return;
            node.active = false;//
            cc.loader.load({ url }, (err, texture) => {
                if (err && !texture) return;
                if (!cc.isValid(node)) return console.log("节点已销毁");
                let nw = node.width = texture.width;
                let nh = node.height = texture.height;
                let component = node.getComponent(cc.Sprite);
                let spriteFrame = new cc.SpriteFrame(texture);
                component.spriteFrame = spriteFrame;
                node.net_url = url;
                if (!cc.isValid(node)) return console.log("节点已销毁");
                if (!node) return console.log("节点已销毁");
                if (w && h) {
                    let scaleX = w / nw;
                    let scaleY = h / nh;
                    let scale = scaleX <= scaleY ? scaleX : scaleY;
                    node.width = nw * scale;
                    node.height = nh * scale;
                }
                node.active = true;
                resolve({ w: nw, h: nh });
            });
        })
    }
}
