using UnityEngine;
using RootMotion.FinalIK;

[ExecuteInEditMode]
public class VRIKAnimationProperties : MonoBehaviour
{
    public VRIK vrik;  // 전역 변수로 변경

    // 내부 변수들
    [Header("Global")]
    public float ikPositionWeight = 0f;

    [Header("Spine")]
    public float pelvisPositionWeight = 0f;
    public float chestGoalWeight = 0f;
    public float headPositionWeight = 0f;

    [Header("Arms")]
    public float leftArmPositionWeight = 0f;
    public float leftArmRotationWeight = 0f;
    public float rightArmPositionWeight = 0f;
    public float rightArmRotationWeight = 0f;

    [Header("Legs")]
    public float leftLegPositionWeight = 0f;
    public float leftLegRotationWeight = 0f;
    public float rightLegPositionWeight = 0f;
    public float rightLegRotationWeight = 0f;

    [Header("Locomotion")]
    public float locomotionWeight = 0f;

    void Start()
    {
        vrik = GetComponent<VRIK>();
        ApplyAllWeights();
    }

    void Update()
    {
        if (vrik == null) vrik = GetComponent<VRIK>();
        if (vrik == null || vrik.solver == null) return;
        
        ApplyAllWeights();
    }

    // 모든 가중치 적용
    public void ApplyAllWeights()
    {
        if (vrik?.solver == null) return;

        IKPositionWeight = ikPositionWeight;
        PelvisPositionWeight = pelvisPositionWeight;
        ChestGoalWeight = chestGoalWeight;
        HeadPositionWeight = headPositionWeight;
        
        LeftArmPositionWeight = leftArmPositionWeight;
        LeftArmRotationWeight = leftArmRotationWeight;
        RightArmPositionWeight = rightArmPositionWeight;
        RightArmRotationWeight = rightArmRotationWeight;
        
        LeftLegPositionWeight = leftLegPositionWeight;
        LeftLegRotationWeight = leftLegRotationWeight;
        RightLegPositionWeight = rightLegPositionWeight;
        RightLegRotationWeight = rightLegRotationWeight;
        
        LocomotionWeight = locomotionWeight;
    }

    // 모든 가중치 초기화
    public void ResetAllWeights()
    {
        ikPositionWeight = 0f;
        pelvisPositionWeight = 0f;
        chestGoalWeight = 0f;
        headPositionWeight = 0f;
        
        leftArmPositionWeight = 0f;
        leftArmRotationWeight = 0f;
        rightArmPositionWeight = 0f;
        rightArmRotationWeight = 0f;
        
        leftLegPositionWeight = 0f;
        leftLegRotationWeight = 0f;
        rightLegPositionWeight = 0f;
        rightLegRotationWeight = 0f;
        
        locomotionWeight = 0f;

        ApplyAllWeights();
    }

    // VRIK Solver에 직접 접근하는 프로퍼티들
    public float IKPositionWeight
    {
        get => vrik?.solver?.IKPositionWeight ?? 0f;
        set { if (vrik?.solver != null) vrik.solver.IKPositionWeight = value; }
    }

    // Spine
    public float PelvisPositionWeight
    {
        get => vrik?.solver?.spine.pelvisPositionWeight ?? 0f;
        set { if (vrik?.solver?.spine != null) vrik.solver.spine.pelvisPositionWeight = value; }
    }

    public float ChestGoalWeight
    {
        get => vrik?.solver?.spine.chestGoalWeight ?? 0f;
        set { if (vrik?.solver?.spine != null) vrik.solver.spine.chestGoalWeight = value; }
    }

    public float HeadPositionWeight
    {
        get => vrik?.solver?.spine.positionWeight ?? 0f;
        set { if (vrik?.solver?.spine != null) vrik.solver.spine.positionWeight = value; }
    }

    // Left Arm
    public float LeftArmPositionWeight
    {
        get => vrik?.solver?.leftArm.positionWeight ?? 0f;
        set { if (vrik?.solver?.leftArm != null) vrik.solver.leftArm.positionWeight = value; }
    }

    public float LeftArmRotationWeight
    {
        get => vrik?.solver?.leftArm.rotationWeight ?? 0f;
        set { if (vrik?.solver?.leftArm != null) vrik.solver.leftArm.rotationWeight = value; }
    }

    // Right Arm
    public float RightArmPositionWeight
    {
        get => vrik?.solver?.rightArm.positionWeight ?? 0f;
        set { if (vrik?.solver?.rightArm != null) vrik.solver.rightArm.positionWeight = value; }
    }

    public float RightArmRotationWeight
    {
        get => vrik?.solver?.rightArm.rotationWeight ?? 0f;
        set { if (vrik?.solver?.rightArm != null) vrik.solver.rightArm.rotationWeight = value; }
    }

    // Left Leg
    public float LeftLegPositionWeight
    {
        get => vrik?.solver?.leftLeg.positionWeight ?? 0f;
        set { if (vrik?.solver?.leftLeg != null) vrik.solver.leftLeg.positionWeight = value; }
    }

    public float LeftLegRotationWeight
    {
        get => vrik?.solver?.leftLeg.rotationWeight ?? 0f;
        set { if (vrik?.solver?.leftLeg != null) vrik.solver.leftLeg.rotationWeight = value; }
    }

    // Right Leg
    public float RightLegPositionWeight
    {
        get => vrik?.solver?.rightLeg.positionWeight ?? 0f;
        set { if (vrik?.solver?.rightLeg != null) vrik.solver.rightLeg.positionWeight = value; }
    }

    public float RightLegRotationWeight
    {
        get => vrik?.solver?.rightLeg.rotationWeight ?? 0f;
        set { if (vrik?.solver?.rightLeg != null) vrik.solver.rightLeg.rotationWeight = value; }
    }

    // Locomotion
    public float LocomotionWeight
    {
        get => vrik?.solver?.locomotion.weight ?? 0f;
        set { if (vrik?.solver?.locomotion != null) vrik.solver.locomotion.weight = value; }
    }

    // Inspector에서 값이 변경될 때 호출
    void OnValidate()
    {
        if (Application.isPlaying)
        {
            ApplyAllWeights();
        }
    }
}