import tkinter as tk
from tkinter import ttk, messagebox


def convert():
    try:
        value = float(entry_value.get())
        category = category_combo.get()
        from_unit = from_unit_combo.get()
        to_unit = to_unit_combo.get()
        
        if category == "Length":
            result = convert_length(value, from_unit, to_unit)
        elif category == "Temperature":
            result = convert_temperature(value, from_unit, to_unit)
        else:
            messagebox.showerror("Error", "Please select a valid category!")
            return
        result_label.config(text=f"{value} {from_unit} = {result:.2f} {to_unit}")
        
    except ValueError:
        messagebox.showerror("Invalid Input", "Please enter a numeric value!")

def convert_length(value,from_unit, to_unit):
    units = {
        "m": 1.0,
        "km": 1000.0,
        "cm": 0.01,
        "inch": 0.0254,
        "ft": 0.3048,
        "mile": 1609.34
    }
    value_in_meters = value * units[from_unit]
    return value_in_meters / units[to_unit]

def convert_temperature(value, from_unit, to_unit):
    if from_unit == "C":
        if to_unit == "F":
            return (value * 9/5) + 32
        elif to_unit == "k":
            return value + 273.15
    elif from_unit == "F":
        if to_unit == "C":
            return (value - 32) * 5/9
        elif to_unit == "K":
            return (value - 32) * 5/9 + 273.15 
    elif from_unit == "K":
        if to_unit == "C":
            return value - 273.15 
        elif to_unit == "F":
            return (value - 273.15) * 9/5 + 32
    return value


def update_units(event):
    category = category_combo.get()
    if category == "Length":
        units = ["m", "km", "cm", "inch", "ft", "mile"]
    elif category == "Temperature":
        units = ["C", "F", "K"] 
    else:
        units = []
        
    from_unit_combo["values"] = units
    to_unit_combo["values"] = units
    from_unit_combo.current(0)
    to_unit_combo.current(1)

# GUI Setup

root = tk.Tk()
root.title("Unit Converter")
root.geometry("400x250")

# Category selection
ttk.Label(root, text="Select Category:").pack(pady=5)
category_combo = ttk.Combobox(root, values=["Length", "Temperature"], state="readonly")
category_combo.pack()

category_combo.bind("<<ComboboxSelected>>", update_units)

# From/To unit selection
frame_units = ttk.Frame(root)
frame_units.pack(pady=10)

ttk.Label(frame_units, text="From:").grid(row=0, column=0, padx=5)
from_unit_combo = ttk.Combobox(frame_units, state="readonly")
from_unit_combo.grid(row=0, column=1, padx=5)

ttk.Label(frame_units, text="To:").grid(row=0, column=2, padx=5)
to_unit_combo = ttk.Combobox(frame_units, state="readonly")
to_unit_combo.grid(row=0, column=3, padx=5)

# Input field
ttk.Label(root, text="Enter Value:").pack(pady=5)
entry_value = ttk.Entry(root)
entry_value.pack()

convert_btn = ttk.Button(root, text="Convert", command=convert)
convert_btn.pack(pady=10)

result_label = ttk.Label(root, text="Result will appear here", font=("Arial", 12, "bold"))
result_label.pack(pady=10)



root.mainloop()