package dynamic.degrees;

import dynamic.common.*;

import java.io.IOException;
import java.util.ArrayList;

public class DegreeInfoGetter {

    static String DB_URL = CommonConfig.getDatabaseURL() + "/viewtab?";

    static String DEGREE_URL = DB_URL + "table=Degree&arg0=%25&arg1=&arg2=&arg3=&arg4=&arg5=&arg6=";
    static String CLASS_URL = DB_URL + "table=Class&arg0=%25&arg1=&arg2=&arg3=&arg4=&arg5=&arg6=";
    static String COURSE_URL = DB_URL + "table=Course&arg0=%25&arg1=&arg2=&arg3=&arg4=&arg5=&arg6=";
    static String XCOURSECLASS_URL = DB_URL + "table=xCourseClass&arg0=%25&arg1=&arg2=&arg3=&arg4=&arg5=&arg6=";

    static TeachdbReport degrees = new TeachdbReport(DEGREE_URL);
    static TeachdbReport classes = new TeachdbReport(CLASS_URL);
    static TeachdbReport courses = new TeachdbReport(COURSE_URL);
    static TeachdbReport xcourseclass = new TeachdbReport(XCOURSECLASS_URL);

    public static ArrayList<String> GetDegreeList() throws IOException {
        return degrees.getAllData("Code");
    }

    public static ArrayList<Integer> GetDegreeYears(String degree_code) {
        classes.filter("Degree", degree_code);
        ArrayList<Integer> yrs = new ArrayList<Integer>();
        for (String yr : classes.getAllData("Yr")) {
            yrs.add(Integer.parseInt(yr));
        }
        classes.filterClear();
        return yrs;
    }

    static String GetDegreeYearCode(String degree_code, int year) {
        classes.filter("Degree",degree_code);
        classes.filter("Yr","" + year);
        String code = classes.getDataAt("Degree<br>Yr", 0);
        classes.filterClear();
        return code;
    }

    private static String getCourseTerm(String course_id) {
        return courses.lookupData("Code",course_id,"Term");
    }

    private static ArrayList<ArrayList<String>> GetCoursesByCompulsion(String degree_code, int year, String compulsion) throws IOException {

        String year_code = GetDegreeYearCode(degree_code,year);
        xcourseclass.filter("Degree<br>Yr", year_code);
        xcourseclass.filter("Required", compulsion);

        ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
        for (int i = 0; i < xcourseclass.getRowCount(); i++) {

            ArrayList<String> c_info = new ArrayList<String>();
            String course_id = xcourseclass.getDataAt("Course", i);
            String course_title = xcourseclass.getDataAt("Title", i);

            course_title += DegreeNameAddendum.addSuperscripts(degree_code, "" + year, course_id);

            String course_term = getCourseTerm(course_id);

            c_info.add(course_id);
            c_info.add(course_title);
            c_info.add(course_term);

            res.add(c_info);
        }
        xcourseclass.filterClear();
        return res;
    }

    public static ArrayList<ArrayList<String>> GetCompulsoryCourses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Required");
    }

    public static ArrayList<ArrayList<String>> GetOptionalCourses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Optional");
    }

    public static ArrayList<ArrayList<String>> GetOptional1Courses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Optional1");
    }

    public static ArrayList<ArrayList<String>> GetOptional2Courses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Optional2");
    }

    public static ArrayList<ArrayList<String>> GetOptional3Courses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Optional3");
    }

//    public static ArrayList<ArrayList<String>> GetAllOptionalCourses(String degree_code, int year) throws IOException {
//        ArrayList<ArrayList<String>> all = new ArrayList<ArrayList<String>>();
//        all.addAll(GetOptionalCourses(degree_code, year));
//        all.addAll(GetOptional1Courses(degree_code, year));
//        all.addAll(GetOptional2Courses(degree_code, year));
//        ArrayListHelper.SortBasedOnFirstElement(all);
//        return all;
//    }


    public static ArrayList<ArrayList<String>> GetSelectiveCourses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Selective");
    }

    public static ArrayList<ArrayList<String>> GetSelective1Courses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Selective1");
    }

    public static ArrayList<ArrayList<String>> GetSelective2Courses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Selective2");
    }

    public static ArrayList<ArrayList<String>> GetSelective3Courses(String degree_code, int year) throws IOException {
        return GetCoursesByCompulsion(degree_code, year, "Selective3");
    }

//    public static ArrayList<ArrayList<String>> GetAllSelectiveCourses(String degree_code, int year) throws IOException {
//        ArrayList<ArrayList<String>> all = new ArrayList<ArrayList<String>>();
//        all.addAll(GetSelectiveCourses(degree_code, year));
//        all.addAll(GetSelective1Courses(degree_code, year));
//        all.addAll(GetSelective2Courses(degree_code, year));
//        ArrayListHelper.SortBasedOnFirstElement(all);
//        return all;
//    }


}
