import barConfig from './barConfig.js';

export const getTemp = (temp) => {
    if (barConfig?.isAmerican) {
        return Math.round((temp * 9 / 5) + 32) + "°F";
    }
    return temp + "°C";
};

export const getWeatherSymbol = (weatherCode) => {
    const dt = new Date();
    const hour = dt.getHours();
    if (hour <= 7 || hour >= 20) {
        return NIGHT_WEATHER_SYMBOL[OMAPI_CODE[weatherCode]];
    }
    return WEATHER_SYMBOL[OMAPI_CODE[weatherCode]];
};

export const getWeatherDesc = (weatherCode) => {
    return OMAPI_CODE_DESC[weatherCode];
};

export const getFormattedTime = () => {
    const date = new Date();
    let hours = date.getHours();
    let minutes = date.getMinutes();
    if (hours < 10) hours = "0" + hours;
    if (minutes < 10) minutes = "0" + minutes;
    return `${hours}:${minutes}`
}

export const getFormattedDate = () => {
    const date = new Date();
    const day = date.getDay();
    const month = date.getMonth();
    let dayOfMonth = date.getDate();
    switch (dayOfMonth) {
        case 1:
        case 21:
        case 31:
            dayOfMonth += "st";
            break;
        case 2:
        case 22:
            dayOfMonth += "nd";
            break;
        case 3:
        case 23:
            dayOfMonth += "rd";
            break;
        default:
            dayOfMonth += "th";
    }

    return `${days[day]}, ${dayOfMonth} ${months[month]}`;
}

export const getBattery = (batt) => {
    if (batt?.charging) {
        return "battery_charging_full";
    }
    if (batt?.charged) {
        return "battery_full";
    }
    
    return battIcons[Math.floor(battIcons.length * (batt?.percent / 100))];
}

const battIcons = ["battery_0_bar","battery_1_bar","battery_2_bar","battery_3_bar","battery_4_bar","battery_5_bar", "battery_6_bar"];

const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

const OMAPI_CODE_DESC = {
    "0":	"Clear",
    "1":	"Mostly Clear",
    "2":	"Partly Cloudy",
    "3":	"Cloudy",
    "45":	"Fog",
    "48":	"Freezing Fog",
    "51":	"Light Drizzle",
    "53":	"Drizzle",
    "55":	"Heavy Drizzle",
    "56":	"Light Freezing Drizzle",
    "57":	"Freezing Drizzle",
    "61":	"Light Rain",
    "63":	"Rain",
    "65":	"Heavy Rain",
    "66":	"Light Freezing Rain",
    "67":	"Freezing Rain",
    "71":	"Light Snow",
    "73":	"Snow",
    "75":	"Heavy Snow",
    "77":	"Light Snow Shower",
    "80":	"Light Rain Shower",
    "81":	"Rain Shower",
    "82":	"Heavy Rain Shower",
    "85":	"Snow Shower",
    "86":	"Heavy Snow Shower",
    "95":	"Thunderstorm",
    "96":	"Heavy Thunderstorm",
    "99":	"THunderstorm with Hail"
}

const OMAPI_CODE = {
    "0":	"Sunny",
    "1":	"PartlyCloudy",
    "2":	"Cloudy",
    "3":	"VeryCloudy",
    "45":	"Fog",
    "48":	"Fog",
    "51":	"LightShowers",
    "53":	"LightRain",
    "55":	"HeavyShowers",
    "56":	"LightSleetShowers",
    "57":	"LightSleet",
    "61":	"LightRain",
    "63":	"LightRain",
    "65":	"HeavyRain",
    "66":	"LightSleet",
    "67":	"LightSleet",
    "71":	"LightSnow",
    "73":	"HeavySnow",
    "75":	"HeavySnow",
    "77":	"LightSnowShowers",
    "80":	"LightShowers",
    "81":	"HeavyShowers",
    "82":	"HeavyShowers",
    "85":	"LightSnowShowers",
    "86":	"HeavySnowShowers",
    "95":	"ThunderyShowers",
    "96":   "ThunderyHeavyRain",
    "99":	"ThunderySnowShowers"
}

const WEATHER_SYMBOL = {
    "Unknown": "air",
    "Cloudy": "cloud",
    "Fog": "foggy",
    "HeavyRain": "rainy",
    "HeavyShowers": "rainy",
    "HeavySnow": "snowing",
    "HeavySnowShowers": "snowing",
    "LightRain": "rainy",
    "LightShowers": "rainy",
    "LightSleet": "rainy",
    "LightSleetShowers": "rainy",
    "LightSnow": "cloudy_snowing",
    "LightSnowShowers": "cloudy_snowing",
    "PartlyCloudy": "partly_cloudy_day",
    "Sunny": "clear_day",
    "ThunderyHeavyRain": "thunderstorm",
    "ThunderyShowers": "thunderstorm",
    "ThunderySnowShowers": "thunderstorm",
    "VeryCloudy": "cloud",
}

const NIGHT_WEATHER_SYMBOL = {
    "Unknown": "air",
    "Cloudy": "cloud",
    "Fog": "foggy",
    "HeavyRain": "rainy",
    "HeavyShowers": "rainy",
    "HeavySnow": "snowing",
    "HeavySnowShowers": "snowing",
    "LightRain": "rainy",
    "LightShowers": "rainy",
    "LightSleet": "rainy",
    "LightSleetShowers": "rainy",
    "LightSnow": "cloudy_snowing",
    "LightSnowShowers": "cloudy_snowing",
    "PartlyCloudy": "partly_cloudy_night",
    "Sunny": "clear_night",
    "ThunderyHeavyRain": "thunderstorm",
    "ThunderyShowers": "thunderstorm",
    "ThunderySnowShowers": "thunderstorm",
    "VeryCloudy": "cloud",
}
