package com.lemenzo.clock.worldclock;

import android.icu.util.TimeZone;
import java.util.Locale;

public final class WorldClockFormatter {
    private WorldClockFormatter() {}

    public static String city(String zoneId) {
        if (zoneId == null || zoneId.isEmpty()) return "";
        int slash = zoneId.lastIndexOf('/');
        String raw = slash >= 0 ? zoneId.substring(slash + 1) : zoneId;
        return raw.replace('_', ' ');
    }

    public static String country(String zoneId, Locale locale) {
        try {
            String region = TimeZone.getRegion(zoneId);
            if (region == null || region.isEmpty() || "001".equals(region)) return "";
            Locale countryLocale = new Locale("", region);
            String display = countryLocale.getDisplayCountry(locale == null ? Locale.getDefault() : locale);
            return display == null ? "" : display;
        } catch (RuntimeException ignored) {
            return "";
        }
    }

    public static String searchText(String zoneId, Locale locale) {
        String country = country(zoneId, locale);
        return (city(zoneId) + " " + country + " " + zoneId).toLowerCase(Locale.ROOT);
    }
}
