#include "PlayerKam.h"

#include <Globals.h>

#include "Game.h"
#include "Globals.h"

PlayerKam::PlayerKam(Framework::Bildschirm3D* zScreen)
    : Kam3D()
{
    kameraControll = 0;
    setBildschirmPosition(0, 0);
    setBildschirmSize(zScreen->getBackBufferSize());
    setStyle(
        Kam3D::Style::Tick | Kam3D::Style::Movable | Kam3D::Style::Rotatable);
    setRotation({(float)PI / 2.f, 0, 0});
    entityId = -1;
}

void PlayerKam::setDirection(Framework::Vec3<float> direction)
{
    if (direction.getLengthSq() > 0)
    {
        float rotZ = std::atan2(direction.y, direction.x) + (float)PI / 2;
        setRotation({getRotation().x, getRotation().y, rotZ});
    }
}

void PlayerKam::doTastaturEreignis(Framework::TastaturEreignis& te)
{
    if (te.id == TE_Press)
    {
        if (te.taste[0] >= '0' && te.taste[0] <= '9')
        {
            char action[5];
            action[0] = 3;
            *(int*)(action + 1) = te.taste[0] - '1';
            if (*(int*)(action + 1) < 0) *(int*)(action + 1) = 9;
            World::INSTANCE->zClient()->sendPlayerAction(action, 5);
        }
    }
    if (te.id == TE_Release)
    {
        if (te.virtualKey == T_Esc)
        {
            bool oldControl = kameraControll;
            kameraControll = 0;
            setShowCursor(true);
            if (!oldControl)
                ((Game*)(Menu*)menuRegister->get("game"))->closeCurrentDialog();
        }
        if (te.virtualKey == T_Tab)
        {
            char action = 4;
            World::INSTANCE->zClient()->sendPlayerAction(&action, 1);
        }
        if (te.taste[0] == 'm')
        {
            ((Game*)(Menu*)menuRegister->get("game"))->zMap()->setVisibility(1);
        }
    }
}

void PlayerKam::doMausEreignis(Framework::MausEreignis& me)
{
    if (me.verarbeitet && me.id != ME_RLinks && me.id != ME_RRechts)
    {
        kameraControll = 0;
        setShowCursor(true);
    }
    else
    {
        if (!kameraControll && me.id != ME_RLinks && me.id != ME_RRechts)
        {
            if (me.id == ME_PLinks) setControlEnabled(1);
        }
        else
        {
            if (kameraControll || me.id == ME_RLinks || me.id == ME_RRechts)
            {
                if (me.id == ME_PLinks)
                {
                    char action[2] = {1, 8};
                    World::INSTANCE->zClient()->sendPlayerAction(action, 2);
                }
                if (me.id == ME_RLinks)
                {
                    char action[2] = {0, 8};
                    World::INSTANCE->zClient()->sendPlayerAction(action, 2);
                }
                if (me.id == ME_PRechts)
                {
                    char action[2] = {1, 9};
                    World::INSTANCE->zClient()->sendPlayerAction(action, 2);
                }
                if (me.id == ME_RRechts)
                {
                    char action[2] = {0, 9};
                    World::INSTANCE->zClient()->sendPlayerAction(action, 2);
                }
            }
        }
        me.verarbeitet = 1;
    }
}

bool PlayerKam::tick(double time)
{
    __int64 style = 0;
    if (hatStyle(Style::Movable)) style |= Style::Movable;
    if (hatStyle(Style::Rotatable)) style |= Style::Rotatable;
    if (hatStyle(Style::Zoomable)) style |= Style::Zoomable;
    removeStyle(Style::Movable | Style::Rotatable | Style::Zoomable);
    bool result = Kam3D::tick(time);
    addStyle(style);
    if (kameraControll)
    {
        Punkt dir
            = window->getGr��e() / 2 - (getMausPos() - window->getPosition());
        setRotation(
            {min(max(getRotation().x - dir.y * (float)time * 0.2f, 0.1f), 2.5f),
                getRotation().y,
                getRotation().z - dir.x * (float)time * 0.2f});
        if (getRotation().z > 2 * PI)
            setRotation({getRotation().x,
                getRotation().y,
                getRotation().z - 2.f * (float)PI});
        if (getRotation().z < -2 * PI)
            setRotation({getRotation().x,
                getRotation().y,
                getRotation().z + 2.f * (float)PI});
        SetCursorPos(window->getPosition().x + window->getK�rperGr��e().x / 2,
            window->getPosition().y + window->getK�rperGr��e().y / 2);
        setShowCursor(false);
        setMausPos(window->getPosition() + window->getGr��e() / 2);
    }
    return 1;
}

void PlayerKam::setEntityId(int id)
{
    entityId = id;
}

void PlayerKam::setControlEnabled(bool enabled)
{
    kameraControll = enabled;
    setShowCursor(!kameraControll);
    if (kameraControll)
    {
        SetCursorPos(window->getPosition().x + window->getK�rperGr��e().x / 2,
            window->getPosition().y + window->getK�rperGr��e().y / 2);
    }
}

int PlayerKam::getEntityId() const
{
    return entityId;
}

Framework::Vec3<float> PlayerKam::getDirection() const
{
    return getWorldDirection(getScreenPos() + getScreenSize() / 2);
}