#include "MoABoneMapping.h"

FName FMoABoneNameUtils::EnumToFName(EMoABoneName BoneEnum)
{
    switch (BoneEnum)
    {
        case EMoABoneName::Hips: return FName("Hips");
        case EMoABoneName::LeftHip: return FName("LeftHip");
        case EMoABoneName::LeftKnee: return FName("LeftKnee");
        case EMoABoneName::LeftAnkle: return FName("LeftAnkle");
        case EMoABoneName::LeftToe: return FName("LeftToe");
        case EMoABoneName::RightHip: return FName("RightHip");
        case EMoABoneName::RightKnee: return FName("RightKnee");
        case EMoABoneName::RightAnkle: return FName("RightAnkle");
        case EMoABoneName::RightToe: return FName("RightToe");
        case EMoABoneName::Chest: return FName("Chest");
        case EMoABoneName::Chest1: return FName("Chest1");
        case EMoABoneName::Chest2: return FName("Chest2");
        case EMoABoneName::LeftCollar: return FName("LeftCollar");
        case EMoABoneName::LeftShoulder: return FName("LeftShoulder");
        case EMoABoneName::LeftElbow: return FName("LeftElbow");
        case EMoABoneName::LeftWrist: return FName("LeftWrist");
        case EMoABoneName::Neck: return FName("Neck");
        case EMoABoneName::Neck1: return FName("Neck1");
        case EMoABoneName::RightCollar: return FName("RightCollar");
        case EMoABoneName::RightShoulder: return FName("RightShoulder");
        case EMoABoneName::RightElbow: return FName("RightElbow");
        case EMoABoneName::RightWrist: return FName("RightWrist");
        case EMoABoneName::LeftFinger0: return FName("LeftFinger0");
        case EMoABoneName::LeftFinger01: return FName("LeftFinger01");
        case EMoABoneName::LeftFinger02: return FName("LeftFinger02");
        case EMoABoneName::LeftFinger1: return FName("LeftFinger1");
        case EMoABoneName::LeftFinger11: return FName("LeftFinger11");
        case EMoABoneName::LeftFinger12: return FName("LeftFinger12");
        case EMoABoneName::LeftFinger2: return FName("LeftFinger2");
        case EMoABoneName::LeftFinger21: return FName("LeftFinger21");
        case EMoABoneName::LeftFinger22: return FName("LeftFinger22");
        case EMoABoneName::LeftFinger3: return FName("LeftFinger3");
        case EMoABoneName::LeftFinger31: return FName("LeftFinger31");
        case EMoABoneName::LeftFinger32: return FName("LeftFinger32");
        case EMoABoneName::LeftFinger4: return FName("LeftFinger4");
        case EMoABoneName::LeftFinger41: return FName("LeftFinger41");
        case EMoABoneName::LeftFinger42: return FName("LeftFinger42");
        case EMoABoneName::RightFinger0: return FName("RightFinger0");
        case EMoABoneName::RightFinger01: return FName("RightFinger01");
        case EMoABoneName::RightFinger02: return FName("RightFinger02");
        case EMoABoneName::RightFinger1: return FName("RightFinger1");
        case EMoABoneName::RightFinger11: return FName("RightFinger11");
        case EMoABoneName::RightFinger12: return FName("RightFinger12");
        case EMoABoneName::RightFinger2: return FName("RightFinger2");
        case EMoABoneName::RightFinger21: return FName("RightFinger21");
        case EMoABoneName::RightFinger22: return FName("RightFinger22");
        case EMoABoneName::RightFinger3: return FName("RightFinger3");
        case EMoABoneName::RightFinger31: return FName("RightFinger31");
        case EMoABoneName::RightFinger32: return FName("RightFinger32");
        case EMoABoneName::RightFinger4: return FName("RightFinger4");
        case EMoABoneName::RightFinger41: return FName("RightFinger41");
        case EMoABoneName::RightFinger42: return FName("RightFinger42");
        default: return NAME_None;
    }
}

EMoABoneName FMoABoneNameUtils::FNameToEnum(FName BoneName)
{
    if (BoneName == "Hips") return EMoABoneName::Hips;
    if (BoneName == "LeftHip") return EMoABoneName::LeftHip;
    if (BoneName == "LeftKnee") return EMoABoneName::LeftKnee;
    if (BoneName == "LeftAnkle") return EMoABoneName::LeftAnkle;
    if (BoneName == "LeftToe") return EMoABoneName::LeftToe;
    if (BoneName == "RightHip") return EMoABoneName::RightHip;
    if (BoneName == "RightKnee") return EMoABoneName::RightKnee;
    if (BoneName == "RightAnkle") return EMoABoneName::RightAnkle;
    if (BoneName == "RightToe") return EMoABoneName::RightToe;
    if (BoneName == "Chest") return EMoABoneName::Chest;
    if (BoneName == "Chest1") return EMoABoneName::Chest1;
    if (BoneName == "Chest2") return EMoABoneName::Chest2;
    if (BoneName == "LeftCollar") return EMoABoneName::LeftCollar;
    if (BoneName == "LeftShoulder") return EMoABoneName::LeftShoulder;
    if (BoneName == "LeftElbow") return EMoABoneName::LeftElbow;
    if (BoneName == "LeftWrist") return EMoABoneName::LeftWrist;
    if (BoneName == "Neck") return EMoABoneName::Neck;
    if (BoneName == "Neck1") return EMoABoneName::Neck1;
    if (BoneName == "RightCollar") return EMoABoneName::RightCollar;
    if (BoneName == "RightShoulder") return EMoABoneName::RightShoulder;
    if (BoneName == "RightElbow") return EMoABoneName::RightElbow;
    if (BoneName == "RightWrist") return EMoABoneName::RightWrist;
    if (BoneName == "LeftFinger0") return EMoABoneName::LeftFinger0;
    if (BoneName == "LeftFinger01") return EMoABoneName::LeftFinger01;
    if (BoneName == "LeftFinger02") return EMoABoneName::LeftFinger02;
    if (BoneName == "LeftFinger1") return EMoABoneName::LeftFinger1;
    if (BoneName == "LeftFinger11") return EMoABoneName::LeftFinger11;
    if (BoneName == "LeftFinger12") return EMoABoneName::LeftFinger12;
    if (BoneName == "LeftFinger2") return EMoABoneName::LeftFinger2;
    if (BoneName == "LeftFinger21") return EMoABoneName::LeftFinger21;
    if (BoneName == "LeftFinger22") return EMoABoneName::LeftFinger22;
    if (BoneName == "LeftFinger3") return EMoABoneName::LeftFinger3;
    if (BoneName == "LeftFinger31") return EMoABoneName::LeftFinger31;
    if (BoneName == "LeftFinger32") return EMoABoneName::LeftFinger32;
    if (BoneName == "LeftFinger4") return EMoABoneName::LeftFinger4;
    if (BoneName == "LeftFinger41") return EMoABoneName::LeftFinger41;
    if (BoneName == "LeftFinger42") return EMoABoneName::LeftFinger42;
    if (BoneName == "RightFinger0") return EMoABoneName::RightFinger0;
    if (BoneName == "RightFinger01") return EMoABoneName::RightFinger01;
    if (BoneName == "RightFinger02") return EMoABoneName::RightFinger02;
    if (BoneName == "RightFinger1") return EMoABoneName::RightFinger1;
    if (BoneName == "RightFinger11") return EMoABoneName::RightFinger11;
    if (BoneName == "RightFinger12") return EMoABoneName::RightFinger12;
    if (BoneName == "RightFinger2") return EMoABoneName::RightFinger2;
    if (BoneName == "RightFinger21") return EMoABoneName::RightFinger21;
    if (BoneName == "RightFinger22") return EMoABoneName::RightFinger22;
    if (BoneName == "RightFinger3") return EMoABoneName::RightFinger3;
    if (BoneName == "RightFinger31") return EMoABoneName::RightFinger31;
    if (BoneName == "RightFinger32") return EMoABoneName::RightFinger32;
    if (BoneName == "RightFinger4") return EMoABoneName::RightFinger4;
    if (BoneName == "RightFinger41") return EMoABoneName::RightFinger41;
    if (BoneName == "RightFinger42") return EMoABoneName::RightFinger42;
    
    return EMoABoneName::Hips; 
}

TArray<EMoABoneName> FMoABoneNameUtils::GetAllBoneEnums()
{
    TArray<EMoABoneName> Result;
    for (int32 i = 0; i < (int32)EMoABoneName::Count; i++)
    {
        Result.Add((EMoABoneName)i);
    }
    return Result;
}

EMoABoneCategory FMoABoneNameUtils::GetBoneCategory(EMoABoneName BoneEnum)
{
    int32 BoneIndex = (int32)BoneEnum;
    
    // Body & Spine: 0-5 (Hips, Chest, Chest1, Chest2, Neck, Neck1)
    if (BoneIndex >= 0 && BoneIndex <= 5) return EMoABoneCategory::BodyAndSpine;
    
    // Legs: 6-13 (LeftHip, LeftKnee, LeftAnkle, LeftToe, RightHip, RightKnee, RightAnkle, RightToe)
    if (BoneIndex >= 6 && BoneIndex <= 13) return EMoABoneCategory::Legs;
    
    // Arms: 14-21 (LeftCollar, LeftShoulder, LeftElbow, LeftWrist, RightCollar, RightShoulder, RightElbow, RightWrist)
    if (BoneIndex >= 14 && BoneIndex <= 21) return EMoABoneCategory::Arms;
    
    // Fingers: 22-51 (30 finger bones)
    if (BoneIndex >= 22 && BoneIndex <= 51) return EMoABoneCategory::Fingers;
    
    return EMoABoneCategory::BodyAndSpine;
}

FString FMoABoneNameUtils::GetCategoryName(EMoABoneCategory Category)
{
    const UEnum* EnumPtr = StaticEnum<EMoABoneCategory>();
    if (EnumPtr)
    {
        return EnumPtr->GetDisplayNameTextByValue((int64)Category).ToString();
    }
    return TEXT("Unknown");
}
