; ***************************************************************************
; copyright   : (C) 2026 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************

#ifndef AQH_AVR_W_MENU_ASM
#define AQH_AVR_W_MENU_ASM


; ***************************************************************************
; defines

.equ MENUWIN_OFFS_BEGIN   = DIALOG_SIZE
.equ MENUWIN_SIZE         = MENUWIN_OFFS_BEGIN+0


.equ MENUWIN_SIGNAL_CLIMATE  =  WIDGET_SIGNAL_NEXTFREE+0
.equ MENUWIN_SIGNAL_LIGHT    =  WIDGET_SIGNAL_NEXTFREE+1
.equ MENUWIN_SIGNAL_WINDOWS  =  WIDGET_SIGNAL_NEXTFREE+2
.equ MENUWIN_SIGNAL_DEBUG    =  WIDGET_SIGNAL_NEXTFREE+3
.equ MENUWIN_SIGNAL_NETSTATS =  WIDGET_SIGNAL_NEXTFREE+4
.equ MENUWIN_SIGNAL_NEXTFREE =  WIDGET_SIGNAL_NEXTFREE+5


.equ MENUWIN_SEL_CLIMATE     = 1
.equ MENUWIN_SEL_LIGHT       = 2
.equ MENUWIN_SEL_WINDOWS     = 3
.equ MENUWIN_SEL_DEBUG       = 4
.equ MENUWIN_SEL_NETSTATS    = 5



; ***************************************************************************
; code

.cseg



; ---------------------------------------------------------------------------
; @routine MenuWin_new @global
;
; @param Y pointer to GUIAPP
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object

MenuWin_new:
  bigcall GuiApp_GetRootWindow
  brcc MenuWin_new_ret
  mov xl, r18  ; use root window as parent for main window
  mov xh, r19

  ldi r24, LOW(DLGLIGHTCONNS_SIZE)
  ldi r25, HIGH(DLGLIGHTCONNS_SIZE)
  bigcall Object_Alloc            ; Y=space (!r16, !r17, !X)
  brcc MenuWin_new_ret
  rcall MenuWin_Init
MenuWin_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_new @global
;
; @param Y pointer to new MainWindow
; @param X parent widget
; @return CFLAG set of okay, cleared otherwise

MenuWin_Init:
  ldi r16, 0                                                                                           ; OPTS
  ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
  ldi r20, LOW(RESSOURCE_TXT_AQHOME)
  ldi r21, HIGH(RESSOURCE_TXT_AQHOME)
  bigcall Dialog_Init
  brcc MenuWin_Init_ret

  ; set default signal map
  ldi r16, LOW(MenuWin_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(MenuWin_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16

MenuWin_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine menuWinCreateContent
;
; @param Y pointer to MainWindow
; @param X pointer to MainWindow content window (becomes parent)
; @return CFLAG set of okay, cleared otherwise

menuWinCreateContent:
  ; create MLayout
  push yl
    push yh
      ldi r16, 0                                                                                           ; OPTS
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
      ldi r20, 5                          ; columns per row
      bigcall MCLayout_new
      mov xl, yl                          ; use MLayout as parent
      mov xh, yh
    pop yh
  pop yl
  brcc menuWinCreateContent_ret

  ldi r20, LOW(RESSOURCE_IMG_TEMP)
  ldi r21, HIGH(RESSOURCE_IMG_TEMP)
  ldi r22, MENUWIN_SEL_CLIMATE
  rcall menuWinCreateButton
  brcc menuWinCreateContent_ret

  ldi r20, LOW(RESSOURCE_IMG_LIGHT)
  ldi r21, HIGH(RESSOURCE_IMG_LIGHT)
  ldi r22, MENUWIN_SEL_LIGHT
  rcall menuWinCreateButton
  brcc menuWinCreateContent_ret

  ldi r20, LOW(RESSOURCE_IMG_WINCLOSED)
  ldi r21, HIGH(RESSOURCE_IMG_WINCLOSED)
  ldi r22, MENUWIN_SEL_WINDOWS
  rcall menuWinCreateButton
  brcc menuWinCreateContent_ret

  ldi r20, LOW(RESSOURCE_IMG_NETWORK)
  ldi r21, HIGH(RESSOURCE_IMG_NETWORK)
  ldi r22, MENUWIN_SEL_NETSTATS
  rcall menuWinCreateButton
  brcc menuWinCreateContent_ret

  ldi r20, LOW(RESSOURCE_IMG_DEBUGEEPROM)
  ldi r21, HIGH(RESSOURCE_IMG_DEBUGEEPROM)
  ldi r22, MENUWIN_SEL_DEBUG
  rcall menuWinCreateButton
  brcc menuWinCreateContent_ret

  mov yl, xl
  mov yh, xh
  sec
menuWinCreateContent_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine menuWinCreateButton
;
; @param Y       pointer to MainWindow
; @param X       parent
; @param r21:r20 img ressource
; @param r22     selector
; @return CFLAG  set if new object created, cleared on error
; @clobbers any, !X, !Y

menuWinCreateButton:
  push xl
    push xh
      push yl
        push yh
          ldi r16, 0
          ldi r17, (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_BEGIN<<WIDGET_PACK_VSELF0_BIT) ; PACK
          ldi r23, BUTTON_MODE_NORMAL
          bigcall C03App_CreateButton
          mov xl, yl
          mov xh, yh
        pop yh
      pop yl
      brcc menuWinCreateButton_popRet
      adiw xh:xl, OBJECT_OFFS_TARGET_LO
      st X+, yl
      st X, yh
      sbiw xh:xl, (OBJECT_OFFS_TARGET_LO+1)
      sec
menuWinCreateButton_popRet:
    pop xh
  pop xl
menuWinCreateButton_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_OnMkContent

MenuWin_OnMkContent:
  bigcall MainWindow_GetContentWidget  ; r19:r18=content window
  brcc MenuWin_OnMkContent_ret
  mov xl, r18                          ; use content window as parent
  mov xh, r19

  ; Y=MainWindow, X=content window
  push yl                              ; main window
    push yh
      rcall menuWinCreateContent
    pop yh                             ; main window
  pop yl
MenuWin_OnMkContent_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_OnCmdClimate

MenuWin_OnCmdClimate:
  ldi r16, MENUWIN_SIGNAL_CLIMATE
  bigcall OBJ_EmitSignal
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_OnCmdLight

MenuWin_OnCmdLight:
  ldi r16, MENUWIN_SIGNAL_LIGHT
  bigcall OBJ_EmitSignal
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_OnCmdWindows

MenuWin_OnCmdWindows:
  ldi r16, MENUWIN_SIGNAL_WINDOWS
  bigcall OBJ_EmitSignal
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_OnCmdNetStats

MenuWin_OnCmdNetStats:
  ldi r16, MENUWIN_SIGNAL_NETSTATS
  bigcall OBJ_EmitSignal
  ret
; @end



; ---------------------------------------------------------------------------
; @routine MenuWin_OnCmdDebug

MenuWin_OnCmdDebug:
  ldi r16, MENUWIN_SIGNAL_DEBUG
  bigcall OBJ_EmitSignal
  ret
; @end






; ***************************************************************************
; data in FLASH

MenuWin_DefaultSignalmap:
  ; header
  .dw Dialog_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,                    DIALOG_SIGNAL_MKCONTENT, LOW(MenuWin_OnMkContent),   HIGH(MenuWin_OnMkContent)
  .db MENUWIN_SEL_CLIMATE,  WIDGET_SIGNAL_COMMAND,   LOW(MenuWin_OnCmdClimate),  HIGH(MenuWin_OnCmdClimate)
  .db MENUWIN_SEL_LIGHT,    WIDGET_SIGNAL_COMMAND,   LOW(MenuWin_OnCmdLight),    HIGH(MenuWin_OnCmdLight)
  .db MENUWIN_SEL_WINDOWS,  WIDGET_SIGNAL_COMMAND,   LOW(MenuWin_OnCmdWindows),  HIGH(MenuWin_OnCmdWindows)
  .db MENUWIN_SEL_NETSTATS, WIDGET_SIGNAL_COMMAND,   LOW(MenuWin_OnCmdNetStats), HIGH(MenuWin_OnCmdNetStats)
  .db MENUWIN_SEL_DEBUG,    WIDGET_SIGNAL_COMMAND,   LOW(MenuWin_OnCmdDebug),    HIGH(MenuWin_OnCmdDebug)

  .db 0, 0, 0, 0  ; end of table



#endif

