using System.Collections.Generic;
using DotRecast.Core.Collections;
using JNGame.Math;
using JNGame.Sync.State.Tile;
using UnityEngine;

namespace JNGame.Sync.Debuger
{
    public class JNTileServerDebuger : SingletonScene<JNTileServerDebuger>
    {
        
        private List<JNSSTileServerService> _services = new List<JNSSTileServerService>();
        
        public void Add(JNSSTileServerService service)
        {
            _services.Add(service);
        }

        public void Remove(JNSSTileServerService service)
        {
            _services.Remove(service);
        }

        private void OnDrawGizmos()
        {

            _services.ForEach(DebugService);

        }

        private void DebugService(JNSSTileServerService service)
        {

            if (!(service.IsStart)) return;
            
            //绘制权限区块
            DrawContains(service.MaxContains,service.MinContains,Color.red);
            
            //绘制连接区域
            service.GetLinkTiles().ForEach(index =>
            {
                (LVector2 max, LVector2 min) = service.GetTileContains(index);
                DrawContains(max,min,Color.cyan);
            });

        }

        /// <summary>
        /// 绘制区域
        /// </summary>
        /// <param name="MaxContains"></param>
        /// <param name="MinContains"></param>
        private void DrawContains(LVector2 MaxContains,LVector2 MinContains,Color color)
        {
            // 绘制正方形  
            // 计算正方形的四个角点  
            Vector3 topLeft = new Vector3(MaxContains.x,0, MaxContains.y);  
            Vector3 topRight = new Vector3(MinContains.x,0, MaxContains.y);  
            Vector3 bottomLeft = new Vector3(MaxContains.x,0, MinContains.y);  
            Vector3 bottomRight = new Vector3(MinContains.x,0, MinContains.y);  
  
            // 使用Gizmos.DrawLine来绘制正方形的四条边  
            Gizmos.color = color; // 设置Gizmos的颜色  
  
            // 顶部边  
            Gizmos.DrawLine(topLeft, topRight);  
            // 底部边  
            Gizmos.DrawLine(bottomLeft, bottomRight);  
            // 左侧边  
            Gizmos.DrawLine(topLeft, bottomLeft);  
            // 右侧边  
            Gizmos.DrawLine(topRight, bottomRight);  
        }
        
    }
}