package project_1;
import java.io.File;
import java.util.Scanner;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

	static LinkedList<Course> CoursesList;
	static LinkedList<Student> StudentsList;
	static LinkedList<Course> sortedYearList;
	static LinkedList<Student> sortedNameList;

	public static int getCoursIndex(String s){
		int counter=0;
		for (int i = 0; i < CoursesList.getSize(); i++) {
			if(CoursesList.getIndexData(i).getId().equalsIgnoreCase(s)){
				return counter;
			}
			++counter;
		}
		return -1;
	}

	public static void main(String[] args) {
		File f;
		Scanner in;
		String s="";
		
		//Reading Courses File

		try{
			f = new File("courses.txt");
			in= new Scanner(f);
			s += in.nextLine()+"$";
			while(in.hasNext()){
				s +=in.nextLine()+"$";
			}
		}
		catch (Exception e) {
			System.exit(0);
		}

		String []array = s.split("[$]");

		CoursesList =new LinkedList<>();

		for (int i = 0; i< array.length ; i++) {
			if(new Course(array[i]).isValid())
				CoursesList.add(new Course(array[i]));
		}
		

        // Reading Student File
		s="";
		try{
			f = new File("students.txt");
			in= new Scanner(f);
			s += in.nextLine()+"$";
			while(in.hasNext()){
				s +=in.nextLine()+"$";
			}
		}
		catch (Exception e) {
			System.exit(0);
		}

		String []array2 = s.split("[$]");

		StudentsList = new LinkedList<>();

		String []array3;
		int index;

		for (int i = 0; i < array2.length; i++) {
			array3 = array2[i].split("#");

			try{
				StudentsList.add(new Student(array3[0],Integer.parseInt(array3[1])));

				for(int j=2;j<array3.length;++j){
					
					index=getCoursIndex(array3[j]);
					
					if(index!=-1){

						if(StudentsList.getIndexData(StudentsList.getSize()-1).add(CoursesList.getIndexData(index)))
						CoursesList.getIndexData(index).add(StudentsList.getIndexData(StudentsList.getSize()-1));
					}
				}
			}
			catch (Exception e) {

			}
		}

		
		sortedNameList = new LinkedList<>();

		// This Loop merge the CoursesList to have a sorted linked list of all student on all courses based on Name
		
		for (int i = 0; i < CoursesList.getSize(); i++) {
			if(i==0 && i+1<=CoursesList.getSize()-1){
				sortedNameList = sortedNameList.merge(CoursesList.getIndexData(i).getList(),CoursesList.getIndexData(i+1).getList() );
				i=1;
			}
			else
				sortedNameList = sortedNameList.merge(sortedNameList,CoursesList.getIndexData(i).getList() );	
		}
		
		
 
        //This Loop sorted the Student in each Course based on ID
		
		for (int i = 0; i < CoursesList.getSize(); i++) {
			if(!(CoursesList.getIndexData(i).getList().isEmpty()))
				LinkedList.radixSortID(CoursesList.getIndexData(i).getList());
		}

		sortedYearList = new LinkedList<>();
		
		//Having all Courses in One Linked List
		for (int i = 0; i < CoursesList.getSize(); i++) {
			sortedYearList.add(CoursesList.getIndexData(i));
		}
		
        //Sorting all courses based on year
		LinkedList.radixSortCourses(sortedYearList);

		
		//Printing on File
		new PrintOnFile(CoursesList, sortedYearList, sortedNameList).print();


		launch(args);





	}
	@Override
	public void start(Stage primaryStage) throws Exception {

		new MainScene(CoursesList,StudentsList,sortedNameList,sortedYearList).show();

	}
	 
}
