﻿using System;
using System.Collections.Generic;

namespace MR.AspNet.Identity.EntityFramework6
{
	/// <summary>
	/// The default implementation of <see cref="IdentityRole{TKey}"/> which uses a string as the primary key.
	/// </summary>
	public class IdentityRole : IdentityRole<string, IdentityUserRole, IdentityRoleClaim>
	{
		/// <summary>
		/// Initializes a new instance of <see cref="IdentityRole"/>.
		/// </summary>
		/// <remarks>
		/// The Id property is initialized to from a new GUID string value.
		/// </remarks>
		public IdentityRole()
		{
			Id = Guid.NewGuid().ToString();
		}

		/// <summary>
		/// Initializes a new instance of <see cref="IdentityRole"/>.
		/// </summary>
		/// <param name="roleName">The role name.</param>
		/// <remarks>
		/// The Id property is initialized to from a new GUID string value.
		/// </remarks>
		public IdentityRole(string roleName) : this()
		{
			Name = roleName;
		}
	}

	/// <summary>
	/// Represents a role in the identity system
	/// </summary>
	/// <typeparam name="TKey">The type used for the primary key for the role.</typeparam>
	/// <typeparam name="TUserRole">The type that represents the link between a user and a role.</typeparam>
	/// <typeparam name="TRoleClaim">The type that represents a claim that is granted to all users within a role.</typeparam>
	public class IdentityRole<TKey, TUserRole, TRoleClaim>
		where TUserRole : IdentityUserRole<TKey>
		where TRoleClaim : IdentityRoleClaim<TKey>
		where TKey : IEquatable<TKey>
	{
		/// <summary>
		/// Initializes a new instance of <see cref="IdentityRole{TKey}"/>.
		/// </summary>
		public IdentityRole()
		{
			Users = new List<TUserRole>();
			Claims = new List<TRoleClaim>();
			ConcurrencyStamp = Guid.NewGuid().ToString();
		}

		/// <summary>
		/// Initializes a new instance of <see cref="IdentityRole{TKey}"/>.
		/// </summary>
		/// <param name="roleName">The role name.</param>
		public IdentityRole(string roleName) : this()
		{
			Name = roleName;
		}

		/// <summary>
		///     Navigation property for users in the role
		/// </summary>
		public virtual ICollection<TUserRole> Users { get; }

		/// <summary>
		///     Navigation property for claims in the role
		/// </summary>
		public virtual ICollection<TRoleClaim> Claims { get; }

		/// <summary>
		/// Gets or sets the primary key for this role.
		/// </summary>
		public virtual TKey Id { get; set; }

		/// <summary>
		/// Gets or sets the name for this role.
		/// </summary>
		public virtual string Name { get; set; }

		/// <summary>
		/// Gets or sets the normalized name for this role.
		/// </summary>
		public virtual string NormalizedName { get; set; }

		/// <summary>
		/// A random value that should change whenever a role is persisted to the store
		/// </summary>
		public virtual string ConcurrencyStamp { get; set; }

		/// <summary>
		/// Returns the name of the role.
		/// </summary>
		/// <returns>The name of the role.</returns>
		public override string ToString()
		{
			return Name;
		}
	}
}
