;; MIT License
;; 
;; Copyright (c) 2021 capAI / Aitana Devis  Ávila (@AitanaDevis) / Carlos Peñarrubia Morcillo  (@carlospm12) / Pablo Ruiz Ponce (@pabloruizp)
;; 
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; 
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; 
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

.include "entity.h.s"
.include "cpctelera.h.s"
.include "../sprites/menu_z.h.s"
.include "../sprites/controles_z.h.s"
.include "../sprites/creditos_z.h.s"
.globl cpct_scanKeyboard_asm
.globl cpct_isKeyPressed_asm
.globl cpct_drawSprite_asm
.globl cpct_zx7b_decrunch_s_asm
.globl cpct_setPALColour_asm
.globl cpct_setPALColour_asm
.globl _pal_main2
.globl cpct_setPalette_asm

PRESS_1=#1
PRESS_2=#2
PRESS_3=#3

;;////////////////////////////////////////////////////////
;;  MENU_CONFIG: Configures the colors of the palette
;;////////////////////////////////////////////////////////
_menu_config:
    ld hl, #0x1400
    call cpct_setPALColour_asm       ;; SETEAMOS EL FONDO NEGRO
    
    ld hl, #0x1410
    call cpct_setPALColour_asm       ;; PINTAMOS EL BORDE DE NEGRO

    ld hl, #_pal_main2
    ld de, #16                    ;;TAMAÑO DE LA PALETA 
    call cpct_setPalette_asm    ;;SETEAMOS LA PALETA DEL SPRITE DOGGO

    ret

;;////////////////////////////////////////////////////////
;;  MENU_INIT: Initializes the menu
;;////////////////////////////////////////////////////////
_menu_init:

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

    ld hl, #_menu_z_end
    ld de, #0xBFFF
    call cpct_zx7b_decrunch_s_asm

    ret

;;////////////////////////////////////////////////////////
;;  MENU_LOGIC: In charge of the logic of the menu
;;////////////////////////////////////////////////////////
_menu_logic:

    loop_menu_logic:

    call _menu_read_keyboard
    
    cp #PRESS_1
    jr z, fin_menu_logic

    cp #PRESS_2
    jr nz, check_press_3
    call _menu_controls
    call wait_for_esc
    call _menu_init
    jr loop_menu_logic

    check_press_3:
    cp #PRESS_3
    jr nz, loop_menu_logic
    call _menu_credits
    call wait_for_esc
    call _menu_init
    jr loop_menu_logic
    
    fin_menu_logic:
    ret

;;////////////////////////////////////////////////////////////////////////
;;  MENU_READ_KEYBOARD: In charge of reading the input for the menu options
;;////////////////////////////////////////////////////////////////////////
_menu_read_keyboard:
    loop_read_keyboard:
    call cpct_scanKeyboard_asm

    ld hl, #Key_1
    call cpct_isKeyPressed_asm  ;;Compruebo si está la tecla 1 pulsada
    cp #0
    jr z, check_keyboard_2
    ld a, #PRESS_1
    jr finish_read_keyboard

    check_keyboard_2:
    ld hl, #Key_2
    call cpct_isKeyPressed_asm 
    cp #0
    jr z, check_keyboard_3
    ld a, #PRESS_2
    jr finish_read_keyboard

    check_keyboard_3:
    ld hl, #Key_3
    call cpct_isKeyPressed_asm 
    cp #0
    jr z, check_keyboard_fin
    ld a, #PRESS_3
    jr finish_read_keyboard

    check_keyboard_fin:
    jr loop_read_keyboard

    finish_read_keyboard:
    ret

;;////////////////////////////////////////////////////////////////////////
;;  MENU_CONTROLS: In charge of unzip the image showing the game controls
;;////////////////////////////////////////////////////////////////////////
_menu_controls:
    ld hl, #_controles_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    ld hl, #_controles_z_end
    ld de, #0xBFFF
    call cpct_zx7b_decrunch_s_asm

    ret

;;////////////////////////////////////////////////////////////////////////
;;  MENU_CREDITS: In charge of unzip the image showing the game credits
;;////////////////////////////////////////////////////////////////////////
_menu_credits:
    ld hl, #_creditos_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    ld hl, #_creditos_z_end
    ld de, #0xBFFF
    call cpct_zx7b_decrunch_s_asm

    ret

;;////////////////////////////////////////////////////////////////////////
;;  WAIT_FOR_ESC: Waits until the player press the ESC key for exiting the actual screen
;;////////////////////////////////////////////////////////////////////////
wait_for_esc:
    loop_wait_for_esc:
        call cpct_scanKeyboard_asm
        ld hl, #Key_Esc
        call cpct_isKeyPressed_asm 
        cp #0
    jr z, loop_wait_for_esc

    ret
