package backend.dao;
import backend.model.Questions;
import backend.model.TestRecord;
import backend.utils.DBConnectionPool;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class QuestionsDao {
    List<Questions> commonResultSet(ResultSet rs) throws SQLException{
        List<Questions> questionPoList = new ArrayList<>();
        while(rs.next()){
            Questions questions = new Questions();
            questions.setId(rs.getInt("id"));
            questions.setName(rs.getString("name"));
            questions.setDescription(rs.getString("description"));
            questions.setAnswer(rs.getString("answer"));
            questions.setFrequency(rs.getInt("frequency"));
            questionPoList.add(questions);
        }
        return questionPoList;
    }
    public List<Questions> getAll() {
        List<Questions> questionPoList = new ArrayList<>();
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,name,description,answer,frequency from questions";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            questionPoList=commonResultSet(rs);
            rs.close();
            stmt.close();

        }catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        }finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return questionPoList;
    }

    public Questions getById(Integer id) {
        Connection conn = DBConnectionPool.getConnection();
        if (conn == null)
            return null;

        Questions questionPo = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            stmt = conn.prepareStatement("select id,name,description,answer,frequency from questions where id = ?");
            stmt.setInt(1, id);
            rs = stmt.executeQuery();

            if (rs.next()) {  // 检查是否有数据
                questionPo = new Questions();
                questionPo.setId(rs.getInt("id"));
                questionPo.setName(rs.getString("name"));
                questionPo.setDescription(rs.getString("description"));
                questionPo.setAnswer(rs.getString("answer"));
                questionPo.setFrequency(rs.getInt("frequency"));
            }
        } catch (SQLException e) {
            System.out.println("数据库异常：" + e.getMessage());
        } finally {
            // 释放资源
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                System.out.println("资源释放异常：" + e.getMessage());
            }
            DBConnectionPool.releaseConnection(conn);
        }
        return questionPo;
    }

    public List<Questions> getByCondition(Integer frequency,String key) {
        List<Questions> questionPoList = null;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,name,description,answer,frequency from questions where name like ?";
        if(frequency != null)
            sql += " and frequency = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, "%"+key+"%");
            if(frequency != null)
                stmt.setInt(2, frequency);
            ResultSet rs = stmt.executeQuery();
            questionPoList =commonResultSet(rs);
            rs.close();
            stmt.close();

        }catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        }finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return questionPoList;
    }

    public boolean insert(Questions question){
        int row = 0;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return false;
        //2. 创建Statement对象
        String sql = "insert into questions(name,description,answer,frequency) values(?,?,?,?)";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, question.getName());
            stmt.setString(2, question.getDescription());
            stmt.setString(3, question.getAnswer());
            if(question.getFrequency()==null)
                stmt.setInt(4, 0);
            else
                stmt.setInt(4, question.getFrequency());
            row = stmt.executeUpdate();
            stmt.close();

        }catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        }finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return row > 0;
    }
    public boolean delete(Integer id){
        int row = 0;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return false;
        //2. 创建Statement对象
        String sql = "delete from questions where id = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            row = stmt.executeUpdate();
            stmt.close();

        }catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        }finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return row > 0;
    }
    public boolean update(Questions question){
        int row = 0;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return false;
        //2. 创建Statement对象
        String sql = "update questions set name = ?,description = ?,answer = ?,frequency=? where id = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, question.getName());
            stmt.setString(2, question.getDescription());
            stmt.setString(3, question.getAnswer());
            if(question.getFrequency()==null)
                stmt.setInt(4, 0);
            else
                stmt.setInt(4, question.getFrequency());
            stmt.setInt(5, question.getId());
            row = stmt.executeUpdate();
            stmt.close();

        }catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        }finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return row > 0;

    }

    public Questions getRandomQuestion() {
        List<Questions> questionPoList = getAll();
        Random random = new Random();
        //生成随机索引
        int index = random.nextInt(questionPoList.size());
        return questionPoList.get(index);
    }

    public Questions getByName(String name) {
        Questions questionPo = null;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,name,description,answer,frequency from questions where name = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();
            questionPo = commonResultSet(rs).get(0);
            rs.close();
            stmt.close();
        } catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        } finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return questionPo;
    }

//    public static void main(String[] args) {
//        WordDao wordDao = new WordDao();
//        //查询
//        List<WordPo> wordPoList = wordDao.getByCondition(3,"v");
//        if(wordPoList != null)
//            for(WordPo wordPo:wordPoList)
//                System.out.println(wordPo.getWord());
//        //增加
//        wordDao.insert(new WordPo("test", "test", "test", 1));
//        //更新
//        wordDao.updata(new WordPo(1, 1, "test", "test", "test"));
//    }
}
