package core.process;
import core.user.User;
import dao.DBManagement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProcessManagement {
    public static Condition getCondition(User user) throws Exception {
        Map<String,String> status = getStatus(user);
        return getCondition(user,status);
    }
    public static Condition getCondition(User user, Map<String, String> status){
        Condition condition = new Condition();
        for(Map.Entry<String,String> entry:status.entrySet()){
            String[] s = entry.getKey().split("\\.");
            TempProcess tempProcess = (TempProcess) getProcess(s[1]);
            tempProcess.setGraduationDesignId(s[0]);
            tempProcess.setStatus(entry.getValue());
            condition.add(tempProcess);
        }
        condition.add(getLastProcesses(user));
        return condition;
    }

    private static List<Process> getLastProcesses(User user) {
        List<Process> lp  = new ArrayList<>();
        return lp;
    }

    //Map<graduation_design_id.x_status,status>
    public static Map<String,String> getStatus(User user) throws Exception {
        Map<String, String> status = new HashMap<>();
        String[][] userTables = DBManagement.getUserStatusTables(user.getType());
        String graduationDesignId = null;
        for(String[] s:userTables){
            String[] ti = s[0].split("\\.");
            String[] ts = s[1].split("\\.");
            List<String> ls = new ArrayList<>();
            ls.add("id");
            Map<String,String> limits = new HashMap<>();
            limits.put(ti[1],user.getId());
            List<String[]> resultLs = DBManagement.select(ls,ti[0],limits,0,1);
            if(resultLs.size()==0) continue;
            graduationDesignId = resultLs.get(0)[0];

            ls = new ArrayList<>();
            ls.add(ts[1]);
            limits = new HashMap<>();
            limits.put("id",graduationDesignId);
            resultLs = DBManagement.select(ls,ts[0],limits,0,1);
            status.put(graduationDesignId+"."+ts[1],resultLs.get(0)[0]);
        }
        return status;
    }
    public static Process getProcess(String process){
        switch (process) {
            case "teacher_choose_student_status":
                return new C_TeacherSelectStudent();
            case "student_choose_teacher_status":
                return  new C_StudentSelectTeacher();
            case"student_fill_graduation_design_information_status":
                return new C_StudentFillGraduationDesignInformation();
            case"opening_report_status":
                return new C_StudentUploadOpeningReport();
            case"opening_report_mentor_opinion_status":
                return new C_MentorFillOpeningReportOpinion();
            case"opening_report_teacher_team_opinion_status":
                return new C_TeacherTeamFillOpeningReportOpinion();
            case"opening_report_college_opinion_status":
                return new C_CollegeFillOpeningReportOpinion();
            case"opening_report_secretary_record_status":
                return new C_SecretaryFillOpeningReportRecord();
            case"finished_product_status":
                return new C_StudentUploadFinishedProduct();
            case"finished_product_mentor_score_status":
                return new C_MentorFillReview();
            case"finished_product_reviewer_score_status":
                return  new C_ReviewerFillReview();
            case"reply_ secretary_record_status":
                return  new C_SecretaryFillReplyRecord();
            case"reply_ teacher_team_score_status":
                return  new C_TeacherTeamFillScoreOpinion();
            case"reply_college_score_status":
                return  new C_CollegeFillScoreOpinion();
            case"reply_school_score_status":
                return  new C_SchoolFillScoreOpinion();
            default:
                return null;
        }
    }
}
