using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlTypes;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;

namespace MileageTraker.Web.Models
{
	public class User
	{
		[Key]
		[HiddenInput(DisplayValue = false)]
		public Guid UserId { get; set; }

		[Required]
		[StringLength(64)]
		public string Username { get; set; }

		[Required]
		[StringLength(64)]
		[DataType(DataType.EmailAddress)]
		public string Email { get; set; }

		[Required]
		[StringLength(128)]
		[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Need complete name")]
		public string FullName { get; set; }

		[DataType(DataType.Password)]
		[StringLength(128)]
		public string Password { get; set; }

		[DataType(DataType.MultilineText)]
		public string Comment { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool IsApproved { get; set; }

		[HiddenInput]
		public int PasswordFailuresSinceLastSuccess { get; set; }

		[HiddenInput(DisplayValue = false)]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
		public DateTime LastPasswordFailureDate { get; set; }

		[HiddenInput(DisplayValue = false)]
		[DataType(DataType.DateTime)]
		public DateTime LastActivityDate { get; set; }

		[HiddenInput(DisplayValue = false)]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
		public DateTime LastLockoutDate { get; set; }

		[HiddenInput(DisplayValue = false)]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
		public DateTime LastLoginDate { get; set; }

		[HiddenInput(DisplayValue = false)]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
		public DateTime Created { get; set; }

		[HiddenInput]
		public bool IsLockedOut { get; set; }

		[HiddenInput]
		[DataType(DataType.Date)]
		[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy}")]
		public DateTime LastPasswordChangedDate { get; set; }

		[HiddenInput(DisplayValue = false)]
		[StringLength(128)]
		public string PasswordResetToken { get; set; }

		[HiddenInput]
		public virtual ICollection<Role> Roles { get; set; }

		public User()
		{
			LastActivityDate = SqlDateTime.MinValue.Value;
			LastLockoutDate = SqlDateTime.MinValue.Value;
			LastLoginDate = SqlDateTime.MinValue.Value;
			LastPasswordChangedDate = SqlDateTime.MinValue.Value;
			LastPasswordFailureDate = SqlDateTime.MinValue.Value;
		}

		public MembershipUser ToMembershipUser(string providerName)
		{
			return new MembershipUser(providerName, Username, UserId,
						  Email, null, null,
						  IsApproved, IsLockedOut, Created,
						  LastLoginDate, LastActivityDate,
						  LastPasswordChangedDate, LastLockoutDate);
		}

		public bool Filter(string q)
		{
			if (string.IsNullOrEmpty(q))
				return true;

			var queries = q.Split(' ');

			return queries.Any(query =>
				Username.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0
				|| FullName.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0);
		}
	}
}