using System.Net;
using System.Net.Sockets;

namespace VRCX
{
    public partial class AppApi
    {
        /// <summary>
        /// Displays an XSOverlay notification with the specified title, content, and optional image.
        /// </summary>
        /// <param name="title">The title of the notification.</param>
        /// <param name="content">The content of the notification.</param>
        /// <param name="timeout">The duration of the notification in milliseconds.</param>
        /// <param name="image">The optional image to display in the notification.</param>
        public void XSNotification(string title, string content, int timeout, string image = "")
        {
            var broadcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            var endPoint = new IPEndPoint(IPAddress.Loopback, 42069);
            var useBase64Icon = false;
            var icon = image;
            if (string.IsNullOrEmpty(image))
            {
                useBase64Icon = true;
                icon = "[encoded data, 4424 characters]";
            }
            
            var height = 110f;
            if (content.Length > 300)
                height = 250f;
            else if (content.Length > 200)
                height = 200f;
            else if (content.Length > 100)
                height = 150f;
            
            var msg = new XSOMessage
            {
                messageType = 1,
                title = title,
                content = content,
                height = height,
                sourceApp = "VRCX",
                timeout = timeout,
                audioPath = string.Empty,
                useBase64Icon = useBase64Icon,
                icon = icon
            };
            
            var byteBuffer = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(msg);
            broadcastSocket.SendTo(byteBuffer, endPoint);
            broadcastSocket.Close();
        }
        
        private struct XSOMessage
        {
            public int messageType { get; set; }
            public int index { get; set; }
            public float volume { get; set; }
            public string audioPath { get; set; }
            public float timeout { get; set; }
            public string title { get; set; }
            public string content { get; set; }
            public string icon { get; set; }
            public float height { get; set; }
            public float opacity { get; set; }
            public bool useBase64Icon { get; set; }
            public string sourceApp { get; set; }
        }
    }
}