package project_1;
public class Student implements Comparable<Student>{

	private String name;
	private int id;
	private String year;
	private LinkedList<Course> list = new LinkedList<>();


	public Student(String name,int id){
		this.name=name;
		this.id=id;
		
        this.year= String.valueOf(id).substring(0,3);
		
	}
	
	public boolean add(Course course){
		if(isThereFreeHours() && course.isValid() && course.canTakeIt(new Student(this.name,this.id)) 
				&& !(isThereConflict(course)) && course.thereIsEmptySite()){
			list.addInsertionTime(list,course);
			return true;
		}
		return false;
	}
	
	
	public boolean isThereFreeHours(){
		return list.getSize() <= 5 ;
	}
	
	public boolean isThereConflict(Course c){
		if(list.getSize()==0){
			return false;
		}
		Node<Course> pos= list.getHead();
		
		
		while(pos != null){
		
			if(isThereConflict(pos.getData(), c)){
				return true;
			}
			pos=pos.getNext();
		}
		return false;
		
	}
	
	public boolean isThereConflict(Course a,Course b ){
		int startA = Integer.parseInt(a.getStartTime().substring(0,2));
		int finishA = Integer.parseInt(a.getFinishTime().substring(0,2));

		int startB = Integer.parseInt(b.getStartTime().substring(0,2));
	
		
		for(int i=startA;i<=finishA;++i){
			if(startB==i)
				return true;
			++startA;
		}
		
		
		return false;
	}
	
	public boolean delete(Course course){
		return list.deleteData(course);
	}
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}
	

	public LinkedList<Course> getList() {
		return list;
	}

	public String getCoursesNames() {
		String s="";
		for (int i = 0; i < list.getSize(); i++) {
			s=s+list.getIndexData(i).getId()+",";
		}
		return s;
	
	}

	public String getYear() {
		return year;
	}

	public void printCourses(){
		list.print();
	}
	@Override
	public String toString() {
		
		return "Name = " + name + ", id = " + id ;
	}

	
	@Override
	public int compareTo(Student data) {
		return this.getName().compareToIgnoreCase(data.getName());
	}
	
	
	
	

}
