#!/usr/bin/env python3
"""
Zadanie 6: Konwerter jednostek temperatury
Program konwertuje temperature pomiedzy Celsiusz, Fahrenheit i Kelvin.
"""

import sys


def celsiusz_na_fahrenheit(c):
    """Konwertuje Celsiusz na Fahrenheit."""
    return c * 9/5 + 32


def celsiusz_na_kelvin(c):
    """Konwertuje Celsiusz na Kelvin."""
    return c + 273.15


def fahrenheit_na_celsiusz(f):
    """Konwertuje Fahrenheit na Celsiusz."""
    return (f - 32) * 5/9


def kelvin_na_celsiusz(k):
    """Konwertuje Kelvin na Celsiusz."""
    return k - 273.15


def przetworz_temperature(dane_wejscia):
    """Przetwarza dane wejsciowe i zwraca wyniki."""
    try:
        dane = dane_wejscia.strip().split()
        if len(dane) != 2:
            return None
        
        wartosc = float(dane[0])
        jednostka = dane[1].upper()
        
        if jednostka not in ['C', 'F', 'K']:
            return None
        
        return (wartosc, jednostka)
    except (ValueError, IndexError):
        return None


def waliduj_temperature(c, f, k):
    """Wal iduje czy temperature sa mozliwe fizycznie."""
    if c < -273.15:
        return False, "Temperatura ponizej zera bezwzglednego!"
    if f < -459.67:
        return False, "Temperatura ponizej zera bezwzglednego!"
    if k < 0:
        return False, "Temperatura ponizej zera bezwzglednego!"
    return True, ""


def main():
    """Glowna funkcja programu."""
    print("=" * 40)
    print("  KONWERTER JEDNOSTEK TEMPERATURY")
    print("=" * 40)
    print("\nFormat: <wartosc> <jednostka>")
    print("Jednostki: C (Celsiusz), F (Fahrenheit), K (Kelvin)")
    print("Przyklad: 100 C")
    
    dane = input("\nPodaj temperature: ").strip()
    
    wynik = przetworz_temperature(dane)
    
    if wynik is None:
        print("Blad: Nieprawidlowy format. Uzyj: wartosc jednostka")
        print("Przyklady: 100 C, 212 F, 350 K")
        sys.exit(1)
    
    wartosc, jednostka = wynik
    
    if jednostka == 'C':
        c = wartosc
        f = celsiusz_na_fahrenheit(c)
        k = celsiusz_na_kelvin(c)
    elif jednostka == 'F':
        f = wartosc
        c = fahrenheit_na_celsiusz(f)
        k = celsiusz_na_kelvin(c)
    else:
        k = wartosc
        c = kelvin_na_celsiusz(k)
        f = celsiusz_na_fahrenheit(c)
    
    poprawny, komunikat = waliduj_temperature(c, f, k)
    
    if not poprawny:
        print(f"Ostrzezenie: {komunikat}")
    
    print("\n" + "-" * 40)
    print("  WYNIKI KONWERSJI")
    print("-" * 40)
    print(f"Celsiusz:     {c:.2f} C")
    print(f"Fahrenheit:  {f:.2f} F")
    print(f"Kelvin:       {k:.2f} K")


if __name__ == "__main__":
    main()