/*
 * KartInteractionComponent.cpp
 *
 *  Created on: Aug 19, 2020
 *      Author: fredrick
 */

#include "KartInteractionComponent.h"

namespace JamSpook {

KartInteractionComponent::KartInteractionComponent(mat4 transform,
                                                   shared_ptr<BroadcastObservable<shared_ptr<Message> > > entity,
                                                   weak_ptr<InteractionSystem> interactionSystem):
        InteractionComponent(transform, entity, interactionSystem),
        mOldDirection(vec3(0)),
        mIsOnGround(false)
{}

KartInteractionComponent::~KartInteractionComponent()
{}

void KartInteractionComponent::onInputDeviceStateChange(const InputDeviceState inputDeviceState)
{
    Log::write(LogLevel::debug, "KartInteractionComponent - onInputDeviceStateChange");

    vec3 accumulatedDirection = vec3(0);

    // Handle controls
    if (inputDeviceState.left == ButtonState::pressed || inputDeviceState.left == ButtonState::held)
    {
        accumulatedDirection += vec3(-1.0f, 0, 0);
    }
    else if (inputDeviceState.left == ButtonState::released)
    {
        accumulatedDirection -= vec3(-1.0f, 0, 0);
    }

    if (inputDeviceState.right == ButtonState::pressed || inputDeviceState.right == ButtonState::held)
    {
        accumulatedDirection += vec3(1.0f, 0, 0);
    }
    else if (inputDeviceState.right == ButtonState::released)
    {
        accumulatedDirection -= vec3(1.0f, 0, 0);
    }

    if (inputDeviceState.up == ButtonState::pressed || inputDeviceState.up == ButtonState::held)
    {
        accumulatedDirection += vec3(0, 0, 1.0f);
    }
    else if (inputDeviceState.up == ButtonState::released)
    {
        accumulatedDirection -= vec3(0, 0, 1.0f);
    }

    if (inputDeviceState.down == ButtonState::pressed || inputDeviceState.down == ButtonState::held)
    {
        accumulatedDirection += vec3(0, 0, -1.0f);
    }
    else if (inputDeviceState.down == ButtonState::released)
    {
        accumulatedDirection -= vec3(0, 0, -1.0f);
    }


    if (inputDeviceState.action == ButtonState::pressed)
    {
        // if on ground (can jump)
        if (mIsOnGround)
        {
            // Send jump message, containing current position
            ICCBroadcast(dynamic_pointer_cast<Message>(
                    make_shared<JumpMessage>(getTranslation(mTransform))));
        }
    }

    if (inputDeviceState.up == ButtonState::neutral &&
        inputDeviceState.down == ButtonState::neutral &&
        inputDeviceState.left == ButtonState::neutral &&
        inputDeviceState.right == ButtonState::neutral)
    {
        accumulatedDirection = vec3(0);
    }

    if (accumulatedDirection != mOldDirection)
    {
            ICCBroadcast(dynamic_pointer_cast<Message>(
                    make_shared<messages::DirectionChangeMessage>(
                            getTranslation(mTransform),
                            mOldDirection,
                            accumulatedDirection)));
    }
    mOldDirection = accumulatedDirection;
}

void KartInteractionComponent::onICCMessage(shared_ptr<Message> message)
{
    InteractionComponent::onICCMessage(message);

    // Jump message, make the kart jump
    if (message->getMessageType() == IDCache::get("GroundCollisionMessage"))
    {
        Log::write(LogLevel::debug, "KartInteractionComponent - onICCMessage - GroundCollisionMessage");
        mIsOnGround = true;
    }
    if (message->getMessageType() == IDCache::get("GroundSeparationMessage"))
    {
        Log::write(LogLevel::debug, "KartInteractionComponent - onICCMessage - GroundSeparationMessage");
        mIsOnGround = false;
    }
}

} // namespace JamSpook
