﻿/*
 * Copyright (c) Michel Bechelani. All rights reserved.
 * see LICENSE
 */

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace CloudNinja.Authorization.EntityFramework
{
    public class Client<TClientScope, TRedirectUri, TClientClaim> : IClient
        where TClientScope : ClientScope
        where TRedirectUri : ClientRedirectUri
        where TClientClaim : ClientClaim
    {
        public virtual int Id { get; set; }

        public virtual string Name { get; set; }
        public virtual string ClientUri { get; set; }
        public virtual string LogoUri { get; set; }

        public virtual string ClientId { get; set; }
        public virtual string ClientSecret { get; set; }

        public virtual Flows Flow { get; set; }

        public virtual bool RequireConsent { get; set; }

        public virtual bool AllowRememberConsent { get; set; }

        public virtual bool Enabled { get; set; }

        public virtual bool AlwaysSendClientClaims { get; set; }
        public virtual bool PrefixClientClaims { get; set; }

        // in seconds
        [Range(0, Int32.MaxValue)]
        public virtual int AccessTokenLifetime { get; set; }
        [Range(0, Int32.MaxValue)]
        public virtual int AuthorizationCodeLifetime { get; set; }
        [Range(0, Int32.MaxValue)]
        public virtual int AbsoluteRefreshTokenLifetime { get; set; }
        [Range(0, Int32.MaxValue)]
        public virtual int SlidingRefreshTokenLifetime { get; set; }

        public TokenUsage RefreshTokenUsage { get; set; }

        public TokenExpiration RefreshTokenExpiration { get; set; }

        public AccessTokenType AccessTokenType { get; set; }

        public ApplicationTypes ApplicationType { get; set; }

        public virtual DateTime Created { get; set; }

        public virtual DateTime? LastModified { get; set; }

        public ICollection<TClientScope> Scopes { get; private set; }

        public ICollection<TRedirectUri> RedirectUris { get; private set; }

        public ICollection<TClientClaim> Claims { get; private set; }

        public Client()
        {
            Flow = Flows.Implicit;
            
            Scopes = new List<TClientScope>();
            RedirectUris = new List<TRedirectUri>();
            Claims = new List<TClientClaim>();

            // 5 minutes
            AuthorizationCodeLifetime = 300;

            // one hour
            AccessTokenLifetime = 3600;

            // 3 days
            SlidingRefreshTokenLifetime = 259200;

            // 30 days
            AbsoluteRefreshTokenLifetime = 2592000;

            RefreshTokenUsage = TokenUsage.OneTimeOnly;
            RefreshTokenExpiration = TokenExpiration.Absolute;

            AccessTokenType = AccessTokenType.Reference;
        }
    }
}
