#include "nodes/hand.h"

Hand::Hand() {
    // Initialization things here
}

Hand::~Hand() = default;

void Hand::drawCard() {
    Card *newCard = new Card();
    addChild(newCard);
    organizeDeck();
}

void Hand::removeCard() {
    if (children.size() > 0) {
        Card *removedCard = (Card*)children.back();
        children.erase(children.end());
        std::free(removedCard);
        printf("removed a card\n");
    }
    organizeDeck();
}

void Hand::organizeDeck() {
    int positionXOffset = 0;
    for (Node *card : children) {
        card->localPosition.x = positionXOffset;
        positionXOffset += 35;
    }
}

void Hand::update() {
    Node::update();
}

void Hand::pollInputs() {
    Node::pollInputs();

    for (SDL_Event e : InputManager::getFrameEvents())
    {
        if (e.type == SDL_KEYDOWN)
        {
            if (e.key.keysym.sym == SDLK_SPACE)
            {
                drawCard();
            }
            if (e.key.keysym.sym == SDLK_d)
            {
                removeCard();
            }
        }
    }
}

void Hand::free() {
    Node::free();
}