export default class Room {
    constructor() {
        this.id = "room";
        this.title = "A room";
        this.description = "You see nothing special";
        this.firstDescription = "";
        this.objects = [];
        this.exits = new Map();
        this.enterCallback = null;
        this.exitCallback = null;
        this.canEnterLogic = null;
        this.canExitLogic = null;
        this.tickCallback = null;
        this.context = null;
        this.music = null;
        this.ambience = null;
        this.impulse = null;
    }

    async onEnter() {
        this.context.output.setMusic(this.music);
        this.context.output.setAmbience(this.ambience);
        this.context.output.setImpulse(this.impulse);
        if (this.enterCallback) return this.enterCallback(this.context);
    }

    async onExit() {
        if (this.exitCallback) return this.exitCallback(this.context);
    }

    canEnter() {
        if (this.canEnterLogic) {
            return this.canEnterLogic(this.context);
        }
        return true;
    }

    canExit() {
        if (this.canExitLogic) {
            return this.canExitLogic(this.context);
        }
        return true;
    }

    addExit(direction, roomID) {
        this.exits.set(direction, roomID);
        return this;
    }

    getExit(direction) {
        return this.exits.get(direction);
    }

    addItem(item) {
        this.objects.push(item);
    }

    removeItem(id) {
        this.objects = this.objects.filter((item) => item != id);
    }

    addEnterCallback(callback) {
        this.enterCallback = callback.bind(this);
    }

    addExitCallback(callback) {
        this.exitCallback = callback.bind(this);
    }

    addEnterLogic(func) {
        this.canEnterLogic = func.bind(this);
    }

    addExitLogic(func) {
        this.canExitLogic = func.bind(this);
    }

    addTickCallback(callback) {
        this.tickCallback = callback.bind(this);
    }

    getItems() {
        return this.objects.map((item) => this.context.getItem(item));
    }

    async onTick() {
        if (this.tickCallback) return this.tickCallback(this.context);
    }
}