#include "game/prota.hpp"

#include <cstdlib>

#include "core/jail/jgame.hpp"
#include "core/jail/jinput.hpp"

Prota::Prota(Jd8::Surface gfx)
    : Sprite(gfx) {
    entitat.frames.reserve(82);

    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 5; x++) {
            Frame f;
            f.w = 15;
            f.h = 15;
            if (info::ctx.num_piramide == 4) {
                f.h -= 5;
            }
            f.x = x * 15;
            f.y = 20 + (y * 15);
            entitat.frames.push_back(f);
        }
    }
    for (int y = 95; y < 185; y += 30) {
        for (int x = 60; x < 315; x += 15) {
            if (x != 300 || y != 155) {
                Frame f;
                f.w = 15;
                f.h = 30;
                if (info::ctx.num_piramide == 4) {
                    f.h -= 5;
                }
                f.x = x;
                f.y = y;
                entitat.frames.push_back(f);
            }
        }
    }
    for (int y = 20; y < 50; y += 15) {
        for (int x = 225; x < 315; x += 15) {
            Frame f;
            f.w = 15;
            f.h = 15;
            if (info::ctx.num_piramide == 4) {
                f.h -= 5;
            }
            f.x = x;
            f.y = y;
            entitat.frames.push_back(f);
        }
    }

    entitat.animacions.resize(6);
    for (int i = 0; i < 4; i++) {
        entitat.animacions[i].frames = {
            static_cast<Uint8>(0 + (i * 5)),
            static_cast<Uint8>(1 + (i * 5)),
            static_cast<Uint8>(2 + (i * 5)),
            static_cast<Uint8>(1 + (i * 5)),
            static_cast<Uint8>(0 + (i * 5)),
            static_cast<Uint8>(3 + (i * 5)),
            static_cast<Uint8>(4 + (i * 5)),
            static_cast<Uint8>(3 + (i * 5)),
        };
    }

    entitat.animacions[4].frames.resize(50);
    for (int i = 0; i < 50; i++) {
        entitat.animacions[4].frames[i] = i + 20;
    }

    entitat.animacions[5].frames.resize(48);
    for (int i = 0; i < 12; i++) {
        entitat.animacions[5].frames[i] = i + 70;
    }
    for (int i = 12; i < 48; i++) {
        entitat.animacions[5].frames[i] = 81;
    }

    cur_frame = 0;
    x = 150;
    y = 30;
    o = 0;
    cycles_per_frame = 4;
    pergami = false;
    frame_pejades = 0;
}

void Prota::draw() {
    Sprite::draw();

    if (info::ctx.num_piramide == 4 && this->o != 4) {
        if ((Jg::getCycleCounter() % 40) < 20) {
            Jd8::blitCK(this->x, this->y, this->gfx, 220, 80, 15, 15, 255);
        } else {
            Jd8::blitCK(this->x, this->y, this->gfx, 235, 80, 15, 15, 255);
        }
    }
}

auto Prota::update() -> Uint8 {
    Uint8 eixir = 0;

    if (this->o < 4) {
        Uint8 dir = 4;
        if (Ji::keyPressed(SDL_SCANCODE_DOWN)) {
            if ((this->x - 20) % 65 == 0) {
                this->o = 0;
            }
            dir = this->o;
        }
        if (Ji::keyPressed(SDL_SCANCODE_UP)) {
            if ((this->x - 20) % 65 == 0) {
                this->o = 1;
            }
            dir = this->o;
        }
        if (Ji::keyPressed(SDL_SCANCODE_RIGHT)) {
            if ((this->y - 30) % 35 == 0) {
                this->o = 2;
            }
            dir = this->o;
        }
        if (Ji::keyPressed(SDL_SCANCODE_LEFT)) {
            if ((this->y - 30) % 35 == 0) {
                this->o = 3;
            }
            dir = this->o;
        }

        switch (dir) {
            case 0:
                if (this->y < 170) {
                    this->y++;
                }
                break;
            case 1:
                if (this->y > 30) {
                    this->y--;
                }
                break;
            case 2:
                if (this->x < 280) {
                    this->x++;
                }
                break;
            case 3:
                if (this->x > 20) {
                    this->x--;
                }
                break;
            default:
                break;
        }

        if (dir == 4) {
            this->cur_frame = 0;
        } else {
            this->frame_pejades++;
            if (this->frame_pejades == 15) {
                this->frame_pejades = 0;
            }
            if (Jg::getCycleCounter() % this->cycles_per_frame == 0) {
                this->cur_frame++;
                if (this->cur_frame == entitat.animacions[this->o].frames.size()) {
                    this->cur_frame = 0;
                }
            }
        }
        eixir = 0U;
    } else {
        if (Jg::getCycleCounter() % this->cycles_per_frame == 0) {
            this->cur_frame++;
            if (this->cur_frame == entitat.animacions[this->o].frames.size()) {
                if (this->o == 4) {
                    eixir = 1;
                } else {
                    eixir = 2;
                }
            }
        }
    }
    return eixir;
}
