; ***************************************************************************
; 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_GUI2_AQHOME_D_LIGHT_SETTINGS_ASM
#define AQH_AVR_GUI2_AQHOME_D_LIGHT_SETTINGS_ASM


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

.equ DLGLIGHTSETTINGS_OFFS_BEGIN            = CDIALOG_SIZE
.equ DLGLIGHTSETTINGS_OFFS_SETTINGS_LO      = DLGLIGHTSETTINGS_OFFS_BEGIN+0
.equ DLGLIGHTSETTINGS_OFFS_SETTINGS_HI      = DLGLIGHTSETTINGS_OFFS_BEGIN+1
.equ DLGLIGHTSETTINGS_SIZE                  = DLGLIGHTSETTINGS_OFFS_BEGIN+2


; selectors
.equ DLGLIGHTSETTINGS_SEL_DIALOG_OK    = 1
.equ DLGLIGHTSETTINGS_SEL_DIALOG_ABORT = 2
.equ DLGLIGHTSETTINGS_SEL_NAME         = 3
.equ DLGLIGHTSETTINGS_SEL_SUFFIX       = 4
.equ DLGLIGHTSETTINGS_SEL_LEDCOUNT     = 5
.equ DLGLIGHTSETTINGS_SEL_ONTIME       = 6
.equ DLGLIGHTSETTINGS_SEL_BTN_CONNS    = 7
.equ DLGLIGHTSETTINGS_SEL_DLG_CONNS    = 8




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

.cseg



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_new @global
;
; @param Y pointer to GUIAPP
; @param r21:r20 pointer to settings
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object

DlgLightSettings_new:
  bigcall GuiApp_GetRootWindow        ; r19:r18=root window (none)
  brcc DlgLightSettings_new_ret
  mov xl, r18                         ; use root window as parent for main window
  mov xh, r19
  ldi r24, LOW(DLGLIGHTSETTINGS_SIZE)
  ldi r25, HIGH(DLGLIGHTSETTINGS_SIZE)
  push r20
    push r21
      bigcall Object_Alloc            ; Y=space (!r16, !r17, !X)
    pop r21
  pop r20
  brcc DlgLightSettings_new_ret
  rcall DlgLightSettings_Init
DlgLightSettings_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_Init @global
;
; @param Y pointer to newly allocated widget data
; @param X pointer to root window
; @param r21:r20 pointer to settings
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

DlgLightSettings_Init:
  push r20
    push r21
      ldi r16, (1<<DIALOG_OPTS_STDBTNS_BIT)                                                                ; OPTS
      ldi r17, (WIDGET_PACK_FILLED<<WIDGET_PACK_HSELF0_BIT) | (WIDGET_PACK_FILLED<<WIDGET_PACK_VSELF0_BIT) ; PACK
      ldi r20, LOW(RESSOURCE_TXT_LIGHTSETTINGS)
      ldi r21, HIGH(RESSOURCE_TXT_LIGHTSETTINGS)
      ldi r22, LOW(DlgLightSettings_Config*2)
      ldi r23, HIGH(DlgLightSettings_Config*2)
      bigcall CDialog_Init               ; Y=main window
    pop r21
  pop r20
  brcc DlgLightSettings_Init_ret

  ; store data
  std Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_LO, r20
  std Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_HI, r21

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

DlgLightSettings_Init_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_toGui @global
;
; @param  Y       pointer to dialog

DlgLightSettings_toGui:
  ldd zl, Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_LO
  ldd zh, Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_HI

  ; name
  ldd xl, Z+LIGHTSETTINGS_OFFS_NAME
  clr xh
  ldi r16, DLGLIGHTSETTINGS_SEL_NAME
  bigcall CDialog_SetChildValue

  ; suffix
  ldd xl, Z+LIGHTSETTINGS_OFFS_IDX
  clr xh
  ldi r16, DLGLIGHTSETTINGS_SEL_SUFFIX
  bigcall CDialog_SetChildValue

  ; LED count
  ldd xl, Z+LIGHTSETTINGS_OFFS_LEDNUM_LO
  ldd xh, Z+LIGHTSETTINGS_OFFS_LEDNUM_HI
  ldi r16, DLGLIGHTSETTINGS_SEL_LEDCOUNT
  bigcall CDialog_SetChildValue

  ; ON time
  ldd xl, Z+LIGHTSETTINGS_OFFS_ONTIME_LO
  ldd xh, Z+LIGHTSETTINGS_OFFS_ONTIME_HI
  ldi r16, DLGLIGHTSETTINGS_SEL_ONTIME
  bigcall CDialog_SetChildValue

  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_fromGui @global
;
; @param  Y       pointer to dialog

DlgLightSettings_fromGui:
  ldd zl, Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_LO
  ldd zh, Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_HI
  
  ; name
  ldi r16, DLGLIGHTSETTINGS_SEL_NAME
  rcall CDialog_GetChildValue
  std Z+LIGHTSETTINGS_OFFS_NAME, r18

  ; suffix
  ldi r16, DLGLIGHTSETTINGS_SEL_SUFFIX
  rcall CDialog_GetChildValue
  std Z+LIGHTSETTINGS_OFFS_IDX, r18

  ; LED count
  ldi r16, DLGLIGHTSETTINGS_SEL_LEDCOUNT
  rcall CDialog_GetChildValue
  std Z+LIGHTSETTINGS_OFFS_LEDNUM_LO, r18
  std Z+LIGHTSETTINGS_OFFS_LEDNUM_HI, r19

  ; ON time
  ldi r16, DLGLIGHTSETTINGS_SEL_ONTIME
  rcall CDialog_GetChildValue
  std Z+LIGHTSETTINGS_OFFS_ONTIME_LO, r18
  std Z+LIGHTSETTINGS_OFFS_ONTIME_HI, r19

  ret
; @end





; ***************************************************************************
; signal handlers

; ---------------------------------------------------------------------------
; @routine DlgLightSettings_OnToGui @global
;
; @param  Y       pointer to dialog
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

DlgLightSettings_OnToGui:
  rcall DlgLightSettings_toGui
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_OnFromGui @global
;
; @param  Y       pointer to dialog
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

DlgLightSettings_OnFromGui:
  rcall DlgLightSettings_fromGui
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_OnCmdConns @global
;
; @param  Y       pointer to dialog
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

DlgLightSettings_OnCmdConns:
  push yl
    push yh
      bigcall Widget_GetApp                     ; R19:R18=gui
      mov yl, r18
      mov yh, r19
      ldd r20, Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_LO
      ldd r21, Y+DLGLIGHTSETTINGS_OFFS_SETTINGS_HI
      bigcall DlgLightConns_new
      mov xl, yl
      mov xh, yh
    pop yh
  pop yl
  brcc DlgLightSettings_OnCmdConns_ret
  rcall Dialog_EnterSubDialog
DlgLightSettings_OnCmdConns_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine DlgLightSettings_OnDlgEndConns @global
;
; @param  Y       pointer to dialog
; @return CFLAG set of okay, cleared otherwise
; @clobbers any, !Y

DlgLightSettings_OnDlgEndConns:
  cpi xl, DIALOG_RESULT_OK
  brne DlgLightSettings_OnDlgEndConns_leave
  ; nothing TODO for now
DlgLightSettings_OnDlgEndConns_leave:
  bigcall Dialog_LeaveSubDialog
  sec
  ret
; @end





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


DlgLightSettings_Config:
  .db CWIDGETCFG_MAINWIDGET_TYPE_MLAYOUT, 0                 ; TYPE, FLAGS
  .dw 0                                                     ; STYLE
  .db 2, 0                                                  ; PARAM1, PARAM2
  
  ; line 1: name
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
         (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDNAME                                 ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_TEXTSEL, 0                     ; TYPE, FLAGS
  .db DLGLIGHTSETTINGS_SEL_NAME, 0                          ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw (DlgLightSettings_Names*2)                            ; PARAM1, PARAM2 (pointer to name list)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; line 2: suffix
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
         (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
         (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDNAME                                 ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db DLGLIGHTSETTINGS_SEL_SUFFIX, 0                        ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 9                                                  ; MINVAL, MAXVAL

  ; line 3: LED count
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDCOUNT                                ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db DLGLIGHTSETTINGS_SEL_LEDCOUNT, 0                      ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 1, 300                                                ; MINVAL, MAXVAL

  ; line 4: ON time
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)    ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_LEDONTIME                               ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_HSPINNER, (1<<CWIDGETCFG_WIDGET_FLAGS_MINMAXVALUES_BIT)  ; TYPE, FLAGS
  .db DLGLIGHTSETTINGS_SEL_ONTIME, 0                        ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .db 0, 5                                                  ; PARAM1, PARAM2 (postkomma digits, total digits)
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 1, 60000                                              ; MINVAL, MAXVAL

  ; line 5: connections (TODO)
  ; left widget
  .db CWIDGETCFG_WIDGET_TYPE_LABEL, 0                       ; TYPE, FLAGS
  .db 0, 0                                                  ; SELECTOR, RESERVED
  .db 0, \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw 0                                                     ; STYLE
  .dw RESSOURCE_TXT_CONN_S                                  ; PARAM1, PARAM2
  .dw 0                                                     ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; right widget
  .db CWIDGETCFG_WIDGET_TYPE_TEXTBUTTON, 0                  ; TYPE, FLAGS
  .db DLGLIGHTSETTINGS_SEL_BTN_CONNS, 0                     ; SELECTOR, RESERVED
  .db (1<<WIDGET_OPTS_BORDER_BIT), \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HSELF0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VSELF0_BIT) | \
      (WIDGET_PACK_BEGIN<<WIDGET_PACK_HCONTENT0_BIT) | \
      (WIDGET_PACK_CENTER<<WIDGET_PACK_VCONTENT0_BIT)       ; OPTS, PACK
  .dw (DefaultStyle_SpacyButton*2)                          ; STYLE
  .dw RESSOURCE_TXT_CONN_SETUP                              ; PARAM1, PARAM2 (text ressource)
  .db 0, BUTTON_MODE_NORMAL                                 ; PARAM3, PARAM4
  .dw 0, 0                                                  ; MINVAL, MAXVAL

  ; end of configuration
  .dw 0



DlgLightSettings_Names:        ; @todo move to lightsettings.asm
  .dw RESSOURCE_TXT_LEDNAME_MAIN
  .dw RESSOURCE_TXT_LEDNAME_CUPBOARD
  .dw RESSOURCE_TXT_LEDNAME_WALL
  .dw 0



DlgLightSettings_DefaultSignalmap:
  ; header
  .dw CDialog_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,                              DIALOG_SIGNAL_TOGUI,      LOW(DlgLightSettings_OnToGui),       HIGH(DlgLightSettings_OnToGui)
  .db 0,                              DIALOG_SIGNAL_FROMGUI,    LOW(DlgLightSettings_OnFromGui),     HIGH(DlgLightSettings_OnFromGui)
  .db DLGLIGHTSETTINGS_SEL_BTN_CONNS, WIDGET_SIGNAL_COMMAND,    LOW(DlgLightSettings_OnCmdConns),    HIGH(DlgLightSettings_OnCmdConns)
  .db DLGLIGHTSETTINGS_SEL_DLG_CONNS, WIDGET_SIGNAL_DIALOG_END, LOW(DlgLightSettings_OnDlgEndConns), HIGH(DlgLightSettings_OnDlgEndConns)
  ; end of table
  .db 0, 0, 0, 0







#endif

