using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace WebApplication1.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {

        [RegularExpression(@"^[\p{IsCyrillic}\p{IsBasicLatin}\s\-]+$", ErrorMessage = "Името смее да содржи само латиница, кирилица, празни места и цртички.")]
        [Required(ErrorMessage = "Името е задолжително.")]
        [StringLength(50, ErrorMessage = "Името мора да е под 50 букви.")]
        public string Name { get; set; }

        [RegularExpression(@"^[\p{IsCyrillic}\p{IsBasicLatin}\s\-]+$", ErrorMessage = "Името смее да содржи само латиница, кирилица, празни места и цртички.")]
        [Required(ErrorMessage = "Презимето е задолжително.")]
        [StringLength(50, ErrorMessage = "Презимето мора да е под 50 букви.")]
        public string Surname { get; set; }

        [StringLength(1000, ErrorMessage = "Кратката биографија мора да е под 1000 карактери.")]
        public string Biography { get; set; }

        public virtual ICollection<Subject> Subjects { get; set; } = new List<Subject>();
        public virtual ICollection<Topic> Topics { get; set; } = new List<Topic>();
        public virtual List<Project> Projects { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class Student : ApplicationUser
    {
        [RegularExpression(@"^\d{6}$", ErrorMessage = "Index must be exactly 6 digits.")]
        public string Index { get; set; }
        public string Major { get; set; }

        [Range(1, 3, ErrorMessage = "Cycle must be between 1 and 3.")]
        public int? Cycle { get; set; }
        [Range(1, 8, ErrorMessage = "Semester must be between 1 and 8.")]
        public int? Semester { get; set; }
    }
    public class Mentor : ApplicationUser
    {
        [DisplayName("When are you available?")]
        public string Timeslots { get; set; } // times available

        [DisplayName("What types of projects are you open to mentoring?")]
        public string TypesOfProject { get; set; }
        [Required]
        [DisplayName("Are you accepting student requests?")]
        public bool Available { get; set; }
        public string ImageURL { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Student> Students { get; set; }
        public DbSet<Mentor> Mentors { get; set; }
        public DbSet<Subject> Subjects { get; set; }
        public DbSet<Topic> Topics { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<MentorContact> MentorContacts { get; set; }
        public DbSet<Message> Messages { get; set; }
        public DbSet<Opinion> Opinions { get; set; }

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Student>().ToTable("AspNetUsers");
            modelBuilder.Entity<Mentor>().ToTable("AspNetUsers");

            modelBuilder.Entity<Message>()
                .HasRequired(m => m.FromUser)
                .WithMany()
                .HasForeignKey(m => m.FromUserId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Message>()
                .HasRequired(m => m.ToUser)
                .WithMany()
                .HasForeignKey(m => m.ToUserId)
                .WillCascadeOnDelete(false);

        }
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        //public System.Data.Entity.DbSet<WebApplication1.Models.ApplicationUser> ApplicationUsers { get; set; }
    }
}