#include "node.hpp"

Node::Node(sf::Vector2f& position) : mPosition{position} {}

void Node::addChild(const std::shared_ptr<Node>& child)
{
    mChildren.push_back(child);
}

sf::Vector2f Node::getPosition()
{
    return mPosition;
}

void Node::unblock()
{
    mState = State::Unblocked;
}

void Node::block()
{
    mState = State::Blocked;
    for (const auto& child : mChildren)
        child->block();
}

void Node::onMousePressed(sf::Vector2f mouseCoords)
{
    if (mState == State::Blocked)
        return;

    if (collisionTest(mouseCoords)) 
    {
        if (mState == State::Unblocked) 
        {
            mState = State::Activated;
            for (const auto& child : mChildren)
                child->unblock();
        }

        else if (mState == State::Activated)
        {
            mState = State::Unblocked;
            for (const auto& child : mChildren)
                child->block();
        }
    }

    for (const auto& child : mChildren)
    {
        child->onMousePressed(mouseCoords);
    }
}



/* HitNode */

HitNode::HitNode(sf::Vector2f position) : Node{position} {}

void HitNode::loadTexture()
{
    sf::String texturePath = getIconPath();
    if (!mTexture.loadFromFile(texturePath))
    {
        cout << "Error! Can't load file " << texturePath.toAnsiString() << endl;
        std::exit(1);
    }
    mSprite.setTexture(mTexture);
    mSprite.setOrigin({mRadius, mRadius});
    mSprite.setPosition(mPosition);
}


sf::Color HitNode::getCurrentColor() const
{
    if (mState == State::Unblocked)
        return sUnlockedColor;
    else if (mState == State::Activated)
        return sActivatedColor;
    return sBlockedColor;
}

void HitNode::draw(sf::RenderWindow& window) const
{
    for (const auto& el : mChildren)
    {
        sfLine connectionLine {mPosition, el->getPosition(), getCurrentColor(), 2};
        connectionLine.draw(window);
        el->draw(window);
    }

    static sf::CircleShape shape(mRadius);
    shape.setOrigin({mRadius, mRadius});
    shape.setFillColor(getCurrentColor());
    shape.setPosition(mPosition);
    window.draw(shape);

    window.draw(mSprite);
}

bool HitNode::collisionTest(sf::Vector2f mouseCoords) 
{
    sf::Vector2f d = mPosition - mouseCoords;
    return d.x * d.x + d.y * d.y < mRadius * mRadius;
}

/* BombSkillNode */

BombSkillNode::BombSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String BombSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_bomb.png"};
}


/* SpikesSkillNode */
SpikesSkillNode::SpikesSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String SpikesSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_spikes.png"};
}

/* LightningSkillNode */
LightningSkillNode::LightningSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String LightningSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_lightning.png"};
}

/* EyeSkillNode */

EyeSkillNode::EyeSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String EyeSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_eye.png"};
}

/* ClawsSkillNode */
ClawsSkillNode::ClawsSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String ClawsSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_claws.png"};
}

/* ShieldSkillNode */
ShieldSkillNode::ShieldSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String ShieldSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_shield.png"};
}

/* SwordSkillNode */
SwordSkillNode::SwordSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String SwordSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_sword.png"};
}

/* ShurikenSkillNode */
ShurikenSkillNode::ShurikenSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String ShurikenSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_shuriken.png"};
}

/* WindSkillNode */
WindSkillNode::WindSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String WindSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_shuriken.png"};
}

/* MeteoriteSkillNode */
MeteoriteSkillNode::MeteoriteSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String MeteoriteSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_meteorite.png"};
}

/* HandSkillNode */
HandSkillNode::HandSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String HandSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_hand.png"};
}

/* EarthquakeSkillNode */
EarthquakeSkillNode::EarthquakeSkillNode(sf::Vector2f position) : HitNode{position} 
{
    loadTexture();
}

sf::String EarthquakeSkillNode::getIconPath() 
{
    return sf::String{"icons/icon_earthquake.png"};
}
