/*
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import {Layout} from './layout';
import {Theme} from './theme';

export class Globals {
    private static layouts: Layout[] = [];
    private static themes: Theme[] = [];

    public static registerTheme(name: string, styleClass: string): void {
        if (Globals.themes.some((value): boolean => value.name === name)) {
            console.warn(`Theme with name '${name}' already registered; ignoring register request for styleClass '${styleClass}'.`);
        } else {
            Globals.themes.push({
                name,
                styleClass
            });
        }
    }

    public static hasThemes(): boolean {
        return Globals.themes.length > 0;
    }

    public static hasMultipleThemes(): boolean {
        return Globals.themes.length > 1;
    }

    public static getFirstTheme(): Theme {
        return Globals.getTheme(0);
    }

    public static getTheme(index: number): Theme {
        return Globals.themes[index];
    }

    public static getThemes(): Theme[] {
        return Globals.themes;
    }

    public static registerLayout(layout: Layout): void {
        const available = !Globals.layouts.some((element): boolean => {
            if (layout.code === element.code) {
                if (layout.prettyName !== element.prettyName) {
                    console.error(
                        `Error: Language code '${layout.code}' cannot be registered for layout '${layout.prettyName}' because it is already registered for layout '${element.prettyName}'!`
                    );
                }
                return true;
            }
            return false;
        });
        if (available) {
            Globals.layouts.push(layout);
            Globals.layouts.sort((a, b): number => a.prettyName.localeCompare(b.prettyName));
        }
    }

    public static hasLayouts(): boolean {
        return Globals.layouts.length > 0;
    }

    public static hasMultipleLayouts(): boolean {
        return Globals.layouts.length > 1;
    }

    public static getFirstLayout(): Layout {
        return Globals.layouts[0];
    }

    public static getLayouts(): Layout[] {
        return Globals.layouts;
    }
}