using System;
using Game.Plugins.App.Sync;
using Game.Plugins.JNGame.Sync.Frame.AstarPath.RVO;
using UnityEngine;

namespace Game.Plugins.App.Game.RVO
{
    public class JNGRVOObstacle : JNGSyncFrameDefault
    {
        
        private JNRVOSimulator Simulator => GetComponentInParent<JNGRVOManager>().Simulator;

        public override void OnSyncLoad()
        {
            base.OnSyncLoad();
            CreateObstacles();
        }

        public void CreateObstacles()
        {
            var boxCollider = this.GetComponent<BoxCollider>();
            Vector3 center = transform.position;  
            Vector3 size = boxCollider.size;
            //
            // var verts = new [] { new Vector3(1, 0, -1), new Vector3(1, 0, 1), new Vector3(-1, 0, 1), new Vector3(-1, 0, -1) };
            // for (int i = 0; i < verts.Length; i++) {
            //     verts[i].Scale(new Vector3(size.x * 0.5f, 0, size.y * 0.5f));
            //     verts[i] += new Vector3(center.x, 0, center.y);
            // }
            //
            var verts = new Vector3[4];
            verts[0] = (new Vector3(size.x, -size.y, size.z) * 0.5f);
            verts[1] = (new Vector3(-size.x, -size.y, size.z) * 0.5f);
            verts[2] = (new Vector3(-size.x, -size.y, -size.z) * 0.5f);
            verts[3] = (new Vector3(size.x, -size.y, -size.z) * 0.5f);
            verts[0].y = 0;
            verts[1].y = 0;
            verts[2].y = 0;
            verts[3].y = 0;
            Simulator.AddObstacle(verts, 1, transform.localToWorldMatrix);
        }


        // private void OnDrawGizmos()
        //          {
        //  
        //              Gizmos.color = new Color(0.615f, 1, 0.06f, false ? 1.0f : 0.7f);
        //              var movementPlane = MovementPlane.XY;
        //              var up = Vector3.up;
        //  
        //              if (gizmoVerts == null || AreGizmosDirty() || _obstacleMode != obstacleMode) {
        //                  _obstacleMode = obstacleMode;
        //  
        //                  if (gizmoVerts == null) gizmoVerts = new List<Vector3[]>();
        //                  else gizmoVerts.Clear();
        //  
        //                  CreateObstacles();
        //              }
        //  
        //              Matrix4x4 m = GetMatrix();
        //  
        //              for (int i = 0; i < gizmoVerts.Count; i++) {
        //                  Vector3[] verts = gizmoVerts[i];
        //                  for (int j = 0, q = verts.Length-1; j < verts.Length; q = j++) {
        //                      Gizmos.DrawLine(m.MultiplyPoint3x4(verts[j]), m.MultiplyPoint3x4(verts[q]));
        //                  }
        //  
        //                  if (selected) {
        //                      for (int j = 0, q = verts.Length-1; j < verts.Length; q = j++) {
        //                          Vector3 a = m.MultiplyPoint3x4(verts[q]);
        //                          Vector3 b = m.MultiplyPoint3x4(verts[j]);
        //  
        //                          if (movementPlane != MovementPlane.XY) {
        //                              Gizmos.DrawLine(a + up*Height, b + up*Height);
        //                              Gizmos.DrawLine(a, a + up*Height);
        //                          }
        //  
        //                          Vector3 avg = (a + b) * 0.5f;
        //                          Vector3 tang = (b - a).normalized;
        //                          if (tang == Vector3.zero) continue;
        //  
        //                          Vector3 normal = Vector3.Cross(up, tang);
        //  
        //                          Gizmos.DrawLine(avg, avg+normal);
        //                          Gizmos.DrawLine(avg+normal, avg+normal*0.5f+tang*0.5f);
        //                          Gizmos.DrawLine(avg+normal, avg+normal*0.5f-tang*0.5f);
        //                      }
        //                  }
        //              }
        //  
        //              gizmoDrawing = false;
        //          }
    }
}