import Area, { Zone, History_Role } from "./Area";
import AllArea from "./AllArea";
import MyZoneItem from "./item/MyZoneItem";
import ViewObject from "../main/ViewObject";
import { GameViewType } from "../main/ViewManage";

/**
 * 历史分区
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class HistoryArea extends ViewObject {

    public area:Area;

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

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

    onLoad(){
        let zones = this.area.zones;
        for (let i = 0; i < zones.length; i++) {
            const element = zones[i];
            if(element.rec && !this.inRoles(element)){
                this.addItem(element,null);
            }
        }
        if(this.area.roles){
            for (let i = 0; i < this.area.roles.length; i++) {
                const element = this.area.roles[i];
                this.addItem(null,element);
            }
        }
    }
    public addItem(zone:Zone,role:History_Role){
        let node = cc.instantiate(this.mZoneItem);
        let item = node.getComponent(MyZoneItem);
        item.zone = zone;
        item.role = role;

        node.parent = this.mContent;

        item.setCallback((m:MyZoneItem)=>{
            this.area.setOptZone(m.zone);
            this.exitDistroy();
        });
        
    }
    public inRoles(zone:Zone):boolean{
        if(this.area.roles == null){
            return false;
        }
        for (let i = 0; i < this.area.roles.length; i++) {
            const element = this.area.roles[i];
            if(element.zoneId == zone.id){
                return true;
            }
        }
        return false;
    }

    /**
     * 打开所有分区
     */
    public openAllArea(){
        this.main.startLoad();
        this.main.viewManage.loadFunc(GameViewType.allArea,(viewObject:ViewObject)=>{
            let allArea = viewObject as AllArea;
            allArea.area = this.area;
            viewObject.show(this);
            this.main.stopLoad();
        });
    }
}
