#include "Chr_Controller.h"
#include "Actor/Object/CharacterObject.h"

float Chr_Controller::ConvertAxisValue(Sint16 rawValue)
{
    float value = 0.f;
    // SDL的轴值范围通常是-32768到32767
    if (rawValue > 0)
    {
        value = static_cast<float>(rawValue) / 32767.0f;
    }
    else
    {
        value = static_cast<float>(rawValue) / 32768.0f;
    }
    // 小于0.1的值视为0
    if (fabs(value) < 0.1f)
    {
        value = 0.f;
    }
    // 大于0.95的值视为1
    else if (fabs(value) > 0.95f)
    {
        value = (value > 0) ? 1.f : -1.f;
    }
    return value;
}

void Chr_Controller::Init(CharacterObject *pCharacter)
{
    addTag(Tag::HANDEL_EVENT);
    m_pCharacter = pCharacter;
}

void Chr_Controller::HandleEvents(SDL_Event *e)
{
    // 处理控制器相关事件
    switch (e->type)
    {
    case SDL_CONTROLLERDEVICEADDED:
    {
        SDL_Log("手柄连接 - 设备ID: %d", e->cdevice.which);
        int deviceIndex = e->cdevice.which;
        if (SDL_IsGameController(deviceIndex))
        {
            SDL_GameController *controller = SDL_GameControllerOpen(deviceIndex);
            if (controller)
            {
                SDL_Log("已连接并打开控制器: %s", SDL_GameControllerName(controller));
            }
            else
            {
                SDL_Log("连接了控制器但无法打开! SDL错误: %s", SDL_GetError());
            }
        }
        break;
    }
    case SDL_CONTROLLERDEVICEREMOVED:
    {
        SDL_Log("手柄断开 - 设备ID: %d", e->cdevice.which);
        break;
    }
    case SDL_CONTROLLERDEVICEREMAPPED:
    {
        SDL_Log("手柄重映射 - 设备ID: %d", e->cdevice.which);
        break;
    }
    case SDL_CONTROLLERBUTTONDOWN:
    {
        SDL_Log("手柄按钮按下 - 控制器ID: %d, 按钮: %d",
                e->cbutton.which, e->cbutton.button);
        break;
    }
    case SDL_CONTROLLERBUTTONUP:
    {
        SDL_Log("手柄按钮释放 - 控制器ID: %d, 按钮: %d",
                e->cbutton.which, e->cbutton.button);
        break;
    }
    case SDL_CONTROLLERAXISMOTION:
    {
        // 转换原始轴值到-1到1范围
        float value = ConvertAxisValue(e->caxis.value);
        // 根据不同的轴更新对应的摇杆位置
        switch (e->caxis.axis)
        {
        case SDL_CONTROLLER_AXIS_LEFTX:
            LeftStick.x = value;
            break;
        case SDL_CONTROLLER_AXIS_LEFTY:
            LeftStick.y = value;
            break;
        case SDL_CONTROLLER_AXIS_RIGHTX:
            RightStick.x = value;
            break;
        case SDL_CONTROLLER_AXIS_RIGHTY:
            RightStick.y = value;
            break;
        default:
            break;
        }
        // 如果父对象存在 调用父对象的消息处理
        if (m_pCharacter)
        {
            // 左摇杆变动时
            if (e->caxis.axis == SDL_CONTROLLER_AXIS_LEFTX || e->caxis.axis == SDL_CONTROLLER_AXIS_LEFTY)
            {
                m_pCharacter->ControllerMsg(CONTROLLER_MSG_TYPE::CONTROLLER_MSG_TYPE_LEFT_JOYSTICK_MOVE, &LeftStick);
            }
            // 右摇杆变动时
            else if (e->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTX || e->caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
            {
                m_pCharacter->ControllerMsg(CONTROLLER_MSG_TYPE::CONTROLLER_MSG_TYPE_RIGHT_JOYSTICK_MOVE, &RightStick);
            }
        }
        break;
    }
    case SDL_KEYDOWN:
    {
        switch (e->key.keysym.sym)
        {
        case SDLK_UP: // 上方向键
            LeftStick.y = -1.0f;
            break;
        case SDLK_DOWN: // 下方向键
            LeftStick.y = 1.0f;
            break;
        case SDLK_LEFT: // 左方向键
            LeftStick.x = -1.0f;
            break;
        case SDLK_RIGHT: // 右方向键
            LeftStick.x = 1.0f;
            break;
        default:
            break;
        }
        // 如果父对象存在 调用父对象的消息处理
        if (m_pCharacter)
        {
            m_pCharacter->ControllerMsg(CONTROLLER_MSG_TYPE::CONTROLLER_MSG_TYPE_LEFT_JOYSTICK_MOVE, &LeftStick);
        }
        break;
    }
    case SDL_KEYUP:
    {
        switch (e->key.keysym.sym)
        {
        case SDLK_UP: // 上方向键
        {
            if (LeftStick.y < 0.f)
                LeftStick.y = 0.f;
            break;
        }
        case SDLK_DOWN: // 下方向键
        {
            if (LeftStick.y > 0.f)
                LeftStick.y = 0.f;
            break;
        }
        case SDLK_LEFT: // 左方向键
        {
            if (LeftStick.x < 0.f)
                LeftStick.x = 0.f;
            break;
        }
        case SDLK_RIGHT: // 右方向键
        {
            if (LeftStick.x > 0.f)
                LeftStick.x = 0.f;
            break;
        }
        default:
            break;
        }
        // 如果父对象存在 调用父对象的消息处理
        if (m_pCharacter)
        {
            m_pCharacter->ControllerMsg(CONTROLLER_MSG_TYPE::CONTROLLER_MSG_TYPE_LEFT_JOYSTICK_MOVE, &LeftStick);
        }
        break;
    }

    default:
        // 可以处理其他类型的事件或忽略
        break;
    }
}
