#include "Hud.h"

// Hud positioning.
#define HUD_X_POSITION_ 0
#define HUD_X_POSITION_TWO_ 64
#define HUD_Y_POSITION_ 192
#define HUD_ITEM_THIN_WIDTH_BYTES_ 2

// Hud dimensions.
#define HUD_WIDTH_ONE_ 64
#define HUD_WIDTH_TWO_ 16
#define HUD_HEIGHT_ 8
#define HUD_ITEM_WIDTH_BYTES_ 3

// Hud item positions.
#define HUD_SCORE_START_ 0
#define HEALTH_START_ 13
#define LIVES_START_ 21
#define WEAPON_START_ 27
#define OXYGEN_START_ 35
#define SPANNER_START_ 45
#define RED_KEY_START_ 47
#define GREEN_KEY_START_ 49
#define YELLOW_KEY_START_ 51
#define DISK_START_ 53
#define TAPE_START_ 55
#define OBJECTIVE_GRAPHIC_START_ 58
#define OBJECTIVE_ACTION_COUNT_START_ 61
#define PICK_START_ 66
#define PICK_COUNT_START_ 69

u8 hudRedraw = TRUE;

void drawHud()
{
    if (hudRedraw)
    {
        static u8 hudText[6];

        // Draw the hud strip on the background in two parts.
        u8* memory = cpct_getScreenPtr(CPCT_VMEM_START, HUD_X_POSITION_, HUD_Y_POSITION_);
        cpct_drawSolidBox(memory, cpct_px2byteM0(2, 2), HUD_WIDTH_ONE_, HUD_HEIGHT_);
        cpct_drawSolidBox(memory + HUD_X_POSITION_TWO_, cpct_px2byteM0(2, 2), HUD_WIDTH_TWO_, HUD_HEIGHT_);

        // Draw the current game score at the bottom left.
        drawScore(levelScore, HUD_SCORE_START_, HUD_Y_POSITION_);

        // Player current health.
        cpct_drawSprite(G_hud_health, memory + HEALTH_START_, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);
        convertNumberToString(player.health, hudText);
        drawString(HEALTH_START_ + HUD_ITEM_WIDTH_BYTES_, HUD_Y_POSITION_, hudText);

        // Player lives count.
        cpct_drawSprite(G_hud_lives, memory + LIVES_START_, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);
        convertNumberToString(playerLives, hudText);
        drawString(LIVES_START_ + HUD_ITEM_WIDTH_BYTES_, HUD_Y_POSITION_, hudText);

        // Player inventory items.
        if (inventoryItems & INVENTORY_ITEM_SPANNER)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, SPANNER_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_spanner, memory, HUD_ITEM_THIN_WIDTH_BYTES_, HUD_HEIGHT_);
        }

        if (inventoryItems & INVENTORY_ITEM_KEY_RED)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, RED_KEY_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_key_red, memory, HUD_ITEM_THIN_WIDTH_BYTES_, HUD_HEIGHT_);
        }

        if (inventoryItems & INVENTORY_ITEM_KEY_GREEN)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, GREEN_KEY_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_key_green, memory, HUD_ITEM_THIN_WIDTH_BYTES_, HUD_HEIGHT_);
        }

        if (inventoryItems & INVENTORY_ITEM_KEY_YELLOW)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, YELLOW_KEY_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_key_yellow, memory, HUD_ITEM_THIN_WIDTH_BYTES_, HUD_HEIGHT_);
        }

        if (inventoryItems & INVENTORY_ITEM_PROJECTILE_WEAPON)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, WEAPON_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_projectile_weapon, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);

            // Projectile ammo count.
            convertNumberToString(player.projectileAmmo, hudText);
            drawString(WEAPON_START_ + 3, HUD_Y_POSITION_, hudText);
        }

        if (inventoryItems & INVENTORY_ITEM_SCUBA)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, OXYGEN_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_oxygen, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);

            // Oxygen count.
            convertNumberToString(player.oxygen, hudText);
            drawString(OXYGEN_START_ + 3, HUD_Y_POSITION_, hudText);
        }

        if (inventoryItems & INVENTORY_ITEM_DISK)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, DISK_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_disk, memory, HUD_ITEM_THIN_WIDTH_BYTES_, HUD_HEIGHT_);
        }

        if (inventoryItems & INVENTORY_ITEM_TAPE)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, TAPE_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_tape, memory, HUD_ITEM_THIN_WIDTH_BYTES_, HUD_HEIGHT_);
        }

        // Display objective graphics if there is an objective applied to the map.
        if (mapObjective != OBJECTIVE_TYPE_NONE)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, OBJECTIVE_GRAPHIC_START_, HUD_Y_POSITION_);

            if (mapObjective == OBJECTIVE_TYPE_FIX_MACHINERY)
            {
                // Machinery graphic on the hud.
                cpct_drawSprite(G_hud_machinery, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);
            }
            else if (mapObjective == OBJECTIVE_TYPE_TURN_VALVES)
            {
                // Valve graphic on the hud.
                cpct_drawSprite(G_hud_valve, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);
            }
            else if (mapObjective == OBJECTIVE_TYPE_COLLECT_PIPES)
            {
                // Pipe graphic on the hud.
                cpct_drawSprite(G_hud_pipe, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);
            }
            else
            {
                // Pipe location graphic on the hud.
                cpct_drawSprite(G_hud_pipe_location, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);
            }

            // The number of actions required to complete the objective.
            convertNumberToString(objectiveActionCount, hudText);
            drawString(OBJECTIVE_ACTION_COUNT_START_, HUD_Y_POSITION_, hudText);
        }

        // Display pick count if the player has picked up a pick.
        if (player.pickCount)
        {
            memory = cpct_getScreenPtr(CPCT_VMEM_START, PICK_START_, HUD_Y_POSITION_);
            cpct_drawSprite(G_hud_pick, memory, HUD_ITEM_WIDTH_BYTES_, HUD_HEIGHT_);

            // Pick count.
            convertNumberToString(player.pickCount, hudText);
            drawString(PICK_COUNT_START_, HUD_Y_POSITION_, hudText);
        }

        hudRedraw = FALSE;
    }
}