import GoodItem from "../../common/GoodItem";
import GoodAbout from "../../common/GoodAbout";
import ViewObject from "../../../main/ViewObject";
import { GameViewType } from "../../../main/ViewManage";
import FoodView from "./FoodView";
import { ReveData } from "../../../util/CHttp";

/**
 * 背包
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class Pack extends ViewObject {
    // /**
    //  * 背包静态缓存
    //  */
    // public static instance: Pack = null;
    /**
     * [0]:装备
     * [1]:道具
     * [2]:宝箱
     */
    @property([cc.Node])
    mContent: Array<cc.Node> = [];

    @property(cc.Prefab)
    mGoodItem: cc.Prefab = null;

    curInset: number = 0;

    public httpEvent(reveData: ReveData) {
        let data = reveData.data;
        if (data._equip != undefined) {
            this.flushEquip();
        }
        if (data._pack != undefined) {
            this.flushGood();
        }
    }

    public onLoad() {
        this.mContent[1].active = false;
        this.flush();
    }
    public flush() {
        this.flushGood();
        this.flushEquip();
    }

    /**
     * 刷新道具
     */
    public flushGood() {
        let pack = this.main.player.pack;
        let index = 0;
        let content = this.mContent[0];
        if (content.children.length) {
            content.removeAllChildren();
        }
        for (let id in pack) {
            let count = pack[id];
            if (count > 0) {
                let data = {
                    id: id,
                    count: count,
                };
                this.addGoodItem(content);
                let node = content.children[index];
                let goodItem = node.getComponent(GoodItem);
                goodItem.initGood(this.main, data);

                if (goodItem.good.type == 5 || goodItem.good.type == 3) {
                    goodItem.setCallback((gi) => {
                        this.openEatFood(gi);
                    });
                } else {
                    goodItem.setCallback((gi) => {
                        this.openGoodAbout(gi);
                    });
                }
                index++
            }
        }
    }

    /**
     * 刷新装备
     */
    public flushEquip() {
        // let equip = this.sortEquip();
        let equip = this.main.player.equip;
        let index = 0;
        let content = this.mContent[1];
        if (content.children.length) {
            content.removeAllChildren();
        }
        for (let i = 0; i < equip.length; i++) {
            const element = equip[i];
            this.addGoodItem(content);
            let node = content.children[index];
            let equipItem = node.getComponent(GoodItem);
            equipItem.initEquip(this.main, element);
            equipItem.node.zIndex = this.main.player.equip.length - equipItem.equip.id;
            equipItem.setCallback((gi) => {
                this.openGoodAbout(gi);
            });
            index++;
        }
    }

    sortEquip() {
        let equip = this.main.player.equip;
        equip.sort((a, b) => {
            return b.id - a.id
        })
        return equip
    }

    private addGoodItem(content: cc.Node) {
        let node = cc.instantiate(this.mGoodItem);
        node.parent = content;
    }

    private openGoodAbout(goodItem: GoodItem) {
        this.main.viewManage.loadFunc(GameViewType.goodAbout, (viewObject: ViewObject) => {
            let goodAbout = viewObject as GoodAbout;
            goodAbout.init(goodItem, false)
            viewObject.show();
        });
    }

    private openEatFood(goodItem: GoodItem) {
        this.main.viewManage.loadFunc(GameViewType.goodEat, (viewObject: ViewObject) => {
            let goodAbout = viewObject as FoodView;
            goodAbout.initGood(this, goodItem)
            viewObject.show();
        });
    }

    onClickInset(eventTouch: cc.Event.EventTouch, data: string) {
        this.curInset = Number(data);
        this.mContent.forEach((content, index) => {
            content.active = index == Number(data);
        })
    }
}
