using PhilExampleCrawler.Common.TCP.Packets;
using PhilExampleCrawler.Core.Abstractions.Interfaces;
using PhilExampleCrawler.TCPAPI;
using PhilExampleCrawler.TCPAPI.Services;


Console.WriteLine("PhilExample Server lacht :)");
Console.WriteLine("Initializing Server...");

Console.WriteLine("Server initialized.");
Console.WriteLine("What should I do?");

int i = 1;
bool started = false;

while (true)
{
    string? cmd = Console.ReadLine();

    if (cmd == "start_crw" || string.IsNullOrEmpty(cmd))
    {
        if (!started)
        {
            started = true;
            Crawler.Start();
        }
    }
    else if (cmd == "stop_crw")
    {
        Crawler.Stop();
    }
    else if (cmd == "get_sessions")
    {
        foreach (var sess in UserService.RunningSessions)
        {
            Console.WriteLine("ConnectionID: " + sess.Key);
            Console.WriteLine("....SessionID: " + sess.Value.ID);
            Console.WriteLine("....User:");
            Console.WriteLine("............ID: " + sess.Value.User.ID);
            Console.WriteLine("............AuthCode: " + sess.Value.User.AuthCode);
            Console.WriteLine("............CreateDate: " + sess.Value.User.CreateDate.ToString("dd:MM:yy HH:mm:ss"));
            foreach (var crawlReq in sess.Value.RegisteredCrawlSessions)
            {
                Console.WriteLine("....CrawlRequest " + crawlReq.ID + ":");
                Console.WriteLine("............keywords: " + crawlReq.SearchParams.KeyWords);
                Console.WriteLine("............locationStr: " + crawlReq.SearchParams.LocationStr);
                Console.WriteLine("............categoryID: " + crawlReq.SearchParams.CategoryID);
                Console.WriteLine("............radius: " + crawlReq.SearchParams.Radius);
            }
        }
    }
    else if(cmd.StartsWith("ping"))
    {
        int connectionID = int.Parse(cmd[4..]);
        Program.TCPServer.Send(connectionID, new BasePacket(new NewInsertionLoad() { Href = "test/href/ping/hihihi" }));
    }
}
public partial class Program
{
    internal static PhilExampleCrawler.DataBase.UserAccess DB_UserAcces { get; } = new(new PhilExampleCrawler.DataBase.Models.DBSettings());
    internal static PhilExampleCrawler.DataBase.CrawlSessionAccess DB_CrawlSessionAccess { get; } = new(new PhilExampleCrawler.DataBase.Models.DBSettings());
    internal static TCPServer TCPServer { get; } = new();
    internal static CrawlingService Crawler { get; } = new();
    internal static UserSessionService UserService { get; } = new();
}