# Read lever power
scoreboard players set #power_cur security 0
execute if block -66 139 52 minecraft:lever[powered=true] run scoreboard players set #power_cur security 1

# PC hum loop cooldown
scoreboard players remove #pc_cd security 1
execute if score #pc_cd security matches ..-1 run scoreboard players set #pc_cd security 0

# Computer OFF -> stop PC sound and reset cooldown
execute if score #power_cur security matches 0 run stopsound @a record mymap:pc
execute if score #power_cur security matches 0 run scoreboard players set #pc_cd security 0

# Computer ON -> play PC loop from the screen
execute if score #power_cur security matches 1 if score #pc_cd security matches 0 positioned -70 139 52 run playsound mymap:pc record @a[distance=..32] ~ ~ ~ 0.5 1
execute if score #power_cur security matches 1 if score #pc_cd security matches 0 run scoreboard players set #pc_cd security 190

# Read left button
scoreboard players set #left_cur security 0
execute if block -69 138 51 #minecraft:buttons[powered=true] run scoreboard players set #left_cur security 1

# Read right button
scoreboard players set #right_cur security 0
execute if block -71 138 51 #minecraft:buttons[powered=true] run scoreboard players set #right_cur security 1


# Lever OFF = show static screen
execute if score #power_cur security matches 0 run data merge entity @e[type=minecraft:item_display,tag=security_screen,limit=1] {item:{id:"minecraft:carrot_on_a_stick",count:1,components:{"minecraft:item_model":"fog:static"}}}


# Lever just turned ON = show current camera image
execute if score #power_cur security matches 1 if score #power_prev security matches 0 run function ship:security_apply


# Left button pressed = previous image
execute if score #power_cur security matches 1 if score #left_cur security matches 1 if score #left_prev security matches 0 run scoreboard players remove #cam security 1
execute if score #power_cur security matches 1 if score #left_cur security matches 1 if score #left_prev security matches 0 positioned -70 139 52 run playsound mymap:click record @a[distance=..16] ~ ~ ~ 0.7 1
execute if score #cam security matches ..0 run scoreboard players set #cam security 18
execute if score #power_cur security matches 1 if score #left_cur security matches 1 if score #left_prev security matches 0 run function ship:security_apply

# Right button pressed = next image
execute if score #power_cur security matches 1 if score #right_cur security matches 1 if score #right_prev security matches 0 run scoreboard players add #cam security 1
execute if score #power_cur security matches 1 if score #right_cur security matches 1 if score #right_prev security matches 0 positioned -70 139 52 run playsound mymap:click record @a[distance=..16] ~ ~ ~ 0.7 1
execute if score #cam security matches 19.. run scoreboard players set #cam security 1
execute if score #power_cur security matches 1 if score #right_cur security matches 1 if score #right_prev security matches 0 run function ship:security_apply


# Store states for next tick
scoreboard players operation #left_prev security = #left_cur security
scoreboard players operation #right_prev security = #right_cur security
scoreboard players operation #power_prev security = #power_cur security