import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import MagicBoxInfoView from "./MagicBoxInfoView";
import MagicBoxItem from "./MagicBoxItem";

/**
 * 魔盒
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class MagicBoxView extends ViewObject {

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

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

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

    start(){
       this.init(1)
    }

    public init(type){
        let magicBoxs = this.main.sManage.magicBoxs

        this.mLine1.destroyAllChildren()
        this.mLine2.destroyAllChildren()
        let count = 0
        for (let i = 0; i < magicBoxs.length; i++) {
            const element = magicBoxs[i];
            if(element.type == type){
                let node = cc.instantiate(this.mBoxItem)
                let boxItem = node.getComponent(MagicBoxItem)
                boxItem.init(this.main,element)
                if(count < 2){
                    node.parent = this.mLine1
                }else{
                    node.parent = this.mLine2
                }
                boxItem.setCallback((item)=>{
                    this.openMagicBox(item)
                })
                count ++
            }
        }
    }

    public openMagicBox(boxItem:MagicBoxItem){
        this.main.viewManage.loadFunc(GameViewType.moheInfo,(viewObject:ViewObject)=>{
            let magicBoxInfo = viewObject as MagicBoxInfoView;
            magicBoxInfo.init(boxItem)
            viewObject.show();
        });
    }



}
