/*
 * Copyright (c)  2017  Francisco José Montiel Navarro.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.franmontiel.localechanger.utils;

import android.os.Build;
import android.os.LocaleList;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * A class useful to retrieve the system configured Locales.
 */
public class SystemLocaleRetriever {

    private SystemLocaleRetriever() {
    }

    public static List<Locale> retrieve() {
        List<Locale> systemLocales;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            systemLocales = mapToListOfLocales(LocaleList.getDefault());
        } else {
            systemLocales = Collections.singletonList(Locale.getDefault());
        }
        return systemLocales;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private static List<Locale> mapToListOfLocales(LocaleList localeList) {
        List<Locale> locales = new ArrayList<>();
        for (int i = 0; i < localeList.size(); i++) {
            locales.add(localeList.get(i));
        }
        return locales;
    }
}