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

namespace Example.Connectors
{
    public static class GetTaxis
    {
        public static List<TaxiDataModule> _taxiDetails = new List<TaxiDataModule>();
        public static async Task<List<TaxiDataModule>> LoadTaxis(string serverPort)
        {
            try
            {
                string apiurl = $"https://api.whalleybot.com/bot/{serverPort}/GetTaxis";
                var handler = new HttpClientHandler()
                {
                    ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
                };

                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.Add("Authorization", "WhalleyBotOverlay_e-H1rr55f");
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    var response = await client.GetAsync(apiurl)
                        .ConfigureAwait(false);
                    //var response = apicall.GetAwaiter().GetResult();
                    if (response.IsSuccessStatusCode)
                    {
                        var json = response.Content.ReadAsStringAsync().Result;

                        _taxiDetails = JsonConvert.DeserializeObject<List<TaxiDataModule>>(json);

                        return _taxiDetails;
                    }
                    if (response.StatusCode == System.Net.HttpStatusCode.BadRequest && _taxiDetails == null)
                    {
                        Form1Module.WarningMsg = response.ReasonPhrase;
                        Form1Module.OverlayCanOpen = "On";
                        Form1Module.OverlayCanOpenBackcolor = System.Drawing.Color.DarkGreen;
                        Form1Module.OverlayCanOpenForecolor = System.Drawing.Color.White;
                        return _taxiDetails;
                    }
                    else
                    {
                        throw new Exception(response.ReasonPhrase + response.StatusCode);
                    }

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