package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Criterion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author Mark George <mark.george@otago.ac.nz>
 */
public class ScheduleDAO {
	private static final Logger logger = LoggerFactory.getLogger(ScheduleDAO.class);
	public List<String> getCategories() {
		final List<String> categories = new ArrayList<>();
		try {
			try (
				 Connection con = JdbcConnection.getConnection();  PreparedStatement stmt = con.prepareStatement("select category from category order by sort_order");  ResultSet rs = stmt.executeQuery()) {
				while (rs.next()) {
					String category = rs.getString("category");
					categories.add(category);
				}
			}
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage(), e);
		}
		return categories;
	}
	public List<Criterion> getCriteria(String category) {
		List<Criterion> criteria = new ArrayList<>();
		try {
			try (
				 Connection con = JdbcConnection.getConnection();  PreparedStatement stmt = con.prepareStatement("select * from criterion where category = ? order by sort_order");) {
				stmt.setString(1, category);
				ResultSet rs = stmt.executeQuery();
				criteria = loadCriteria(rs);
			}
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage(), e);
		}
		return criteria;
	}
	static List<Criterion> loadCriteria(ResultSet rs) {
		final List<Criterion> criteria = new ArrayList<>();
		try {
			while (rs.next()) {
				String criterion = rs.getString("criterion");
				String description = rs.getString("description");
				Integer min = rs.getInt("min");
				Integer max = rs.getInt("max");
				String editorType = rs.getString("editor_type");
				String criterionType = rs.getString("criterion_type");
				criteria.add(new Criterion(criterion, description, min, max, editorType, criterionType));
			}
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage(), e);
		}
		return criteria;
	}
	public String getAssessmentName() {
		try {
			try (
				 Connection con = JdbcConnection.getConnection();  PreparedStatement stmt = con.prepareStatement("select name from assessment");) {
				ResultSet rs = stmt.executeQuery();
				rs.next();
				String name = rs.getString("name");
				return name;
			}
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage(), e);
		}
		return "Assessment not defined";
	}
	public Integer getComplete() {
		try {
			try (
				 Connection con = JdbcConnection.getConnection();  PreparedStatement stmt = con.prepareStatement("select cast((round(cast ((select count(distinct submission_id) * 100 from result) as real)/ select count(submission_id) from submission)) as integer) as complete");) {
				ResultSet rs = stmt.executeQuery();
				rs.next();
				Integer complete = rs.getInt("complete");
				return complete;
			}
		} catch (SQLException e) {
			logger.error(e.getLocalizedMessage(), e);
		}
		return 0;
	}
}