/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.messages;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class SteamwarResourceBundle extends PropertyResourceBundle {

    private static final String BASE_PATH = "/" + "de.steamwar.messages.BungeeCore".replace('.', '/');

    private static final Map<String, ResourceBundle> bundles = new HashMap<>();

    public static ResourceBundle getResourceBundle(Locale locale) {
        return getResourceBundle(locale.toString(), getResourceBundle(locale.getLanguage(), getResourceBundle( "", null)));
    }

    private static synchronized ResourceBundle getResourceBundle(String locale, ResourceBundle parent) {
        return bundles.computeIfAbsent(locale, locale1 -> {
            InputStream inputStream = Message.class.getResourceAsStream(BASE_PATH + ("".equals(locale) ? "" : "_" + locale) + ".properties");
            if(inputStream == null)
                return parent;
            try {
                return new SteamwarResourceBundle(inputStream, parent);
            } catch (IOException e) {
                return parent;
            }
        });
    }

    private SteamwarResourceBundle(InputStream stream, ResourceBundle parent) throws IOException {
        super(stream);
        setParent(parent);
    }
}
