import tkinter as tk
from tkinter import ttk, messagebox

WIDTH = 600
HEIGHT = 400

def calculate():
    try:
        initial = float(entry_initial.get().replace(',', '.'))
        rate = float(entry_rate.get().replace(',', '.'))
        years_float = float(entry_years.get().replace(',', '.'))
        if years_float != int(years_float):
            raise ValueError("Количество лет должно быть целым числом")
        years = int(years_float)
        if years < 0:
            raise ValueError("Количество лет не может быть отрицательным")
        if years > 10000:
            raise ValueError("Количество лет не должно превышать 10000")
        if initial <= 0:
            raise ValueError("Начальная зарплата должна быть положительной")
    except ValueError as e:
        messagebox.showerror("Ошибка ввода", str(e))
        return

    salaries = [initial * (1 + rate / 100) ** i for i in range(years + 1)]
    final_salary = salaries[-1]
    label_result.config(text=f"Конечная зарплата: {final_salary:.2f}")

    draw_graph(salaries, years)

def draw_graph(salaries, years):
    canvas.delete("all")
    left, right, top, bottom = 60, 20, 20, 40
    plot_width = WIDTH - left - right
    plot_height = HEIGHT - top - bottom

    if years == 0:
        min_y = salaries[0] - 1
        max_y = salaries[0] + 1
    else:
        min_y = min(salaries)
        max_y = max(salaries)
        if max_y - min_y < 1e-9:
            min_y -= 1
            max_y += 1

    canvas.create_line(left, top, left, HEIGHT - bottom, fill="black", arrow=tk.LAST)
    canvas.create_line(left, HEIGHT - bottom, WIDTH - right, HEIGHT - bottom, fill="black", arrow=tk.LAST)

    canvas.create_text(WIDTH // 2, HEIGHT - 10, text="Годы", font=("Arial", 10))
    canvas.create_text(20, HEIGHT // 2, text="Зарплата", font=("Arial", 10), angle=90)

    if years > 0:
        step_x = max(1, years // 10)
        for year in range(0, years + 1, step_x):
            x = left + (year / years) * plot_width
            canvas.create_line(x, HEIGHT - bottom, x, HEIGHT - bottom + 5, fill="black")
            canvas.create_text(x, HEIGHT - bottom + 15, text=str(year), font=("Arial", 8))
        if years % step_x != 0:
            x = left + plot_width
            canvas.create_line(x, HEIGHT - bottom, x, HEIGHT - bottom + 5, fill="black")
            canvas.create_text(x, HEIGHT - bottom + 15, text=str(years), font=("Arial", 8))
    else:
        x = left
        canvas.create_line(x, HEIGHT - bottom, x, HEIGHT - bottom + 5, fill="black")
        canvas.create_text(x, HEIGHT - bottom + 15, text="0", font=("Arial", 8))

    num_y_ticks = 5
    for i in range(num_y_ticks + 1):
        value = min_y + (max_y - min_y) * i / num_y_ticks
        y = HEIGHT - bottom - i * plot_height / num_y_ticks
        canvas.create_line(left - 5, y, left, y, fill="black")
        text = f"{value:g}"
        canvas.create_text(left - 10, y, text=text, anchor="e", font=("Arial", 8))

    points = []
    for i, salary in enumerate(salaries):
        if years == 0:
            x = left + plot_width / 2
        else:
            x = left + (i / years) * plot_width
        y = HEIGHT - bottom - ((salary - min_y) / (max_y - min_y)) * plot_height
        points.append((x, y))
        if len(salaries) <= 30:
            canvas.create_oval(x - 3, y - 3, x + 3, y + 3, fill="blue", outline="blue")
    if len(points) >= 2:
        canvas.create_line(*[coord for point in points for coord in point], fill="blue", width=2)

root = tk.Tk()
root.title("Расчет зарплаты с индексацией")

frame_input = ttk.Frame(root, padding="10")
frame_input.pack(fill=tk.X)

ttk.Label(frame_input, text="Начальная зарплата:").grid(row=0, column=0, sticky=tk.W, pady=5)
entry_initial = ttk.Entry(frame_input, width=20)
entry_initial.grid(row=0, column=1, pady=5)

ttk.Label(frame_input, text="Процент индексации в год:").grid(row=1, column=0, sticky=tk.W, pady=5)
entry_rate = ttk.Entry(frame_input, width=20)
entry_rate.grid(row=1, column=1, pady=5)

ttk.Label(frame_input, text="Количество лет:").grid(row=2, column=0, sticky=tk.W, pady=5)
entry_years = ttk.Entry(frame_input, width=20)
entry_years.grid(row=2, column=1, pady=5)

btn_calculate = ttk.Button(frame_input, text="Рассчитать", command=calculate)
btn_calculate.grid(row=3, column=0, columnspan=2, pady=10)

label_result = ttk.Label(root, text="Конечная зарплата: ")
label_result.pack(pady=5)

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
canvas.pack(padx=10, pady=5)

root.mainloop()