import { Component, instantiate, Node, _decorator } from 'cc';
import { BaseLayer } from '../../common/BaseLayer';
const { ccclass, property } = _decorator;

@ccclass('ItemLayer')
export class ItemLayer extends Component {

    itemUI: Node;

    itemUIArr: any;
    itemArr: any;
    idItemUIObj: any = {};

    layerJS: BaseLayer;
    callback: Function;

    onLoad() {
        this.itemUI = this.node.children[0];
        this.itemUI.active = false;

        // 缓存还未被使用的itemUI
        this.itemUIArr = [this.itemUI];

        // 存储每个itemUI的数据
        this.itemArr = [];
        this.idItemUIObj = {};
    }

    hideAllItems() {
        let chs = this.node.children;
        this.itemUIArr = [];
        this.idItemUIObj = {};
        for (let i in chs) {
            let node = chs[i];
            node.active = false;
            this.itemUIArr.push(node);
        }
    }

    initUI(layerJS: BaseLayer, arr: any, callback: Function) {
        this.layerJS = layerJS;
        this.callback = callback;

        // 先全部回收隐藏
        this.hideAllItems();

        this.itemArr = [];
        this.idItemUIObj = {};
        for (let i in arr) {
            let item = arr[i];
            this.addItem(item);
        }

    }

    addItem(item: any, cb?: Function) {
        let itemUI = this.itemUIArr.shift();
        if (!itemUI) {
            itemUI = instantiate(this.itemUI);
            itemUI.parent = this.node;
        }
        itemUI.active = true;
        itemUI.item = item;
        let index = this.itemArr.length;
        itemUI.index = index;

        if (typeof (item) == "object" && item && item.id != undefined) {
            this.idItemUIObj[item.id] = itemUI;
        }

        if (cb) {
            cb(itemUI, item, index);
        } else if (this.callback) {
            this.callback(itemUI, item, index);
        }

        if (this.layerJS && this.layerJS.addButtonListener) {
            this.layerJS.addButtonListener(itemUI);
        }

        this.itemArr.push(item);

        return itemUI;
    }

    getItemUIById(id: number) {
        let itemUI = this.idItemUIObj[id];

        return itemUI;
    }

    loadAndRefreshItemUIByItem(item: any, cb?: Function) {
        if (!item || item.id == undefined) {
            return;
        }
        let itemUI = this.getItemUIById(item.id);
        if (!itemUI) {
            this.addItem(item, cb);
            return;
        }
        this.refreshItemUIByItem(item);
    }

    refreshItemUIByItem(item: any) {
        if (!item || item.id == undefined) {
            return;
        }
        let itemUI = this.getItemUIById(item.id);
        if (!itemUI) {
            return;
        }

        itemUI.item = item;
        if (this.callback) {
            this.callback(itemUI, item, itemUI.index);
        }
    }

    refreshItemUIById(id: number) {
        let itemUI = this.getItemUIById(id);
        if (!itemUI) {
            return;
        }
        this.refreshItemUIByItem(itemUI.item);
    }

    removeItemByIndex(index) {
        if (index < 0 || index >= this.itemArr.length) {
            return;
        }
        let chs = this.node.children;
        let itemUI = null;
        for (let i in chs) {
            let tmpItemUI = chs[i];
            if (!tmpItemUI.active || tmpItemUI["index"] != index) {
                continue;
            }
            itemUI = tmpItemUI;
            break;
        }
        this.removeItemUIByItemUI(itemUI);
    }

    removeItemUIById(id: number) {
        if (id == undefined) {
            return;
        }
        let itemUI = this.idItemUIObj[id];
        this.removeItemUIByItemUI(itemUI);
    }

    removeItemUIByItemUI(itemUI: Node) {
        if (!itemUI) {
            return;
        }
        itemUI.active = false;
        if (this.itemUIArr.indexOf(itemUI) == -1) {
            itemUI["index"] = -1;
            this.itemUIArr.push(itemUI);
        }

        let item = itemUI["item"];
        let index = this.itemArr.indexOf(item);
        if (index != -1) {
            this.itemArr.splice(index, 1);
        }
        if (typeof (item) == "object" && item && item.id) {
            delete this.idItemUIObj[item.id];
        }

    }

    forShowItemUI(cb: Function) {
        let chs = this.node.children;
        for (let i in chs) {
            let node = chs[i];
            if (!node.active) {
                continue;
            }
            if (cb) {
                cb(node, node["item"]);
            }
        }
    }

    refreshUI() {
        if (!this.callback) {
            return;
        }
        let chs = this.node.children;
        for (let i in chs) {
            let node = chs[i];
            if (!node.active) {
                continue;
            }
            this.callback(node, node["item"], node["index"]);
        }
    }
}

