package gdms;

import dao.DBManagement;
import error.GExcptFileIO;
import error.GExcptInit;
import error.GExcptSQL;
import net.sf.json.JSONObject;
import utils.Utils;

import java.util.*;

public class GDMS {
    static Map<String, Date> schedules;
    static String schedulePath = "";
    public static void init() throws GExcptInit {
        initSchedule();
        Timer timer = new Timer(true);
        for(Map.Entry<String, Date> schedule:schedules.entrySet()){
            timer.schedule(getTimerTask(schedule.getKey()),schedule.getValue());
        }
    }


    private static TimerTask getTimerTask(String s) throws GExcptInit {
        switch (s){
            case "teacher_choose_student_start_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("teacher_choose_student_status");
                        try {
                            DBManagement.updateAll("graduation_design",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }
                };
            case "student_choose_teacher_start_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("student_choose_teacher_status");
                        try {
                            DBManagement.updateAll("graduation_design",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }
                };
            case "choose_end_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("student_choose_teacher_status");
                        fields.add("teacher_choose_student_status");
                        try {
                            DBManagement.updateAll("graduation_design",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("student_fill_graduation_design_information_status");
                        try {
                            DBManagement.updateAll("graduation_design_information",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("opening_report_status");
                        try {
                            DBManagement.updateAll("graduation_design_opening_report",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("opening_report_mentor_opinion_status");
                        fields.add("opening_report_teacher_team_opinion_status");
                        fields.add("opening_report_college_opinion_status");
                        fields.add("opening_report_secretary_record_status");
                        try {
                            DBManagement.updateAll("graduation_design_opening_report_opinion_record",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }
                };
            case "opening_report_opinion_deadline_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("opening_report_mentor_opinion_status");
                        fields.add("opening_report_teacher_team_opinion_status");
                        fields.add("opening_report_college_opinion_status");
                        fields.add("opening_report_secretary_record_status");
                        try {
                            DBManagement.updateAll("graduation_design_opening_report_opinion_record",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("finished_product_status");
                        try {
                            DBManagement.updateAll("graduation_design_finished_product",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }
                };
            case "finished_product_deadline_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("finished_product_status");
                        try {
                            DBManagement.updateAll("graduation_design_finished_product",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("finished_product_mentor_score_status");
                        try {
                            DBManagement.updateAll("graduation_design_finished_product_mentor_score",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("finished_product_reviewer_score_status");
                        try {
                            DBManagement.updateAll("graduation_design_finished_product_reviewer_score",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }
                };
            case "finished_product_review_deadline_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("finished_product_mentor_score_status");
                        try {
                            DBManagement.updateAll("graduation_design_finished_product_mentor_score",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("finished_product_reviewer_score_status");
                        try {
                            DBManagement.updateAll("graduation_design_finished_product_reviewer_score",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                        fields = new ArrayList<>();
                        fields.add("reply_mentor_score_status");
                        fields.add("reply_teacher_team_score_status");
                        fields.add("reply_college_score_status");
                        fields.add("reply_secretary_record_status");
                        try {
                            DBManagement.updateAll("graduation_design_reply_opinion_record_score",fields,"1");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }

                };
            case "reply_opinion_deadline_date":
                return new TimerTask() {
                    @Override
                    public void run() {
                        List<String> fields = new ArrayList<>();
                        fields.add("reply_mentor_score_status");
                        fields.add("reply_teacher_team_score_status");
                        fields.add("reply_college_score_status");
                        fields.add("reply_secretary_record_status");
                        try {
                            DBManagement.updateAll("graduation_design_reply_opinion_record_score",fields,"4");
                        } catch (GExcptSQL gExcptSQL) {
                            gExcptSQL.printStackTrace();
                            System.exit(1);
                        }
                    }
                };
        }

        throw new GExcptInit("GlobalTimePoint.json error");
    }

    private static void initSchedule() {
        schedules = new HashMap<>();
        String sets= null;
        try {
            sets = Utils.readFile(schedulePath);
        } catch (GExcptFileIO gExcptFileIO) {
            gExcptFileIO.printStackTrace();
        }
        JSONObject jo=JSONObject.fromObject(sets);
        Iterator it = jo.keys();
        while (it.hasNext()) {
            String key = (String)it.next();
            Object value = jo.get(key);
            schedules.put(key, new utils.Utils.dateFormat((String) value));
        }
    }
}
