import Tab from "./Tab";

export default class Window {
    private _id : number;
    private _tabs : Array<Tab>;

    constructor(id : number) {
        if (id === null || id === undefined || isNaN(id)) {
            throw new Error('cannot create ManagedWindow without id')
        }
        this._id = id;
        this._tabs = [];
    }

    get id() : number {
        return this._id;
    }

    hasTab(tabId : number) : boolean {
        return this._tabs.some(tab => tab.id === tabId);
    }

    connectTab(tabId: number) {
        let tab = this._tabs.find(tab => tab.id === tabId);
        if (tab) {
            tab.connect();
        }
    }

    getDisconnectedTabIds() : Array<number> {
        return this._tabs.filter(tab => !tab.isConnected).map(tab => tab.id);
    }

    getConnectedTabIds() : Array<number> {
        return this._tabs.filter(tab => tab.isConnected).map(tab => tab.id);
    }

    addTab(tabId : number) {
        if (! this._tabs.find(tab => tab.id === tabId)) {
            //console.log(`add new tab: ${tabId}`);
            let tab = new Tab(tabId);
            this._tabs.push(tab);
        }
    }

    removeTab(tabId : number) {
        let findIndex = this._tabs.findIndex(tab => tab.id === tabId)
        if (findIndex !== -1 ) {
            //console.log(`remove old: ${tabId}`);
            this._tabs.splice(findIndex,1);
        }
    }

    activateTab(tabId: number) {
        this._tabs.forEach(tab => {
            if (tab.id === tabId) {
                //console.log(`activate tab: ${tab.id}`);
                tab.activate();
            } else {
                //console.log(`deactivate tab: ${tab.id}`);
                tab.deActivate();
            }
        })
    }

}