using System.ComponentModel.DataAnnotations;

namespace Core.Entities;
public class Team
{
    public int Id { get; set; }

	[Required]
    public EventDefinition Event { get; set; }

    public List<Student> Students { get; set; } = [];

    public Student? Captain { get; set;  }

    [Display(Name = "Team Identifier")]
	[StringLength(32)]
    public string? Identifier { get; set; }


 //   public Tuple<DateTime,DateTime?>? RegionalTimeSlotObj
	//{
	//	get
	//	{
			
	//		if (string.IsNullOrEmpty(RegionalTimeSlot))
	//			return null;
	//		var times = Regex.Matches(RegionalTimeSlot, @"(.*)\s*-\s*(.*)");
	//		if (times.Count == 0)
	//			return Tuple.Create(P(RegionalTimeSlot), (DateTime?)null);
	//		var match = times[0];
	//		if (!match.Success)
	//			return Tuple.Create(P(RegionalTimeSlot), (DateTime?)null);

	//		return Tuple.Create(P(match.Groups[1].Value), (DateTime?)P(match.Groups[2].Value));
	//		return null;
	//	}
	//}

    private DateTime P(string s)
	{
		var dt = DateTime.Parse(s);
		if (dt.TimeOfDay < TimeSpan.FromHours(7))
			return dt + TimeSpan.FromHours(12);
		return dt;
	}

	public virtual Team CloneWithOmittedStudents(IEnumerable<Student> studentsToOmit)
	{
		var studentsToOmitList = studentsToOmit.ToList();
		var omittedStudents = Students.Where(studentsToOmitList.Contains).ToList();
		if (omittedStudents.Count == 0)
			return new Team{Captain = Captain, Event = Event, Students = Students.ToList(), Identifier = Identifier, Id = Id};

		var remainingStudents = Students.Where(s => !studentsToOmitList.Contains(s)).ToList();
		return new PartialTeam {  Event = Event, Students = remainingStudents,  OmittedStudents = omittedStudents, Identifier = Identifier, Id = Id};
	}

	public Team Clone() => CloneWithOmittedStudents([]);
	

	public override string ToString()
	{
		return $"{Event.Name} {(Identifier != null ? $"({Identifier})" : "")}";
	}

    public string StudentsFirstNames
    {
        get
        {
            return
                string.Join(", ", 
                    Students.Select(e => 
                        e.FirstName
                        + (Captain != null && (Captain.Equals(e)) ? "(Cpt)" : ""))
                    );
        }
    }
}