using System;
using System.IO;
using System.Threading.Tasks;
using Monoculus;
using NAudio.Wave;

namespace sharpFRN
{
    class Program
    {
        static WebSocketAudioStreamer audioStreamer = new WebSocketAudioStreamer();
        static Settings settings = new Settings();

        static async Task Main(string[] args)
        {
            settings = SettingsLoader.Load("settings.json");

            audioStreamer.StartWebSocketServerAsync(settings.WebSocketHost);

            // Initialize the FRN client
            var client = new FRNClient(
                host: settings.FRNClient.Host,
                port: settings.FRNClient.Port,
                callSign: settings.FRNClient.CallSign,
                name: settings.FRNClient.Name,
                email: settings.FRNClient.Email,
                password: settings.FRNClient.Password,
                network: settings.FRNClient.Network,
                country: settings.FRNClient.Country,
                city: settings.FRNClient.City,
                locator: settings.FRNClient.Locator,
                type: settings.FRNClient.Type,
                description: settings.FRNClient.Description
            );

            client.OnConnect += () => Console.WriteLine("Connected to the server.");
            client.OnDisconnect += () => Console.WriteLine("Disconnected from the server.");
            client.OnLogin += () => Console.WriteLine("Logged in successfully.");
            client.OnMessageReceived += (message) => Console.WriteLine($"Message received: {message}");
            client.OnClientListReceived += (clients) =>
            {
                Console.WriteLine("Client list updated:");
                foreach (var c in clients)
                {
                    Console.WriteLine($"- {c.CallsignAndUser} ({c.ID})");
                }
            };
            client.OnVoiceDataReceived += (voiceData, clientIndex) =>
            {
                Console.WriteLine($"Voice data received at index {clientIndex}");

                // Decode and play the voice data chunk
                //PlayGSMChunk(voiceData);
                audioStreamer.PlayGSMChunk(voiceData);
            };

            await client.RunAsync();
        }
    }
}
