using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using MercatorExtensions;

// <CompileWithRoslyn />

namespace TestTunnel
{
    public static class Class1
    {
        public static ParticipantResponse ParticipantInfo(string peppolId, out string error)
        {
            if (!peppolId.Contains(":"))
            {
                error = "Invalid peppolId!";
                return null;
            }
            string url = "https://directory.peppol.eu/search/1.0/json?participant=iso6523-actorid-upis::" + peppolId;

            MercatorExtensionsClass.JsonResultUpdater jsonResultUpdater = (jsonResult) =>
                jsonResult.Replace("-result-", "result")
                          .Replace("-page-", "page")
                          .Replace("query-terms", "queryterms")
                          .Replace("creation-dt", "creationdt");

#if (MERCATOR_CORE)
            MercatorHttpClient.HttpClient client = MercatorHttpClient.HttpClient.Create(url);
#else
            HttpWebRequest client = (HttpWebRequest)WebRequest.Create(url);
#endif
            client.Timeout = 2000;
            return client.GetData<ParticipantResponse>(jsonResultUpdater, out error);
        }

        public async static Task<(ParticipantResponse participantResponse, string error)> ParticipantInfoAsync(string peppolId)
        {
            if (!peppolId.Contains(":"))
                return (null, "Invalid peppolId!");
            
            string url = "https://directory.peppol.eu/search/1.0/json?participant=iso6523-actorid-upis::" + peppolId;

            MercatorExtensionsClass.JsonResultUpdater jsonResultUpdater = (jsonResult) =>
                jsonResult.Replace("-result-", "result")
                          .Replace("-page-", "page")
                          .Replace("query-terms", "queryterms")
                          .Replace("creation-dt", "creationdt");

#if (MERCATOR_CORE)
            MercatorHttpClient.HttpClient client = MercatorHttpClient.HttpClient.Create(url);
#else
            HttpWebRequest client = (HttpWebRequest)WebRequest.Create(url);
#endif
            client.Timeout = 2000;
            return await client.GetDataAsync<ParticipantResponse>(jsonResultUpdater);
        }
    }

    public class ParticipantResponse
    {
        public class Contact
        {
            public string type { get; set; }
            public string name { get; set; }
            public string phone { get; set; }
            public string email { get; set; }
        }

        public class DocType
        {
            public string scheme { get; set; }
            public string value { get; set; }
        }

        public class Entity
        {
            public List<Name> name { get; set; }
            public string countryCode { get; set; }
            public List<Identifier> identifiers { get; set; }
            public List<Contact> contacts { get; set; }
            public string regDate { get; set; }
        }

        public class Identifier
        {
            public string scheme { get; set; }
            public string value { get; set; }
        }

        public class Match
        {
            public ParticipantID participantID { get; set; }
            public List<DocType> docTypes { get; set; }
            public List<Entity> entities { get; set; }
        }

        public class Name
        {
            public string name { get; set; }
        }

        public class ParticipantID
        {
            public string scheme { get; set; }
            public string value { get; set; }
        }

        public string version { get; set; }
        public int totalresultcount { get; set; }
        public int usedresultcount { get; set; }
        public int resultpageindex { get; set; }
        public int resultpagecount { get; set; }
        public int firstresultindex { get; set; }
        public int lastresultindex { get; set; }
        public string queryterms { get; set; }
        public DateTime creationdt { get; set; }
        public List<Match> matches { get; set; }
    }
}