#!/usr/bin/bash
echo "NOTE: Because this script installs components of x11 there may be some minor changes to your OSes UI! (like changed button designs)"
sleep 3
echo "This script downloads the game and the implementation for the hotbar to be scrollable, installs every needed module to compile it, then compiles it and runs it. If you don't want to do this or had the script running successfully before press Ctrl+C now to cancel this script. This script will continue in 10 seconds."



mkdir $HOME/.cache &> /dev/null
cat << '**ENDOFFILE**' > $HOME/.cache/countdownfrom10.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
try:
        import time as t
        print("\r10 [==========]", end="\r")
        t.sleep(1)
        print("\r09 [=========-]", end="\r")
        t.sleep(1)
        print("\r08 [========--]", end="\r")
        t.sleep(1)
        print("\r07 [=======---]", end="\r")
        t.sleep(1)
        print("\r06 [======----]", end="\r")
        t.sleep(1)
        print("\r05 [=====-----]", end="\r")
        t.sleep(1)
        print("\r04 [====------]", end="\r")
        t.sleep(1)
        print("\r03 [===-------]", end="\r")
        t.sleep(1)
        print("\r02 [==--------]", end="\r")
        t.sleep(1)
        print("\r01 [=---------]", end="\r")
        t.sleep(1)
        print("\r00 [----------]", end="\r")
        print("\r__ [ EXPIRED! ]", end="\r")
except KeyboardInterrupt:
        print("\rKBD[INTERRUPT!]", end="\r")
except not KeyboardInterrupt:
        print("\r__ [  ERROR!  ]", end="\r")
**ENDOFFILE**

python3 $HOME/.cache/countdownfrom10.py



echo "Updating system..."
sudo apt update -y
sudo apt upgrade -y

echo "Updating/installing git..."
sleep 0.5
sudo apt install git -y

echo "Starting download using git..."
sleep 0.5

git clone --recurse-submodules https://github.com/jdah/minecraft-weekend.git

echo "Opening directory..."
sleep 0.5

cd minecraft-weekend



echo "Modding game code..."
sleep 0.5

cat << '**ENDOFFILE**' > src/ui/hotbar.c
#include "hotbar.h"
#include "ui.h"

#include "../state.h"
#include "../gfx/renderer.h"
#include "../block/block.h"

const f32 SLOT_PIXELS = 40.0f;

const f32 ICON_OFFSET_PIXELS = 6.0f;
const f32 ICON_SIZE_PIXELS = 28.0f;

static void render_icon(vec2s offset, enum BlockId block_id) {
    mat4s m = glms_mat4_identity();
    m = glms_translate(m, (vec3s) {{
        offset.x + ICON_OFFSET_PIXELS,
        offset.y + ICON_OFFSET_PIXELS,
        0.0f }});
    m = glms_scale(m, (vec3s) {{ ICON_SIZE_PIXELS, ICON_SIZE_PIXELS, 1.0f }});

    vec2s uv_min, uv_max;
    atlas_get(
        state.renderer.block_atlas.atlas,
        BLOCKS[block_id].get_texture_location(&state.world, GLMS_IVEC3_ZERO, UP),
        &uv_min, &uv_max);
    renderer_quad_texture(
        &state.renderer, state.renderer.block_atlas.atlas.texture,
        (vec2s) {{ 1.0f, 1.0f }},
        (vec4s) {{ 1.0f, 1.0f, 1.0f, 1.0f }},
        uv_min, uv_max, m);
}

static void render(struct UIHotbar *self) {
    vec2s base_offset = (vec2s) {{ (state.window->size.x - (HOTBAR_SLOTS * SLOT_PIXELS)) / 2.0f, 16.0f }};
    
    for (size_t i = 0; i < HOTBAR_SLOTS; i++) {
        vec2s offset = glms_vec2_add((vec2s) {{ i * SLOT_PIXELS, 0 }}, base_offset);

        renderer_quad_texture(
            &state.renderer, state.renderer.textures[TEXTURE_HOTBAR],
            (vec2s) {{ SLOT_PIXELS, SLOT_PIXELS }},
            (vec4s) {{ 1.0f, 1.0f, 1.0f, 1.0f }},
            (vec2s) {{ 0.0f, 0.0f }}, (vec2s) {{ 0.5f, 1.0f }},
            glms_translate_make((vec3s) {{ offset.x, offset.y, 0.0f }}));

        if (i == self->index) {
            renderer_quad_texture(
                &state.renderer, state.renderer.textures[TEXTURE_HOTBAR],
                (vec2s) {{ SLOT_PIXELS, SLOT_PIXELS }},
                (vec4s) {{ 1.0f, 1.0f, 1.0f, 1.0f }},
                (vec2s) {{ 0.5f, 0.0f }}, (vec2s) {{ 1.0f, 1.0f }},
                glms_translate_make((vec3s) {{ offset.x, offset.y, 0.0f }}));
        }
        
        render_icon(offset, self->values[i]);
    }
}

static void update(struct UIHotbar *self) {
    for (size_t i = 0; i < 10; i++) {
        if (state.window->keyboard.keys[GLFW_KEY_0 + i].pressed) {
            self->index = i == 0 ? 9 : (i - 1);
        }
    }
}

static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset){
    // Y-offset is either 1 or -1 (scroll up or down)
    struct UIHotbar* self = (struct UIHotbar*)glfwGetWindowUserPointer(window);
    // Scroll left 
    if (yoffset == 1){
        if (self->index == 0){
            self->index = 9;
        }else{
            self->index -= 1;
        }
    // Scroll right
    }else if(yoffset == -1){
        self->index += 1;
        self->index = self->index % 10;
    }
    return;
}


struct UIComponent hotbar_init(struct UIHotbar *self) {
    self->index = 0;
    glfwSetWindowUserPointer(state.window->handle,self);
    glfwSetScrollCallback(state.window->handle,scrollCallback);


    memcpy(self->values, (enum BlockId[]) {
            GRASS,
            DIRT,
            STONE,
            COBBLESTONE,
            PLANKS,
            LOG,
            GLASS,
            ROSE,
            TORCH,
            SAND,
            LEAVES
        }, HOTBAR_SLOTS * sizeof(enum BlockId));

    return (struct UIComponent) {
        .component = self,
        .render = (FUIComponent) render,
        .update = (FUIComponent) update,
        .tick = NULL
    };
}
**ENDOFFILE**



echo "Installing other needed packages..."
sleep 0.5

sudo apt install make -y
sudo apt install cmake -y
sudo apt install clang -y
sudo apt install libx11-dev -y
sudo apt install libxrandr-dev -y
sudo apt install libxinerama-dev -y
sudo apt install libxcursor-dev -y
sudo apt install libxi-dev -y

echo "Starting to compile: Compiling librarys..."
sleep 0.5

make --ignore-errors libs

echo "Starting to compile: Compiling the game itself..."
sleep 0.5

make --ignore-errors

echo "Creating script for making launching easier later..."
sleep 0.5

cd ../

cat << '**ENDOFFILE**' > run-mc-weekend.sh
prev_path=${pwd}
cd $HOME/minecraft-weekend
bin/game
cd $prev_path
**ENDOFFILE**

chmod +x run-mc-weekend.sh
cd minecraft-weekend

echo "Now let's try to run the game!"
sleep 0.5

bin/game
if [ $? == 127 ];
then
    echo "The game isn't there? Exiting..."
    python3 $HOME/.cache/countdownfrom10.py
    kill -INT $$
fi

echo "Game closed."
sleep 1
echo "To run the game again, simply run 'bin/game' in ~/minecraft-weekend or run './run-mc-weekend.sh' in your home directory (~/)."
sleep 1
echo "If 'run-mc-weekend.sh' is missing, execute 'wget https://dateien.lampe2020/dateien/run-mc-weekend.sh' or download the script from that location using a web browser."
sleep 3

echo "Ending script in 10 seconds..."

python3 $HOME/.cache/countdownfrom10.py

rm $HOME/.cache/countdownfrom10.py
