import tkinter as tk

# Constants for the abacus layout.
NUM_COLUMNS = 6             # number of rods
BEAD_RADIUS = 15
ROD_SPACING = 75
HEAVEN_Y = 50
EARTH_START_Y = 150
EARTH_SPACING = 40
HEAVEN_COUNT = 1
EARTH_COUNT = 4

class Bead:
    def __init__(self, canvas, col, row, is_heaven):
        self.canvas = canvas
        self.col = col          # which rod this bead belongs to
        self.row = row          # index within its group
        self.is_heaven = is_heaven

        x = (col + 1) * ROD_SPACING
        if is_heaven:
            y = HEAVEN_Y + row * 2 * BEAD_RADIUS
        else:
            y = EARTH_START_Y + row * 2 * BEAD_RADIUS

        self.x = x
        self.y = y
        self.id = canvas.create_oval(
            x - BEAD_RADIUS, y - BEAD_RADIUS,
            x + BEAD_RADIUS, y + BEAD_RADIUS,
            fill="tan", outline="black"
        )
        canvas.tag_bind(self.id, "<Button-1>", self.toggle)

    def toggle(self, event):
        """
        Slide a bead up or down depending on which group it belongs to.
        """
        if self.is_heaven:
            # Heaven bead can only move DOWN (towards the bar) or back UP.
            target = HEAVEN_Y + BEAD_RADIUS*2
            if self.y < target:
                dy = (target - self.y)
            else:
                dy = - (target - HEAVEN_Y)
        else:
            # Earth beads move upward toward the bar or back down to their start.
            target = EARTH_START_Y - BEAD_RADIUS*2
            if self.y > target:
                dy = -(self.y - target)
            else:
                dy = (EARTH_START_Y - self.y)

        # Move only this bead
        self.canvas.move(self.id, 0, dy)
        self.y += dy
        update_display()

def create_rods(canvas):
    for c in range(NUM_COLUMNS):
        x = (c + 1) * ROD_SPACING
        canvas.create_line(x, HEAVEN_Y - 50, x, EARTH_START_Y + 200, width=4)

def get_value():
    value = 0
    for col in range(NUM_COLUMNS):
        # Check heaven bead
        heaven_bead = beads[(col, 0, True)]
        if heaven_bead.y > HEAVEN_Y + BEAD_RADIUS:
            value += 5 * (10**(NUM_COLUMNS - 1 - col))

        # Count earth beads that have moved up
        earth_count = 0
        for i in range(EARTH_COUNT):
            b = beads[(col, i, False)]
            if b.y < EARTH_START_Y + i*2*BEAD_RADIUS:
                earth_count += 1
        value += earth_count * (10**(NUM_COLUMNS - 1 - col))
    return value

def update_display():
    val = get_value()
    label_var.set(f"Value: {val}")

root = tk.Tk()
root.title("Abacus")

canvas = tk.Canvas(root, width=(NUM_COLUMNS+1)*ROD_SPACING,
                   height=350, bg="white")
canvas.pack()

# Draw the separating bar
canvas.create_line(0, EARTH_START_Y - 30,
                   (NUM_COLUMNS+1)*ROD_SPACING,
                   EARTH_START_Y - 30, width=6)

create_rods(canvas)

# Create beads and store them in a dictionary
beads = {}
for col in range(NUM_COLUMNS):
    # heaven beads
    for r in range(HEAVEN_COUNT):
        bead = Bead(canvas, col, r, True)
        beads[(col, r, True)] = bead
    # earth beads
    for r in range(EARTH_COUNT):
        bead = Bead(canvas, col, r, False)
        beads[(col, r, False)] = bead

# Value display
label_var = tk.StringVar()
label_var.set("Value: 0")
label = tk.Label(root, textvariable=label_var, font=("Helvetica", 14))
label.pack(pady=5)

root.mainloop()
