using DamnOverSharp.Renderers.Chromium;
using DamnOverSharp.Renderers.Graphic;
using DamnOverSharp.Renderers.WPF;
using Example.Connectors;
using Example.Models.StaticData;
using Example.Models;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Example.Ingame;
using System.Diagnostics;
using System.Windows.Forms;

namespace Example
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static WpfRenderer Renderer;
        GraphicRenderer BitmapRenderer;
        public static CurrentServerLoggedOn connectedServer = new CurrentServerLoggedOn();
        public static DispatcherTimer timer = new DispatcherTimer();

        private CancellationTokenSource _cancellationTokenReadLogs;

        public MainWindow()
        {
            InitializeComponent();
            //connectedServer = await GetLogedInServer.LoadServerData("76561197995865733").Result;
            timer.Tick += (sender, e) => UpdatePagesInformation(sender, e);
            timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            timer.Start();
            Screen primaryScreenSize = Screen.PrimaryScreen;
            UserPCSettings.maxScreenWidth = primaryScreenSize.Bounds.Width;
            UserPCSettings.maxScreenHeight = primaryScreenSize.Bounds.Height;

        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            Renderer?.Destroy();
            Renderer = null;
            BitmapRenderer?.Destroy();
            BitmapRenderer = null;
        }

        private void Button_ShowWPFOverlay_Clicked(object sender, RoutedEventArgs e)
        {
            Renderer?.Destroy();
            Renderer = null;
            BitmapRenderer?.Destroy();
            BitmapRenderer = null;

            Renderer = new WpfRenderer("SCUM", new Size(UserPCSettings.maxScreenWidth, UserPCSettings.maxScreenHeight), new Point(0, 0)) { InteractionOffset = new Point(0, 0) };
            Renderer.AddControl(new overlay() { VerticalAlignment = VerticalAlignment.Top });
            Renderer.UpdateVisual();
        }


        private async void UpdatePagesInformation(object sender, EventArgs e)
        {
            if (SetThreadStatus.dochecks == 0)
            {
                SetThreadStatus.dochecks = 1;
                _cancellationTokenReadLogs = new CancellationTokenSource();
                await MainLoop(_cancellationTokenReadLogs.Token);
            }


            GetLogedInServer._currentServerLoggedOn.status = true;
            GetLogedInServer._currentServerLoggedOn.serverID = "eeddaaf5";
            GetLogedInServer._currentServerLoggedOn.apiPort = "";
            //UserPCSettings.activeSteamID = "76561199205876580";
            if (GetLogedInServer._currentServerLoggedOn.status)
            {
                Form1Module.CurrentServer = GetLogedInServer._currentServerLoggedOn.serverID;
                UserPCSettings.serverConnection = GetLogedInServer._currentServerLoggedOn.status;
            }
            MainWindow_UserName.Text = GetPlayerInfo.PlayerDetails.playerName;
        }

        private async Task MainLoop(CancellationToken cancellation)
        {
            await Task.Run(() =>
            {
                try
                {
                    while (true)
                    {
                        Thread.Sleep(100);
                        dispatcherTimer_Tick();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Source);
                }
            });

        }

        private async void dispatcherTimer_Tick()
        {
            UserPCSettings.activeSteamID = "76561197995865733";
            UserPCSettings.gameDetection = true;

            connectedServer = new CurrentServerLoggedOn();
            connectedServer.status = true;
            connectedServer.serverID = "eeddaaf5";
            connectedServer.apiPort = "";
            if (connectedServer.status)
            {
                Form1Module.CurrentServer = connectedServer.serverID;
                UserPCSettings.serverConnection = connectedServer.status;
            }
            await CheckInfo.ConfirmServerWhaleyServerConnection();



        }

        private void btnMinimize_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void btnClose_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Border_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void btnMaximise_Click(object sender, RoutedEventArgs e)
        {
            switch (this.WindowState)
            {
                case WindowState.Maximized:
                    this.WindowState = WindowState.Normal;
                    break;
                case WindowState.Normal:
                    this.WindowState = WindowState.Maximized;
                    break;

            }
        }
    }
}
