using System;
using UnityEngine;

namespace Woop.Unity
{
    [Serializable]
    public struct WoopRigRoles
    {
        public string schemaVersion;
        public string head;
        public string leftUpperArm;
        public string leftLowerArm;
        public string leftHand;
        public string rightUpperArm;
        public string rightLowerArm;
        public string rightHand;
        public string upperTorso;
        public string lowerTorso;
        public string leftLeg;
        public string rightLeg;

        public bool HasMappedLimbs =>
            !string.IsNullOrEmpty(leftUpperArm)
            || !string.IsNullOrEmpty(rightUpperArm)
            || !string.IsNullOrEmpty(leftLeg)
            || !string.IsNullOrEmpty(rightLeg);
    }

    [DisallowMultipleComponent]
    public sealed class WoopRigMetadata : MonoBehaviour
    {
        [Serializable]
        public struct PartInfo
        {
            public string id;
            public int sourceWidth;
            public int sourceHeight;
            public int zIndex;
            public Transform sceneTransform;
        }

        [Serializable]
        public struct JointInfo
        {
            public string id;
            public string parentPartId;
            public string parentAnchorId;
            public string childPartId;
            public string childAnchorId;
        }

        [SerializeField] private string schemaVersion;
        [SerializeField] private string projectId;
        [SerializeField] private Vector2Int sourceSheetSize;
        [SerializeField] private PartInfo[] parts = Array.Empty<PartInfo>();
        [SerializeField] private JointInfo[] joints = Array.Empty<JointInfo>();
        [SerializeField] private WoopRigRoles rigRoles;

        public string SchemaVersion => schemaVersion;
        public string ProjectId => projectId;
        public Vector2Int SourceSheetSize => sourceSheetSize;
        public PartInfo[] Parts => parts;
        public JointInfo[] Joints => joints;
        public WoopRigRoles RigRoles => rigRoles;

        public void Configure(
            string importedSchemaVersion,
            string importedProjectId,
            Vector2Int importedSheetSize,
            PartInfo[] importedParts,
            JointInfo[] importedJoints,
            WoopRigRoles importedRigRoles)
        {
            schemaVersion = importedSchemaVersion;
            projectId = importedProjectId;
            sourceSheetSize = importedSheetSize;
            parts = importedParts ?? Array.Empty<PartInfo>();
            joints = importedJoints ?? Array.Empty<JointInfo>();
            rigRoles = importedRigRoles;
        }
    }
}
