package course;

import common.DBTool;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class courseDB {
    public ArrayList<courseInfo> getAllCourses(){
        ArrayList<courseInfo> courseList = new ArrayList<>();
        try{
            DBTool db = new DBTool();
            PreparedStatement ps = db.getConn().prepareStatement("select * from t_course");
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt("N_COURSE_ID");
                String name = rs.getString("VC_COURSE_NAME");
                int type = rs.getInt("N_TYPE");
                float credits = rs.getFloat("F_CREDIT");
                int grade = rs.getInt("N_GRADE");
                int major = rs.getInt("N_MAJOR");
                String detail = rs.getString("VC_DETAIL");
                courseList.add(new courseInfo(id, name, type, credits, grade, major, detail));
            }
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return courseList;
    }

    public courseInfo getCourseInfo(int id){
        courseInfo courseInfo = null;
        try{
            DBTool db = new DBTool();
            PreparedStatement ps = db.getConn().prepareStatement("select * from t_course WHERE N_COURSE_ID = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                String name = rs.getString("VC_COURSE_NAME");
                int type = rs.getInt("N_TYPE");
                float credits = rs.getFloat("F_CREDIT");
                int grade = rs.getInt("N_GRADE");
                int major = rs.getInt("N_MAJOR");
                String detail = rs.getString("VC_DETAIL");
                courseInfo = new courseInfo(id, name, type, credits, grade, major, detail);
            }
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return courseInfo;
    }

    public int editCourse(courseInfo course){
        int flag;
        try{
            DBTool db = new DBTool();
            PreparedStatement ps = db.getConn().prepareStatement("UPDATE t_course SET VC_COURSE_NAME = ?,"+
                    " N_TYPE = ?, F_CREDIT = ?, N_GRADE = ?, N_MAJOR = ?, VC_DETAIL = ? WHERE N_COURSE_ID = ?");
            ps.setString(1, course.getName());
            ps.setInt(2, course.getType());
            ps.setFloat(3, course.getCredit());
            ps.setInt(4, course.getGrade());
            ps.setInt(5, course.getMajor());
            ps.setString(6, course.getDetail());
            ps.setInt(7, course.getId());
            flag = ps.executeUpdate();
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    public void insertCourse(courseInfo course){
        try{
            String sql = "INSERT INTO t_course (VC_COURSE_NAME, N_TYPE, F_CREDIT, N_GRADE, N_MAJOR, VC_DETAIL) VALUES(?, ?, ?, ?, ?, ?)";
            DBTool db = new DBTool();
            PreparedStatement ps = db.getConn().prepareStatement(sql);
            ps.setString(1, course.getName());
            ps.setInt(2, course.getType());
            ps.setFloat(3, course.getCredit());
            ps.setInt(4, course.getGrade());
            ps.setInt(5, course.getMajor());
            ps.setString(6, course.getDetail());
            ps.executeUpdate();
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void deleteCourse(int id){
        try{
            String sql = "DELETE FROM t_course WHERE N_COURSE_ID = ?";
            DBTool db = new DBTool();
            PreparedStatement ps = db.getConn().prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}
