using IdentityModel;
using IdentityServer4;
using IdentityServer4.Models;
using Kimuser.Configurations;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static IdentityServer4.IdentityServerConstants;

namespace Kimuser
{
    public static class Config
    {
        // scopes define the resources in your system
        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResource("roles",new []{"role"}),

            };
        }

        public static IEnumerable<ApiScope> GetApiScopes(IConfiguration configuration)
        {
            var apiScopes = new List<ApiScope>();
            apiScopes.Add(new ApiScope(LocalApi.ScopeName));
            var apiScopeConfigs = configuration.GetSection("ApiScopes").Get<List<ApiScopeConfig>>();

            if (apiScopeConfigs.Any())
            {
                foreach (var apiScopeConfig in apiScopeConfigs)
                {
                    apiScopes.Add(new ApiScope(apiScopeConfig.Name, apiScopeConfig.DisplayName));
                }
            }
            return apiScopes;
        }

        // clients want to access resources (aka scopes)
        public static IEnumerable<Client> GetClients(IConfiguration configuration)
        {
            var configClients = configuration.GetSection("Clients").Get<List<ClientConfig>>();

            if (!configClients.Any()) return new List<Client>();
            var clients = new List<Client>();
            foreach (var configClient in configClients)
            {
                var client = new Client();
                if (configClient.ClientType == ClientType.Website)
                {
                    client = new Client()
                    {
                        ClientId = configClient.ClientId,
                        //ClientName = configClient.ClientName,
                        AllowedGrantTypes = GrantTypes.Code,
                        //configClient.AllowedGrantTypes,
                        //RequireConsent = configClient.RequireConsent,
                        ClientSecrets = { new Secret(configClient.ClientSecrets.ToSha256()) },
                        RedirectUris = configClient.RedirectUris,
                        PostLogoutRedirectUris = configClient.PostLogoutRedirectUris,
                        AllowedScopes = configClient.AllowedScopes,
                        //AllowOfflineAccess = configClient.AllowOfflineAccess,
                        //AlwaysSendClientClaims = configClient.AlwaysSendClientClaims,
                        AccessTokenLifetime = configClient.AccessTokenLifetime
                    };
                }
                else if (configClient.ClientType == ClientType.Api)
                {
                    client.ClientId = configClient.ClientId;
                    client.AllowedGrantTypes = configClient.AllowedGrantTypes;
                    client.ClientSecrets = new List<Secret>()
                    {
                        new Secret(configClient.ClientSecrets.ToSha256())
                    };
                    client.AllowedScopes = configClient.AllowedScopes;
                    client.AllowOfflineAccess = true;
                }
                else if (configClient.ClientType == ClientType.MobileApp)
                {
                    client = new Client
                    {
                        ClientId = configClient.ClientId,
                        ClientName = configClient.ClientName,

                        RedirectUris = configClient.RedirectUris,
                        PostLogoutRedirectUris = configClient.PostLogoutRedirectUris,

                        RequireClientSecret = false,

                        AllowedGrantTypes = GrantTypes.Code,
                        AllowedScopes = configClient.AllowedScopes,

                        AllowOfflineAccess = true,
                        RefreshTokenUsage = TokenUsage.OneTimeOnly,
                        RefreshTokenExpiration = TokenExpiration.Sliding
                    };

                    //client.ClientId = configClient.ClientId;
                    //client.ClientName = configClient.ClientName;

                    //client.RedirectUris = { ""};
                    //client.PostLogoutRedirectUris = configClient.PostLogoutRedirectUris;

                    //client.RequireClientSecret = configClient.RequireClientSecret;

                    //client.AllowedGrantTypes = configClient.AllowedGrantTypes;
                    ////GrantTypes.Code

                    //client.AllowedScopes = configClient.AllowedScopes;

                    //client.AllowOfflineAccess = configClient.AllowOfflineAccess;

                    //client.RefreshTokenUsage = configClient.RefreshTokenUsage;
                    ////TokenUsage.OneTimeOnly;

                    //client.RefreshTokenExpiration = configClient.RefreshTokenExpiration;
                    ////TokenExpiration.Sliding;


                    //client.ClientId = configClient.ClientId;
                    //client.AllowedGrantTypes = configClient.AllowedGrantTypes;
                    //client.ClientSecrets = new List<Secret>()
                    //{
                    //    new Secret(configClient.ClientSecrets.ToSha256())
                    //};
                    //client.AllowedScopes = configClient.AllowedScopes;
                    //client.AllowOfflineAccess = true;
                }

                if (!string.IsNullOrEmpty(client.ClientId))
                {
                    clients.Add(client);
                }
            }
            return clients;
        }
    }
}
