import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import Common5 from "../../Platform/th/Common5";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HeChengNode extends cc.Component {

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

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

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

    //当前合成箱内数据列表
    private hechengDataList = [];
    //当前合成箱
    private curBox: cc.Node = null;
    //合成配置
    private heChengConfig = null;

    start() {
        this.hechengDataList = [];
        this.curBox = this.boxList.children[0];
        this.curBox.active = true;

        this.checkBox.attr({
            XianSuoBarScr: this
        });
    }

    setHeChengConfig(heChengConfig) {
        this.heChengConfig = heChengConfig;
    }

    addNode(data_) {
        Common5.playEffect('放置成功');

        this.hechengDataList.push(data_);
        if (this.hechengDataList.length >= 3) {
            this.checkBox.active = false;
        }

        this.curBox.active = false;
        this.curBox = this.boxList.children[this.hechengDataList.length];
        this.curBox.active = true;

        for (let index = 0; index < this.hechengDataList.length; index++) {
            Common5.getSpriteFrameFromBundle(this.hechengDataList[index].itemInfo.bandleName, this.hechengDataList[index].itemInfo.url2, this.curBox.children[index].getComponent(cc.Sprite));
            this.curBox.children[index].attr(this.hechengDataList[index]);
        }
    }

    removeNode(target) {
        Common5.playEffect('放置成功');

        let hechengDatas = this.hechengDataList;
        this.hechengDataList = [];
        for (let index = 0; index < hechengDatas.length; index++) {
            if (hechengDatas[index].xianSuoIndex != target.xianSuoIndex) {
                this.hechengDataList.push(hechengDatas[index]);
            }
        }

        if (this.hechengDataList.length < 3) {
            this.checkBox.active = true;
        }

        this.curBox.active = false;
        this.curBox = this.boxList.children[this.hechengDataList.length];
        this.curBox.active = true;

        for (let index = 0; index < this.hechengDataList.length; index++) {
            Common5.getSpriteFrameFromBundle(this.hechengDataList[index].itemInfo.bandleName, this.hechengDataList[index].itemInfo.url2, this.curBox.children[index].getComponent(cc.Sprite));
            this.curBox.children[index].attr(this.hechengDataList[index]);
        }

        if (target.XianSuoMoveTouchScr) {
            target.XianSuoMoveTouchScr.showTouchNodeIcon();
        }
    }

    determine() {
        for (const iterator of this.heChengConfig) {
            let zuhe: number[] = iterator["zuhe"];
            if (zuhe.length == this.hechengDataList.length) {
                let success = true;
                for (const iterator2 of this.hechengDataList) {
                    if (zuhe.indexOf(iterator2.itemInfo.id) == -1) {
                        success = false;
                        break;
                    }
                }
                if (success) {
                    this.node.active = false;
                    Common5.playEffect('放置成功');
                    EventMgr.emitEvent_custom(ryw_Event.XianSuoHeChengEvent, { touchIndex: iterator["id"] });
                    return;
                }
            }
        }
        Common5.playEffect('放置错误');
        cc.tween(this.background)
            .set({ color: cc.Color.RED })
            .delay(1)
            .set({ color: cc.Color.WHITE })
            .start();
    }

    onClick(event) {
        let target = event.target;

        if (target.name == "button") {
            this.determine();
        } else {
            this.removeNode(target);
        }
    }

    // update (dt) {}

}