/*
 * Copyright (C) 2026  Tim Zimmermann <tim@aglais.org>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.i18n;

import org.junit.jupiter.api.Test;

import java.util.Locale;
import java.util.ResourceBundle;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Testcases for {@link InterpolatingResourceBundle}
 */
public class InterpolatingResourceBundleTest {

    private static final String TEST_BUNDLE = "test";

    private InterpolatingResourceBundle getTestBundle() {
        return (InterpolatingResourceBundle) ResourceBundle.getBundle(TEST_BUNDLE);
    }

    @Test
    public void testGetBundle() {
        ResourceBundle bundle = ResourceBundle.getBundle(TEST_BUNDLE);

        assertInstanceOf(InterpolatingResourceBundle.class, bundle);
    }

    @Test
    public void testPositionalPlaceholder() {
        String key = "test_positional_placeholder";
        InterpolatingResourceBundle bundle = getTestBundle();

        assertEquals("Test ${0}", bundle.getString(key));
        assertEquals("Test 1", bundle.getString(key, 1));
        assertEquals("Test " + key, bundle.getString(key, key));
        assertEquals("Test null", bundle.getString(key, (Object) null));
    }

    @Test
    public void testKeyPlaceholder() {
        String key = "test_key_placeholder";
        InterpolatingResourceBundle bundle = getTestBundle();

        assertEquals("Test 1", bundle.getString(key + "1"));
        assertEquals("Test ${nonexistent}", bundle.getString(key + "2"));
        assertEquals("Test 12345", bundle.getString(key + "3", 12345));
    }

    @Test
    public void testFallbackBundle() {
        String key = "test_fallback";
        Locale locale = Locale.getDefault();
        Locale.setDefault(Locale.US);
        InterpolatingResourceBundle bundle = getTestBundle();
        Locale.setDefault(Locale.GERMANY);
        InterpolatingResourceBundle germanBundle = getTestBundle();
        Locale.setDefault(locale);

        assertNotEquals(germanBundle.getLocale(), bundle.getLocale());
        assertEquals(bundle.getString(key + "_untranslated"), germanBundle.getString(key + "_untranslated"));
        assertNotEquals(bundle.getString(key + "_translated"), germanBundle.getString(key + "_translated"));
    }
}
