;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program is distributed in the hope that it will be useful,
;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;  GNU Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; GLOBL INCLUDES
;;====================================================================
.include "menu.h.s"
.include "cpctelera.h.s"

;;Global includes
.include "global/global.h.s"

;;Managers
.include "game.h.s"

;;Macros
.include "macros/math.h.s"

;;System includes
.include "sys/render.h.s"
.include "sys/music.h.s"

;;Screens
.include "assets/img/controlsscreen_c.h.s"
.include "assets/img/controlsscreen_player_c.h.s"
.include "assets/img/controlsscreen_bullet_c.h.s"
.include "assets/img/menuscreen_c.h.s"

;;====================================================================
;; PRIVATE VARIABLES
;;====================================================================

m_is_on_controls:       .db 0     ;;Whether the user is on the control's scren or not
m_is_on_bullet_controls:.db 0     ;;Whether the user in on a sub control's screen or not
m_is_on_menu:           .db 0     ;;Whether the user is on the main menu's scren or not

m_current_key_action:   .dw main_menu_actions

;;====================================================================
;; KEY ACTIONS MAPPING
;;====================================================================

main_menu_actions:
    .dw Key_Esc,    init_main_menu_visual
    .dw Key_1,      init_player_vs_ai
    .dw Key_2,      init_player_vs_player
    .dw Key_3,      init_controls
    .dw 0

controls_menu_actions:
    .dw Key_Esc,    init_main_menu_visual
    .dw Key_Space,  man_menu_select_controls
    .dw 0

;;====================================================================
;; FUNCTIONS 
;;====================================================================

man_menu_init::

    SET_VARIABLE_VALUE m_is_on_controls, 0
    SET_VARIABLE_VALUE m_is_on_menu, 0
    SET_VARIABLE_VALUE m_is_on_bullet_controls, 0

    ;;Initialize the render for the menu
    call sys_render_menu_init

    ;;Initialize the music for the menu
    call sys_music_menu_init
    
    ;;Reseting man_game variables as score
    call man_game_reset

    ;;Create the screen for the main menu
    call init_main_menu_visual
ret

man_menu_update::
    call sys_music_menu_update

    ld ix, (#m_current_key_action)
    dec ix
    dec ix
    dec ix
    dec ix

    ;;Loop checking the key_actions
loop_menu_actions:
    INCREMENT_REGISTER_2_BYTES ix, 4

    ld l, 0(ix)
    ld h, 1(ix)

    ;;Check if Key == null == 0
	ld  a, l            ;; A = H | L
	or  h
	ret z               ;;IF A == 0, Key == null, ret

    ;;Check if key is pressed
	call cpct_isKeyPressed_asm
	jr z, loop_menu_actions

    ;;Key is pressed, execute action
	ld hl, #position_after_menu_option
	push hl

    ld  l, 2(ix)    ;; / HL = action function pointer
	ld  h, 3(ix)    ;; 
	jp  (hl)

    position_after_menu_option:
ret

;;===================================================================
;; KEY ACTIONS FUNCTIONS
;;===================================================================

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Show the screen for the main menu.
;;           It doesn't activate the flag to go to the game
;; Modifies: ?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
init_main_menu_visual:

    ;;If we are on the main menu we dont want to do this again
    ld a, (#m_is_on_menu)
    IS_EQUALS 1
    ret z

    ld hl, #main_menu_actions
    ld (m_current_key_action), hl


    SET_VARIABLE_VALUE m_is_on_menu, 1
    SET_VARIABLE_VALUE m_is_on_controls, 0
    SET_VARIABLE_VALUE m_is_on_bullet_controls, 0
    
    ld hl, #_menuscreen_c_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    ;;Setting the carry Z on to not jump to the game
    ld a, #0x00
    cp #0x00
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Clean the game screen to go to the singleplayer game
;;           It does activate the flag to go to the game
;; Modifies: ?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
init_player_vs_ai:

    ;;Check if m_is_on_controls == 0 to start the game, otherwise ret
    ld a, (#m_is_on_controls)
    IS_EQUALS 1
    ret z

    call man_game_set_gamemode_singleplayer

    ;;Setting the carry Z on to jump to the game
    ld a, #0x01
    sub #0x00
ret 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Clean the game screen to go to the multiplayer game
;;           It does activate the flag to go to the game
;; Modifies: ?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
init_player_vs_player:

    ;;Check if m_is_on_controls == 0 to start the game, otherwise ret
    ld a, (#m_is_on_controls)
    IS_EQUALS 1
    ret z

    call man_game_set_gamemode_multiplayer

    ;;Setting the carry Z on to jump to the game
    ld a, #0x01
    sub #0x00
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Show the screen for the controls.
;;           It doesn't activate the flag to go to the game
;; Modifies: ?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
init_controls:

    ;;If we are on the controls we dont want to do this again
    ld a, (#m_is_on_controls)
    IS_EQUALS 1
    ret z

    ld hl, #controls_menu_actions
    ld (m_current_key_action), hl

    SET_VARIABLE_VALUE m_is_on_menu, 0
    SET_VARIABLE_VALUE m_is_on_controls, 1    

    ld hl, #_controlsscreen_player_c_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    ;;Setting the carry Z on to not jump to the game
    ld a, #0x00
    cp #0x00
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Change between the controls screens.
;;           It doesn't activate the flag to go to the game
;; Modifies: ?
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_menu_select_controls:

    ld a, (#m_is_on_bullet_controls)
    IS_EQUALS 0
    jr z, is_on_player_controls

    ;;isnt_on_player_controls
    ld hl, #_controlsscreen_player_c_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    SET_VARIABLE_VALUE m_is_on_bullet_controls, 0

    ;;Setting the carry Z on to not jump to the game
    ld a, #0x00
    cp #0x00

    ret
    
    is_on_player_controls:

    ld hl, #_controlsscreen_bullet_c_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    SET_VARIABLE_VALUE m_is_on_bullet_controls, 1

    ;;Setting the carry Z on to not jump to the game
    ld a, #0x00 
    cp #0x00

ret