using System;
using System.Collections.Generic;
using System.Diagnostics;
using Example.Models;
using Example.Models.StaticData;
using Example.Helpers;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Color = System.Drawing.Color;

namespace Example.Connectors
{
    public class CheckInfo
    {
        public static ServerInfoModel BattlemetricsInfo { get; set; }
        private static FileSystemWatcher fileWatcher;
        private static string _scumLogsPath;
        public static void ConfirmGameOpen()
        {
            switch (UserPCSettings.gameDetection)
            {
                case true:
                    {
                    }
                    break;
            }
        }

        public async static Task ConfirmServerWhaleyServerConnection()
        {
            if (UserPCSettings.gameDetection)
            {
                switch (UserPCSettings.serverConnection)
                {
                    case false:
                        {
                            if (CreateCategoryLists.CreatedCategoryList.Count != 0)
                            {
                                CreateCategoryLists.CreatedCategoryList.Clear();
                                CreateTaxiList._TaxiList.Clear();
                                CreateSquad.SquadMembersInfo.Clear();
                            }
                        }
                        break;

                    case true:
                        {
                            if (GetLogedInServer._currentServerLoggedOn.status == false)
                            {
                                Form1Module.OverlayCanOpen = "Off";
                                Form1Module.OverlayCanOpenForecolor = Color.Red;
                                Form1Module.OverlayCanOpenBackcolor = Color.DarkGray;
                                Thread.Sleep(1000);
                                await GetLogedInServer.LoadServerData(UserPCSettings.activeSteamID);
                            }
                            else
                            {
                                var serverChanged = await IfServerAsChanged();
                                if (serverChanged == true)
                                {
                                    await GetPlayerInfo.LoadPlayerData(UserPCSettings.activeSteamID, GetLogedInServer._currentServerLoggedOn.serverID);
                                    await ConfirmPlayerIsLinked();

                                }
                                else
                                {
                                    if (GetLogedInServer._currentServerLoggedOn != null)
                                    {
                                        if (GetLogedInServer._currentServerLoggedOn.serverID != null || GetLogedInServer._currentServerLoggedOn.serverID != string.Empty)
                                        {
                                            await GetPlayerInfo.LoadPlayerData(UserPCSettings.activeSteamID, GetLogedInServer._currentServerLoggedOn.serverID);

                                            await ConfirmPlayerIsLinked();
                                        }
                                    }
                                    else
                                    {
                                        Thread.Sleep(10000);
                                        await IfServerAsChanged();
                                    }
                                }
                            }
                        }
                        break;
                }
            }


        }

        public async static Task ConfirmPlayerIsLinked()
        {
            if (UserPCSettings.serverConnection)
            {
                switch (GetLogedInServer._currentServerLoggedOn.status)
                {
                    case false:
                        {
                            Form1Module.WarningMsg = "Info: Unable to load Information please login into a WhalleyBot supported server!";
                            Form1Module.OverlayCanOpen = "Off";
                            Form1Module.OverlayCanOpenForecolor = Color.Red;
                            Form1Module.OverlayCanOpenBackcolor = Color.DarkGray;
                            Form1Module.OverlayCanOpenCheckBox = false;
                            Form1Module.CurrentServer = "0";
                            Thread.Sleep(1000);
                            CreateCategoryLists.CreatedCategoryList.Clear();
                            CreateTaxiList._TaxiList.Clear();
                            CreateSquad.SquadMembersInfo.Clear();
                            await GetLogedInServer.LoadServerData(UserPCSettings.activeSteamID);
                        }
                        break;

                    case true:
                        {
                            if (GetPlayerInfo.PlayerDetails.isLinked)
                            {

                                if (CreateCategoryLists.CreatedCategoryList.Count == 0)
                                {

                                    await CreateCategoryLists.GetPacksList();
                                    await CreateTaxiList.GetTaxiList();
                                    Thread.Sleep(100);
                                    Form1Module.OverlayCanOpenCheckBox = true;
                                }
                                await CreateSquad.GetSquadmembers();

                            }
                            else
                            {
                                Form1Module.WarningMsg = "INFO - Please Visit Server's Discord to request your WelcomePack! Thank you.";
                                Form1Module.OverlayCanOpen = "Off";
                                Form1Module.OverlayCanOpenBackcolor = Color.DarkGray;
                                Form1Module.OverlayCanOpenForecolor = Color.Red;
                                Form1Module.OverlayCanOpenCheckBox = false;
                                ConfirmServerWhaleyServerConnection();
                            }
                        }
                        break;
                }
            }
        }
        public async static Task<bool> IfServerAsChanged()
        {
            switch (Form1Module.CurrentServer != GetLogedInServer._currentServerLoggedOn.serverID)
            {
                case false:
                    {
                        if (UserPCSettings.serverConnection)
                        {
                            Thread.Sleep(10000);
                            await GetLogedInServer.LoadServerData(UserPCSettings.activeSteamID);
                        }
                        return false;
                    }

                case true:
                    {
                        Form1Module.OverlayCanOpen = "Loading...";
                        Form1Module.OverlayCanOpenBackcolor = Color.DarkGray;
                        Form1Module.OverlayCanOpenForecolor = Color.Yellow;
                        if (CreateCategoryLists.CreatedCategoryList.Count != 0)
                        {
                            CreateCategoryLists.CreatedCategoryList.Clear();
                            CreateTaxiList._TaxiList.Clear();
                            CreateSquad.SquadMembersInfo.Clear();
                        }
                        Thread.Sleep(10000);
                        await GetLogedInServer.LoadServerData(UserPCSettings.activeSteamID);
                        Form1Module.CurrentServer = GetLogedInServer._currentServerLoggedOn.serverID;
                        return true;
                    }
            }
            return false;
        }

        public async static Task GetBattlemetricData()
        {
            try
            {
                var CurrentLoggedServer = await GetLogedInServer.SearchServer(UserPCSettings.joinedGameIP + ":" + UserPCSettings.joinedGamePort);
                List<ServerInfoModel> logged = CurrentLoggedServer.data.Where(x => x.attributes.ip == UserPCSettings.joinedGameIP && x.attributes.port == UserPCSettings.joinedGamePort).ToList();
                BattlemetricsInfo = logged.First();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Source + ex.Message + ex.Data + ex.StackTrace);
            }

        }




        // Initialize the FileSystemWatcher in your setup or initialization code
        public static void InitializeReadingScumLogFile()
        {

            //fileCreatedDate = File.GetCreationTime(UserPCSettings.gameLogsPath);

            //if (DateTime.Now.AddMinutes(1) > fileCreatedDate)
            //{
            // Add your logic to read the newly added lines in the log file here
            using (var file = File.Open(UserPCSettings.gameLogsPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                if (UserPCSettings._scumLogPathPosition == file.Length)
                    return;
                if (UserPCSettings._scumLogPathPosition > file.Length)
                {
                    UserPCSettings._scumLogPathPosition = 0;
                    return;
                }


                file.Position = UserPCSettings._scumLogPathPosition;
                //using (var reader = new StreamReader(file))
                //{
                //    string line;
                //    while ((line = reader.ReadLine()) != null)
                //    {
                //        if (!string.IsNullOrEmpty(line))
                //        {
                //            if (line.Contains("[LogEOSAnalytics] Start Session"))
                //            {
                //                Form1Module.lbl_Main_GameOpen = "Game Loading";
                //                UserPCSettings.serverConnection = false;
                //                Form1Module.lbl_Main_ServerConnection = "Waiting...";
                //            }
                //            else if (line.Contains("LogEOS: [LogEOSMessaging] Succesfully connected to Stomp."))
                //            {
                //                Form1Module.lbl_Main_GameOpen = "At main menu";
                //                UserPCSettings._scumAtMainMenu = true;
                //                Form1Module.lbl_Main_ServerConnection = "Waiting...";
                //            }
                //            else if (line.Contains("LogSCUM: Starting multiplayer game on "))
                //            {
                //                try
                //                {
                //                    var GetServerIp = line.Split("LogSCUM: ")[1].Replace("Starting multiplayer game on ", "").Replace(" with response port ", "|").Split(":");
                //                    UserPCSettings.joinedGameIP = GetServerIp[0];
                //                    UserPCSettings.joinedGamePort = GetServerIp[1].Split("|")[1];
                //                    UserPCSettings._scumAtMainMenu = false;
                //                    Form1Module.lbl_Main_ServerConnection = "Connecting";

                //                    Form1Module.lbl_Main_GameOpen = "Online";
                //                }
                //                catch (Exception ex)
                //                {
                //                    Console.WriteLine(ex.Message);
                //                }

                //            }
                //            else if (line.Contains("LogSCUM: APrisoner::HandlePossessedBy:"))
                //            {
                //                UserPCSettings.serverConnection = true;
                //                SetThreadStatus.dochecks = 2;
                //                Form1Module.ShowInventoryOverlayButton = 2;
                //                Form1Module.lbl_Main_ServerConnection = "Connected";

                //                Form1Module.lbl_Main_GameOpen = "Online";
                //            }
                //            else if (line.Contains("LogNet: UNetConnection::Close: [UNetConnection] RemoteAddr:"))
                //            {
                //                UserPCSettings.serverConnection = false;
                //                Form1Module.lbl_Main_ServerConnection = "Waiting...";
                //                Form1Module.lbl_Main_GameOpen = "At main menu";
                //            }
                //            else if (line.Contains("LogViewport: Display: Viewport MouseLockMode Changed, LockOnCapture -> LockAlways")/*&& line.Contains("LogViewport: Display: Viewport MouseCaptureMode Changed, CapturePermanently->CaptureDuringMouseDown")*/)
                //            {
                //                try
                //                {
                //                    var showoverlayButton = line.Split("][")[1].Replace("]LogViewport: Display: Viewport MouseCaptureMode Changed, CapturePermanently -> CaptureDuringMouseDown", "");
                //                    if (Form1Module.num != showoverlayButton)
                //                    {
                //                        Form1Module.ShowInventoryOverlayButton = 1;
                //                    }
                //                    Form1Module.num = showoverlayButton;
                //                }
                //                catch (Exception ex)
                //                {
                //                    Debug.WriteLine(ex.Message);
                //                }

                //            }
                //            else if (line.Contains("LogViewport: Display: Viewport MouseLockMode Changed, LockAlways -> LockOnCapture")/*&& line.Contains("LogViewport: Display: Viewport MouseCaptureMode Changed, NoCapture -> CapturePermanently")*/)
                //            {
                //                try
                //                {
                //                    var showoverlayButton = line.Split("][")[1].Replace("]LogViewport: Display: Viewport MouseLockMode Changed, LockAlways -> LockOnCapture", "");
                //                    if (Form1Module.num != showoverlayButton)
                //                    {
                //                        Form1Module.ShowInventoryOverlayButton = 0;
                //                    }
                //                    Form1Module.num = showoverlayButton;
                //                }
                //                catch (Exception ex)
                //                {
                //                    Debug.WriteLine(ex.Message);

                //                }
                //            }
                //            else if (line.Contains("LogViewport: Display: Viewport MouseLockMode Changed, LockOnCapture -> DoNotLock")/*&& line.Contains("LogViewport: Display: Viewport MouseCaptureMode Changed, NoCapture -> CapturePermanently")*/)
                //            {
                //                try
                //                {
                //                    var showoverlayButton = line.Split("][")[1].Replace("]LogViewport: Display: Viewport MouseLockMode Changed, LockOnCapture -> DoNotLock", "");
                //                    if (Form1Module.num != showoverlayButton)
                //                    {
                //                        Form1Module.ShowSquadMembers = true;
                //                    }
                //                    Form1Module.num = showoverlayButton;
                //                }
                //                catch (Exception ex)
                //                {
                //                    Debug.WriteLine(ex.Message);

                //                }
                //            }
                //            else if (line.Contains("LogViewport: Display: Player bShowMouseCursor Changed, False -> True")/*&& line.Contains("LogViewport: Display: Viewport MouseCaptureMode Changed, NoCapture -> CapturePermanently")*/)
                //            {
                //                try
                //                {
                //                    var showoverlayButton = line.Split("][")[1].Replace("]LogViewport: Display: Player bShowMouseCursor Changed, False -> True", "");
                //                    if (Form1Module.num == showoverlayButton)
                //                    {
                //                        Form1Module.ShowSquadMembers = false;
                //                    }
                //                    Form1Module.num = showoverlayButton;
                //                }
                //                catch (Exception ex)
                //                {
                //                    Debug.WriteLine(ex.Message);

                //                }
                //            }
                //            else if (line.Contains("LogViewport: Display: Viewport MouseLockMode Changed, DoNotLock -> LockOnCapture")/*&& line.Contains("LogViewport: Display: Viewport MouseCaptureMode Changed, NoCapture -> CapturePermanently")*/)
                //            {
                //                try
                //                {
                //                    var showoverlayButton = line.Split("][")[1].Replace("]LogViewport: Display: Viewport MouseLockMode Changed, DoNotLock -> LockOnCapture", "");
                //                    if (Form1Module.num != showoverlayButton)
                //                    {
                //                        Form1Module.ShowSquadMembers = false;
                //                    }
                //                    Form1Module.num = showoverlayButton;
                //                }
                //                catch (Exception ex)
                //                {
                //                    Debug.WriteLine(ex.Message);

                //                }
                //            }
                //            else if (line.Contains("LogExit: Exiting."))
                //            {
                //                UserPCSettings.serverConnection = false;
                //                Form1Module.lbl_Main_ServerConnection = "Offline";
                //                Form1Module.lbl_Main_GameOpen = "Game Closed";
                //                UserPCSettings.gameDetection = false;
                //            }

                //        }
                //        UserPCSettings._scumLogPathPosition = file.Position;

                //    }


                //}
            }
            Thread.Sleep(10);


        }


    }
}
