using UnityEngine;
using IStalk.Data;

namespace IStalk.EditorTools.MapGen
{
    public class ConnectorMarker : MonoBehaviour
    {
        public ConnectionTag connectionDirection;
        [Range(0.5f, 3f)]
        public float connectorWidth = 2f;
        [Range(2f, 4f)]
        public float connectorHeight = 3f;

        private void OnDrawGizmos()
        {
            Gizmos.color = GetDirectionColor();
            Vector3 size = new(connectorWidth, connectorHeight, 0.2f);

            Quaternion rot = connectionDirection switch
            {
                ConnectionTag.Left => Quaternion.Euler(0, 90, 0),
                ConnectionTag.Right => Quaternion.Euler(0, -90, 0),
                ConnectionTag.Up => Quaternion.identity,
                ConnectionTag.Down => Quaternion.Euler(0, 180, 0),
                _ => Quaternion.identity
            };

            Matrix4x4 oldMatrix = Gizmos.matrix;
            Gizmos.matrix = Matrix4x4.TRS(transform.position, rot, Vector3.one);
            Gizmos.DrawWireCube(Vector3.zero, size);
            Gizmos.DrawLine(Vector3.zero, Vector3.forward * 1.5f);
            Gizmos.matrix = oldMatrix;
        }

        private void OnDrawGizmosSelected()
        {
            Gizmos.color = GetDirectionColor() * 1.5f;
            Gizmos.DrawSphere(transform.position, 0.3f);
        }

        private Color GetDirectionColor() => connectionDirection switch
        {
            ConnectionTag.Left => Color.red,
            ConnectionTag.Right => Color.blue,
            ConnectionTag.Up => Color.green,
            ConnectionTag.Down => Color.yellow,
            _ => Color.white
        };
    }
}
