﻿using System;
using System.Web;
using Weixing.Toolkit.Authentication.Abstractions;

namespace Weixing.Toolkit.Authentication.Carriers
{
	internal static class IdentityManagerFactory
	{
		internal static IIdentityManager<T> CreateIdentityManager<T>(HttpContextBase httpContext, IdentityCarrier carrier, IdentityOptions<T> options = null)
			where T : IFormattable, IEquatable<T> {
			if ( httpContext == null ) {
				throw new ArgumentNullException(nameof(httpContext));
			}
			switch ( carrier ) {
				default:
				case IdentityCarrier.Cookie: return new CookieIdentityManager<T>(httpContext, options);
				case IdentityCarrier.Header: return new HeaderIdentityManager<T>(httpContext, options);
				case IdentityCarrier.Session: return new SessionIdentityManager<T>(httpContext, options);
			}
		}

		internal static IIdentityManager<T> CreateIdentityManager<T>(HttpContextBase httpContext, IdentityCarrier carrier, Action<IdentityOptions<T>> configure = null)
			where T : IFormattable, IEquatable<T> {
			if ( httpContext == null ) {
				throw new ArgumentNullException(nameof(httpContext));
			}
			var options = new IdentityOptions<T>();
			configure?.Invoke(options);
			return CreateIdentityManager(httpContext, carrier, options);
		}

	}
}
