#include "InGameState.h"
#include <iostream>

bool keys[4] = {false, false, false, false};

void InGameState::spawnItem(Type type) {
    Item item{};
    item.type = type;
    item.x = rand() % WINDOW_WIDTH;
    item.y = rand() % WINDOW_HEIGHT;

    items.push_back(item);
}

void InGameState::renderPlayer(SDL_Renderer* renderer, SDL_Texture* playerTexture, Player p) const {
    drawTexture(renderer, playerTexture, p.x, p.y, 80, 96);

    if (showHitboxes) {
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
        SDL_Rect hitbox = {p.x, p.y, 80, 96};
        SDL_RenderDrawRect(renderer, &hitbox);
    }
}

void InGameState::checkCollision() {
    for (int i = 0; i < opponents.size(); i++) {
        Opponent o = opponents[i];
        if (o.x == o.targetX && o.y == o.targetY) {
            opponents.erase(opponents.begin() + i);
        }
        if (player.x < o.x + 80 && player.x + 80 > o.x && player.y < o.y + 96 && player.y + 96 > o.y) {
            if (player.health > 0) {
                player.health--;
            }
            else {
                // TODO: Game over sccreen
            }

            opponents.erase(opponents.begin() + i);
        }
    }

    for (int i = 0; i < items.size(); i++) {
        Item item = items[i];
        if (player.x < item.x + 24 && player.x + 80 > item.x && player.y < item.y + 24 && player.y + 96 > item.y) {
            switch (item.type) {
            case Type::HEALTH:
                if (player.health < 3) {
                    player.health++;
                }
                break;
            case Type::DAMAGE:
                player.health--;
                break;
            }

            items.erase(items.begin() + i);
        }
    }
}

void InGameState::renderItem(SDL_Renderer* renderer, Item i) const {
    SDL_Texture* itemTexture = IMG_LoadTexture(renderer, i.type == Type::HEALTH ? "./res/heart.png" : "./res/damage.png");
    switch (i.type) {
    case Type::HEALTH:
        drawTexture(renderer, itemTexture, i.x, i.y, 24, 24);
        SDL_DestroyTexture(itemTexture);
        break;
    case Type::DAMAGE:
        drawTexture(renderer, itemTexture, i.x, i.y, 24, 24);
        SDL_DestroyTexture(itemTexture);

        break;
    default: break;
    }

    if (showHitboxes) {
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
        SDL_Rect hitbox = {i.x, i.y, 24, 24};
        SDL_RenderDrawRect(renderer, &hitbox);
    }
}

void InGameState::createOpponent() {
    Opponent o;

    int edge = rand() % 4;
    switch (edge) {
    case 0:
        o.x = rand() % WINDOW_WIDTH;
        o.y = -96;
        o.targetY = WINDOW_HEIGHT + 96;
        o.targetX = rand() % WINDOW_WIDTH;
        break;
    case 1:
        o.x = rand() % WINDOW_WIDTH;
        o.y = WINDOW_HEIGHT;
        o.targetY = -96;
        o.targetX = rand() % WINDOW_WIDTH;
        break;
    case 2:
        o.x = -80;
        o.y = rand() % WINDOW_HEIGHT;
        o.targetX = WINDOW_WIDTH + 80;
        o.targetY = rand() % WINDOW_HEIGHT;
        break;
    case 3:
        o.x = WINDOW_WIDTH;
        o.y = rand() % WINDOW_HEIGHT;
        o.targetX = -80;
        o.targetY = rand() % WINDOW_HEIGHT;
        break;
    }

    opponents.push_back(o);
}

void InGameState::renderOpponent(SDL_Renderer* renderer, SDL_Texture* opponentTexture, Opponent o) const {
    drawTexture(renderer, opponentTexture, o.x, o.y, 80, 96);

    if (showHitboxes) {
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
        SDL_Rect hitbox = {o.x, o.y, 80, 96};
        SDL_RenderDrawRect(renderer, &hitbox);
    }
}

void InGameState::renderHUD(SDL_Renderer* renderer, SDL_Texture* heart) const {
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_Rect bgRect = {0, 0, 30 * 3 + 15, 40};
    SDL_RenderFillRect(renderer, &bgRect);

    for (int i = 0; i < this->player.health; i++) {
        drawTexture(renderer, heart, 10 + i * 30, 10, 24, 24);
    }
}

void InGameState::init() {
    startTime = time(nullptr);
}


void InGameState::handleEvents(SDL_Event& event) {
    if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
        case SDLK_ESCAPE:
            std::cout << "Switching to main menu\n";
            this->changeRoom("main_menu");
            break;
        default:
            break;
        }
    }

    if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
        bool isPressed = (event.type == SDL_KEYDOWN);
        switch (event.key.keysym.sym) {
        case SDLK_w:
            keys[0] = isPressed;
            break;
        case SDLK_a:
            keys[1] = isPressed;
            break;
        case SDLK_s:
            keys[2] = isPressed;
            break;
        case SDLK_d:
            keys[3] = isPressed;
            break;
        case SDLK_h:
            if (isPressed) showHitboxes = !showHitboxes;
            break;
        }
    }
}

void InGameState::update() {

    checkCollision();

    if (rand() % 100 == 0) createOpponent();

    if (time(nullptr) - startTime > 5) {
    startTime = time(nullptr);
    spawnItem(Type::HEALTH);
    }

    for (Opponent& o : opponents) {
        o.move(10);
    }

    if (keys[0]) player.moveY(-20);
    if (keys[1]) player.moveX(-20);
    if (keys[2]) player.moveY(20);
    if (keys[3]) player.moveX(20);
}

void InGameState::render(SDL_Renderer* renderer) {
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 35, 39, 42, SDL_ALPHA_OPAQUE);

    SDL_Texture* background = IMG_LoadTexture(renderer, "./res/background.png");
    drawTexture(renderer, background, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

    SDL_Texture* playerTexture = IMG_LoadTexture(renderer, "./res/player.png");
    renderPlayer(renderer, playerTexture, this->player);

    SDL_Texture* opponentTexture = IMG_LoadTexture(renderer, "./res/opponent.png");
    for (Opponent o : this->opponents) {
        renderOpponent(renderer, opponentTexture, o);
    }

    for (Item i : this->items) {
        renderItem(renderer, i);
    }

    SDL_Texture* heart = IMG_LoadTexture(renderer, "./res/heart.png");
    renderHUD(renderer, heart);

    TTF_Font* font = TTF_OpenFont("./res/font.ttf", 24);

    drawText(renderer, font, "h: " + std::to_string(player.health), 30 * 3 + 15 + 10, 10);

    SDL_DestroyTexture(playerTexture);
    SDL_DestroyTexture(opponentTexture);
    SDL_DestroyTexture(heart);

    SDL_RenderPresent(renderer);
}
