using System;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using EFB.Models;
using EFB.Models.JSON;
using EFB.Sessions;
using EFB.Controllers.Form;
using EFB.Controllers.API;

namespace EFB.Models
{
    public class ChartModel
    {
        public string ICAO { get; set; }
        public Chart[] General { get; set; }
        public Chart[] TextualData { get; set; }
        public Chart[] GroundLayout { get; set; }
        public Chart[] SID { get; set; }
        public Chart[] STAR { get; set; }
        public Chart[] Approach { get; set; }
        public Chart[] Transition { get; set; }
        public Chart[] PilotBriefing { get; set; }

        [JsonConstructor]
        public ChartModel(){
            //Empty constructor for JSON Serialisation Purposes
        }
        public ChartModel(string ICAO, ChartList response)
        {
            this.ICAO = ICAO;
            General = FillChart(response.General);
            TextualData = FillChart(response.TextualData);
            GroundLayout = FillChart(response.GroundLayout);
            SID = FillChart(response.SID);
            STAR = FillChart(response.STAR);
            Approach = FillChart(response.Approach);
            Transition = FillChart(response.Transition);
            PilotBriefing = FillChart(response.PilotBriefing);
        }

        private Chart[] FillChart(ChartsCollection collection){
            if (collection != null)
            {
                return collection.Charts;
            }
            return new Chart[]{};
        }

        public static async Task<ChartList> FetchAsync(string ICAO){
            Console.WriteLine("Start");
            if (FormAuthenticator.ValidateICAOCode(ICAO))
            {
                APIInterface API = new APIInterface();
                Dictionary <string, string> headerData = new Dictionary<string, string>();
                headerData.Add("referer", "luke-else.co.uk");

                Dictionary<string, string> formData = new Dictionary<string, string>();
                
                formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User));
                FormUrlEncodedContent body = new FormUrlEncodedContent(formData);

                //make Charts request
                var requestCharts = await API.Post<ChartResponse>($"https://chartfox.org/api/charts/grouped/{ICAO}", headerData, body);
                Console.WriteLine("End");
                if (requestCharts.Result.Status == "success")
                {
                    return requestCharts.Result.Response;   
                }
            }
            return null;
        }
    }

    public class Pinned
    {
        public Chart[] Charts { get; set; }
    }

    // public class Chart   *Found in Models/JSON*
    // {
    //     public string Name { get; set; }
    //     public string Identifier { get; set; }
    //     public int TypeCode { get; set; }
    //     public string Type { get; set; }
    //     public string Runway { get; set; }
    //     public string PseudoURL { get; set; }
    //     public string URL { get; set; }
    // }

}