; ════════════════════════════════════════════════════════════════════════════
; KUMOS App Template
;
; 1. Copy this file and KUMOS.sbi into a new folder.
; 2. Edit manifest.json (also in this folder) with your app's details.
; 3. Compile with SpiderBasic — output: index.html
; 4. Zip index.html + manifest.json → yourapp.zip
; 5. Install via App Manager.
;
; Checklist before shipping:
;   ☐ Unique reverse-domain id in manifest.json   e.g. com.yourname.yourapp
;   ☐ Only request permissions you actually use
;   ☐ Call KUMOS::Init() before KUMOS::Ready()
;   ☐ All KUMOS calls must happen AFTER Ready() resolves
; ════════════════════════════════════════════════════════════════════════════

IncludeFile "KUMOS.sbi"

; ── State (your app's data goes here) ────────────────────────────────────────

Global Count.i = 0

; ── UI ───────────────────────────────────────────────────────────────────────

OpenWindow(0, 0, 0, 400, 300, "Loading…",
           #PB_Window_BorderLess | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(22, 22, 34))

Global Label_Count.i = TextGadget(#PB_Any,
  0, 100, 400, 40, "…",
  #PB_Text_Center | #PB_Text_Border)
SetGadgetColor(Label_Count, #PB_Gadget_FrontColor, RGB(100, 120, 255))
SetGadgetColor(Label_Count, #PB_Gadget_BackColor,  RGB(22, 22, 34))
SetGadgetFont(Label_Count, LoadFont(#PB_Any, "sans-serif", 28))

Global Btn_Click.i = ButtonGadget(#PB_Any, 150, 160, 100, 32, "Click me")
Global Btn_Toast.i = ButtonGadget(#PB_Any,  50, 210, 130, 28, "Toast")
Global Btn_Confirm.i= ButtonGadget(#PB_Any, 220, 210, 130, 28, "Confirm")

; ── Helpers ───────────────────────────────────────────────────────────────────

Procedure UpdateUI()
  SetGadgetText(Label_Count, Str(Count))
EndProcedure

; ── Storage callbacks ─────────────────────────────────────────────────────────

Procedure OnSaved(Success.i, Data.s)
  If Not Success
    KUMOS::Notify_Toast("Save failed: " + Data, KUMOS::#Warning)
  EndIf
EndProcedure

Procedure OnLoaded(Success.i, Data.s)
  If Success
    Protected N.i = Val(Data)
    If N > 0 : Count = N : UpdateUI() : EndIf
  EndIf
  ; File missing on first launch is normal — just start from zero.
EndProcedure

Procedure SaveCount()
  KUMOS::Storage_Write("/count.txt", Str(Count), @OnSaved())
EndProcedure

; ── Button events ─────────────────────────────────────────────────────────────

Procedure OnConfirmResult(Result.i)
  If Result
    KUMOS::Notify_Toast("You clicked OK!", KUMOS::#Success)
  Else
    KUMOS::Notify_Toast("You clicked Cancel.", KUMOS::#Info)
  EndIf
EndProcedure

BindGadgetEvent(Btn_Click, @Clicked())
Procedure Clicked()
  Count + 1
  UpdateUI()
  SaveCount()
EndProcedure

BindGadgetEvent(Btn_Toast, @ToastClicked())
Procedure ToastClicked()
  KUMOS::Notify_Toast("Hello from SpiderBasic!", KUMOS::#Success)
EndProcedure

BindGadgetEvent(Btn_Confirm, @ConfirmClicked())
Procedure ConfirmClicked()
  KUMOS::Notify_Confirm("Are you sure?", "This is a confirm dialog.", @OnConfirmResult())
EndProcedure

; ── Boot: always KUMOS::Init() then KUMOS::Ready() ────────────────────────────

Procedure OnReady(Success.i, Data.s)
  If Not Success
    ; Running outside KUMO.S — useful for testing in a plain browser
    SetWindowTitle(0, "Template App (standalone)")
    UpdateUI()
    ProcedureReturn
  EndIf

  ; Set the window title via the shell
  KUMOS::Window_SetTitle("Template App")

  ; Load persisted state
  KUMOS::Storage_Read("/count.txt", @OnLoaded())

  UpdateUI()
EndProcedure

KUMOS::Init()
KUMOS::Ready(@OnReady())

; IDE Options = SpiderBasic 3.20 (Windows - x86)
; CursorPosition = 1
; Folding = --
; iOSAppOrientation = 0
; AndroidAppCode = 0
; AndroidAppOrientation = 0
; EnableXP
; DPIAware
; CompileSourceDirectory