: AyaanScript+ — Live Analog Clock with hour, minute, second hands.
: Reads system time, animates hands continuously.

const W = 600
const H = 600
const CX = 300
const CY = 300
gui_native "AyaanScript+ — Analog Clock" W H
gui_fps 30

set running = 1
set R_HOUR = 110
set R_MIN = 170
set R_SEC = 200
set PI = 3.14159265358979

while running == 1
    gui_open running
    if running == 0
        break
    endif

    : background
    gui_gradient_rect 0 0 W H "#1a1a2e" "#0f0c29"

    : clock face
    gui_fill_circle CX CY 240 "#0a0a14"
    gui_circle CX CY 240 "white"
    gui_circle CX CY 235 "lightgray"

    : hour ticks (12 marks)
    set tick = 0
    repeat 12 tick
        set ang = {tick * PI / 6}
        set ix = {CX + sin(ang) * 220}
        set iy = {CY - cos(ang) * 220}
        set ox = {CX + sin(ang) * 235}
        set oy = {CY - cos(ang) * 235}
        gui_thick_line ix iy ox oy 3 "white"
    endrepeat

    : minute ticks (60 marks, skip multiples of 5)
    set tk = 0
    repeat 60 tk
        set mod5 = {tk - 5 * int(tk / 5)}
        if mod5 != 0
            set ang = {tk * PI / 30}
            set ix = {CX + sin(ang) * 228}
            set iy = {CY - cos(ang) * 228}
            set ox = {CX + sin(ang) * 235}
            set oy = {CY - cos(ang) * 235}
            gui_line ix iy ox oy "gray"
        endif
    endrepeat

    : get current time pieces
    now_iso ts
    : ts is like "2026-06-21 14:33:21"
    : extract HH MM SS via substring at positions 11,14,17
    substr hour_s = ts 11 2
    substr min_s = ts 14 2
    substr sec_s = ts 17 2
    set h = {int(hour_s)}
    set m = {int(min_s)}
    set s = {int(sec_s)}

    : hour hand (12-hour, smooth)
    set h12 = {h - 12 * int(h / 12)}
    set ha = {(h12 + m / 60) * PI / 6}
    set hx = {CX + sin(ha) * R_HOUR}
    set hy = {CY - cos(ha) * R_HOUR}
    gui_thick_line CX CY hx hy 8 "white"

    : minute hand
    set ma = {(m + s / 60) * PI / 30}
    set mx = {CX + sin(ma) * R_MIN}
    set my = {CY - cos(ma) * R_MIN}
    gui_thick_line CX CY mx my 5 "lightgray"

    : second hand
    set sa = {s * PI / 30}
    set sxh = {CX + sin(sa) * R_SEC}
    set syh = {CY - cos(sa) * R_SEC}
    gui_thick_line CX CY sxh syh 2 "red"

    : center cap
    gui_fill_circle CX CY 10 "red"
    gui_fill_circle CX CY 5 "white"

    : digital readout
    substr tdisp = ts 11 8
    gui_text_big 200 500 tdisp "cyan" 3
    gui_text 230 550 "AYAANSCRIPT+ CLOCK" "lightgray"

    gui_key_down "esc" -> esc
    if esc == 1
        set running = 0
    endif

    gui_update
endwhile

gui_close
