using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TEAMModelOS.Models
{
    public class Option
    {
        public Option()
        {
        }

        public string Location { get; set; }
        public string LocationNum { get; set; }  
        public string BlobDomain { get; set; }
        public string HostName { get; set; }
        public string HttpTrigger { get; set; }
        public IList<string> AllowedHosts { get; }
        public IList<StringSegment> AllowedRedirects
        {
            get
            {
                var allowedRedirects = new List<StringSegment>();
                if (AllowedHosts?.Count > 0 && !TryProcessHosts(AllowedHosts, allowedRedirects))
                {
                    return allowedRedirects;
                }
                return allowedRedirects;
            }
        }
        public string JwtSecretKey { get; set; }
        public string Authority { get; set; }
        public string Audience { get; set; }
        public string OSFunction { get; set; }



        private bool TryProcessHosts(IEnumerable<string> incoming, IList<StringSegment> results)
        {
            foreach (var entry in incoming)
            {
                // Punycode. Http.Sys requires you to register Unicode hosts, but the headers contain punycode.
                var host = new HostString(entry).ToUriComponent();
                if (IsTopLevelWildcard(host)) continue;
                if (!results.Contains(host, StringSegmentComparer.OrdinalIgnoreCase)) results.Add(host);
            }
            return true;
        }

        private bool IsTopLevelWildcard(string host)
        {
            return (string.Equals("*", host, StringComparison.Ordinal) // HttpSys wildcard
                           || string.Equals("[::]", host, StringComparison.Ordinal) // Kestrel wildcard, IPv6 Any
                           || string.Equals("0.0.0.0", host, StringComparison.Ordinal)); // IPv4 Any
        }

    }  
}
