using CounterDrone.Core.Algorithms;
using System;
using System.Collections.Generic;
using System.Text.Json;

namespace CounterDrone.Core.Simulation
{
    public class ControlZoneEntity
    {
        public string Id { get; }
        public string Name { get; }
        public List<Vector3> Vertices { get; }
        public float MinAltitude { get; }
        public float MaxAltitude { get; }

        public struct Vector3 { public float X { get; set; } public float Y { get; set; } public float Z { get; set; } }

        public ControlZoneEntity(string id, string name, string verticesJson, float minAlt, float maxAlt)
        {
            Id = id;
            Name = name;
            MinAltitude = minAlt;
            MaxAltitude = maxAlt;
            Vertices = JsonSerializer.Deserialize<List<Vector3>>(verticesJson) ?? new();
        }

        public bool ContainsPoint(float x, float y, float z)
        {
            if (y < MinAltitude || y > MaxAltitude) return false;

            var vertices2D = new (float X, float Z)[Vertices.Count];
            for (int i = 0; i < Vertices.Count; i++)
                vertices2D[i] = (Vertices[i].X, Vertices[i].Z);

            return Kinematics.PointInPolygon(x, z, vertices2D);
        }
    }
}
