/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mycode;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

/**
 *
 * @author Robin
 */
//Question 4
//Q4.1 - 1
//Class header correct
public class CandidatePaperManager
{

    //Q4.2 - 2
    //private array of candidate objects
    //size set to 20
    private Candidate cArr[] = new Candidate[20];

    //Question 6
    //Q6.1 - 2
    //array of ResearchPaper object created with correc size
    private ResearchPaper rpArr[] = new ResearchPaper[20];
    //rpSize variable created and initialize
    private int rpSize = 0;

    //Q4.3 - 9
    //constructor header correct
    public CandidatePaperManager()
    {
        try 
        {
            //open file for reading
            Scanner sc = new Scanner(new File("candidates.txt"));

            //loop through all 20 lines
            for (int i = 0; i < 20; i++)
            {
                //read the line
                String line = sc.nextLine();

                //split the line into the required parts
                String tokens[] = line.split("#");

                String sid = tokens[0];
                String name = tokens[1];
                int year = Integer.parseInt(tokens[2]);
                //create Candidate object
                //add to array 
                //correct index
                cArr[i] = new Candidate(sid, name, year);
            }
        } catch (FileNotFoundException fne)
        {
            System.out.println("File Missing " + fne);
            // error message
        }
    }

    //Q4.4 - 5
    //header correct
    public String displayAllCandidates()
    {
        String r = "";
        //loop through array
        for (int i = 0; i < cArr.length; i++)
        {
            //append to string
            //extra blank line
            r += cArr[i] + "\n";
        }

        return r; //appended string returned
    }

    //Q4.5 - 8
    //Header correct
    public void sortByYear()
    {
        //loop through the array
        for (int i = 0; i < cArr.length; i++)
        {
            //nested loop 
            //correct for sort used
            for (int j = i+1; j < cArr.length - 1; j++)
            {
                //compare the object years
                //correct objects referenced for sort
                //correct use of method
                if (cArr[i].getYear() < cArr[j ].getYear())
                {
                    //swopping elements

                    Candidate temp = cArr[i];

                    cArr[i] = cArr[j];

                    cArr[j] = temp;
                }
            }
        }
    }

    //Q6.2 - 11
    //header
    public String reviewResearchPapers()
    {
        //string to store the accepted and rejected
        String accepted = "Accepted\n";
        String rejected = "Rejected\n";

        try
        {
            //open file
            Scanner sc = new Scanner(new File("researchPapers.txt"));

            //read lines
            while (sc.hasNextLine())
            {
                //split tokens
                String line = sc.nextLine();
                String tokens[] = line.split("#");

                //Create researchPaper object
                ResearchPaper rp = new ResearchPaper(tokens[0], tokens[1], Boolean.parseBoolean(tokens[2]), tokens[3], tokens[4]);

                //check if it meets requirements
                //must call method
                if (rp.getMeetsDeptRequirements())
                {
                    //add to array
                    rpArr[rpSize] = rp;
                    // increase rpsize
                    rpSize++;
                    accepted += rp + "\n";
                } else
                {
                    rejected += rp + "\n";
                }
            }
        } catch (FileNotFoundException e)
        {
            System.out.println("File Missing" + e);
        }

        //return string with accepted and reject lists
        return accepted + "\n" + rejected;
    }

    //Q8.1 - 17
    //header
    public String doctoralCandidates()
    {

        //make date formatter
        DateTimeFormatter formatDate = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
        //format date
        String dateF = LocalDateTime.now().format(formatDate);

        String depts = "MatSci#BusCom#HumArt#LawMed";
        //separating departments
        Scanner scDepts = new Scanner(depts).useDelimiter("#");
        String currentDept = "", temp = dateF + "\n";
        //getting the list of department doctoral candidates    
        while (scDepts.hasNext())
        {
            currentDept = scDepts.next();
            temp = temp + currentDept + "\n";
            //loop through all research papers
            for (int i = 0; i < rpSize; i++)
            {
                //check if the paper matches the department
                if (rpArr[i].getDepartment().equals(currentDept))
                {
                    //find the research paper's author's name
                    Candidate candidate = findCandidate(rpArr[i].getStudentID());
                    // add to the department list
                    temp = temp + candidate.getName();
                    //check if on the dean's list
                    if (rpArr[i].onDeansList())
                    {
                        //add (D)
                        temp = temp + " (D)";
                    }
                    //add to concatenated list
                    //add extra line
                    temp = temp + "\n" + rpArr[i].getTitle() + "\n\n";
                }

            }

        }

       

        //Create file to write to with correct name
        try
        {
            PrintWriter outFile = new PrintWriter(new FileWriter("doctors.txt"));

            //save to file and close
            outFile.println(temp);

            outFile.close();
        } catch (Exception e)
        {
            System.out.println("Error writing to file " + e);
        }

        return temp;
    }

    public Candidate findCandidate(String sid)
    {
        Candidate c = null;
        //loop through candidates
        for (int i = 0; i < cArr.length; i++)
        {
            //check for matching studentid
            if (cArr[i].getStudentID().equalsIgnoreCase(sid))
            {
                c = cArr[i];
            }
        }

        return c;
    }

  

}
