using System;
using System.Drawing;

namespace AGVNavigationCore.PathFinding.Core
{
    /// <summary>
    /// A* 알고리즘에서 사용하는 경로 노드
    /// </summary>
    public class PathNode
    {
        /// <summary>
        /// 노드 ID
        /// </summary>
        public string NodeId { get; set; }

        /// <summary>
        /// 노드 위치
        /// </summary>
        public Point Position { get; set; }

        /// <summary>
        /// 시작점으로부터의 실제 거리 (G cost)
        /// </summary>
        public float GCost { get; set; }

        /// <summary>
        /// 목적지까지의 추정 거리 (H cost - 휴리스틱)
        /// </summary>
        public float HCost { get; set; }

        /// <summary>
        /// 총 비용 (F cost = G cost + H cost)
        /// </summary>
        public float FCost => GCost + HCost;

        /// <summary>
        /// 부모 노드 (경로 추적용)
        /// </summary>
        public PathNode Parent { get; set; }

        /// <summary>
        /// 연결된 노드 ID 목록
        /// </summary>
        public System.Collections.Generic.List<string> ConnectedNodes { get; set; }

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="nodeId">노드 ID</param>
        /// <param name="position">위치</param>
        public PathNode(string nodeId, Point position)
        {
            NodeId = nodeId;
            Position = position;
            GCost = 0;
            HCost = 0;
            Parent = null;
            ConnectedNodes = new System.Collections.Generic.List<string>();
        }

        /// <summary>
        /// 다른 노드까지의 유클리드 거리 계산
        /// </summary>
        /// <param name="other">대상 노드</param>
        /// <returns>거리</returns>
        public float DistanceTo(PathNode other)
        {
            float dx = Position.X - other.Position.X;
            float dy = Position.Y - other.Position.Y;
            return (float)Math.Sqrt(dx * dx + dy * dy);
        }

        /// <summary>
        /// 문자열 표현
        /// </summary>
        public override string ToString()
        {
            return $"{NodeId} - F:{FCost:F1} G:{GCost:F1} H:{HCost:F1}";
        }

        /// <summary>
        /// 같음 비교 (NodeId 기준)
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj is PathNode other)
            {
                return NodeId == other.NodeId;
            }
            return false;
        }

        /// <summary>
        /// 해시코드 (NodeId 기준)
        /// </summary>
        public override int GetHashCode()
        {
            return NodeId?.GetHashCode() ?? 0;
        }
    }
}