using System;
using Example.ViewModels;
using Example.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Diagnostics;

namespace Example.Connectors
{
    public static class GetLogedInServer
    {
        public static CurrentServerLoggedOn _currentServerLoggedOn = new CurrentServerLoggedOn();


        public static async Task<CurrentServerLoggedOn> LoadServerData(string playerSteamid)
        {
            try
            {
                string url = $"https://api.whalleybot.com/v2/GetCurrentServer/{playerSteamid}";
                var handler = new HttpClientHandler()
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
                };
                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.Add("Authorization", "ptlc_zMHm25ZD6AftYkhQJySteBYBxeXyT35V2SAUfDPgHBK");
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    var apicall = await client.GetAsync(url)
                        .ConfigureAwait(false);
                    if (apicall.IsSuccessStatusCode)
                    {
                        var json = apicall.Content.ReadAsStringAsync().Result;
                        var readjson = new CurrentServerLoggedOn();
                        readjson = JsonConvert.DeserializeObject<CurrentServerLoggedOn>(json);



                        if (_currentServerLoggedOn != null)
                        {
                            if (readjson.status == true)
                            {
                                _currentServerLoggedOn = readjson;
                                return _currentServerLoggedOn;
                            }
                            else if (readjson.status == true && readjson.serverID != _currentServerLoggedOn.serverID)
                            {
                                _currentServerLoggedOn = readjson;
                                return _currentServerLoggedOn;

                            }
                            else if (_currentServerLoggedOn.status == true && readjson.status == false)
                            {
                                return _currentServerLoggedOn;
                            }
                            else
                            {
                                _currentServerLoggedOn = readjson;
                                _currentServerLoggedOn.serverID = "";
                                return _currentServerLoggedOn;
                            }
                        }
                        _currentServerLoggedOn = readjson;
                        return _currentServerLoggedOn;


                    }
                    else if (apicall.StatusCode == System.Net.HttpStatusCode.BadRequest && _currentServerLoggedOn != null)
                    {
                        //_currentServerLoggedOn.status = true;
                        return _currentServerLoggedOn;
                    }
                    else if (apicall.StatusCode == System.Net.HttpStatusCode.BadRequest)
                    {
                        var json = apicall.Content.ReadAsStringAsync().Result;
                        _currentServerLoggedOn = JsonConvert.DeserializeObject<CurrentServerLoggedOn>(json);
                        return _currentServerLoggedOn;
                    }
                    else
                    {
                        //DialogResult msgBox_GameClose = MessageBox.Show("Sorry! We have a problem finding your player information.\n\n Please try again or restart your app!", "Connection Fail!", MessageBoxButtons.AbortRetryIgnore);
                        //if (msgBox_GameClose == DialogResult.Retry)
                        //{
                        //    await LoadServerData(playerSteamid);
                        //}
                        //else
                        //{

                        //    return null;
                        //}
                        ////throw new Exception(apicall.ReasonPhrase);
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Source + ex.Message + ex.Data + ex.StackTrace);

                return null;
            }

        }

        public static async Task<BattlemetricServerModule> SearchServer(string serverIP_port)
        {
            try
            {
                string apiUrl = "https://api.battlemetrics.com/servers?filter[game]=scum&filter[search]=";
                using (var battlemetric = new HttpClient())
                {
                    var battleResponse = await battlemetric.GetAsync(apiUrl + serverIP_port)
                        .ConfigureAwait(false);

                    if (battleResponse.IsSuccessStatusCode)
                    {
                        var battleJson = battleResponse.Content.ReadAsStringAsync().Result;
                        BattlemetricServerModule Infoserver = JsonConvert.DeserializeObject<BattlemetricServerModule>(battleJson);

                        return Infoserver;
                    }
                    else
                    {
                        throw new Exception(battleResponse.ReasonPhrase + battleResponse.StatusCode);
                    }

                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Source + ex.Message + ex.Data + ex.StackTrace);
                return null;
            }

        }
    }
}
