<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<?xml version="1.0" ?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<% response.setContentType("text/vnd.wap.wml"); %>

<%
  Class.forName("org.hsqldb.jdbcDriver");
  Connection c = 
    DriverManager.getConnection ("jdbc:hsqldb:/Users/juntao/Java/tomcat/jakarta-tomcat-4.1.24/webapps/Trivia/database/testdb", "sa", "");
  c.setAutoCommit(true);
  Statement stmt = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                     ResultSet.CONCUR_READ_ONLY);
  ResultSet rs;

  HttpSession sess = request.getSession(true);
  String [] questionIds = (String []) sess.getAttribute("questionIds");
  String [] answers = 
   (String []) sess.getAttribute("answers");
  if (questionIds == null) {
    rs = 
      stmt.executeQuery("select id from Questions where category='wap'");
    int count = 0;
    int i = 0;
    while (rs.next()) count++;
    questionIds = new String [count];
    answers = new String [count];
    for (i = 0; i < count; i++) {
      questionIds[i] = null;
      answers[i] = null;
    }
    rs.beforeFirst();
    i = 0;
    while (rs.next()) {
      questionIds[i] = rs.getString(1);
      i++;
    }
    sess.setAttribute("questionIds", questionIds);
    sess.setAttribute("answers", answers);
  }
  
  for (int i = 0; i < questionIds.length; i++) {
    if ( questionIds[i].equals(request.getParameter("questionId")) ) {
      answers[i] = request.getParameter("answerId");
      break;
    }
  }
  
  int pos = 0;
  for (pos = 0; pos < questionIds.length; pos++) {
    if (answers[pos] == null) break;
  }
  
  if ( pos < questionIds.length ) {
    // Present the next question
    String questionId = questionIds[pos];
    rs = stmt.executeQuery("select question from Questions where id=" + questionId);
    rs.next();
    String question = rs.getString(1);
    rs = stmt.executeQuery("select choiceId, choice from Choices where questionId=" + questionId + " order by choiceId ASC");
%>

<wml>
  <card id="question" title="question">
  <p><%=question%> (Check out the picture <a href="#imagecard">here</a></p>
<%
    while (rs.next()) {
      String choiceId = rs.getString(1);
      String choice = rs.getString(2);
%>

  <p><a href="trivia.jsp?questionId=<%=questionId%>&answerId=<%=choiceId%>"><%=choiceId%>  <%=choice%></a></p>
<%
    }
%>
  </card>
  <card id="imagecard" title="image">
  <p><img src="../database/gif/<%=questionId%>.gif" alt=" " /></p>
  </card>
</wml>

<%
  } else {
    // Show results
    int correctNum = 0;
    PreparedStatement pstmt = c.prepareStatement("select id from Questions where id=? and answerId=?");
    for (int i = 0; i < questionIds.length; i++) {
      pstmt.setString(1, questionIds[i]);
      pstmt.setString(2, answers[i]);
      rs = pstmt.executeQuery();
      if (rs.next()) correctNum++;
    }
    pstmt.close();
%>

<wml>
  <card id="result" title="result">
  <p>You got <%=correctNum%> of correct answers out of <%=questionIds.length%> questions</p>
  </card>
</wml>

<%
  }
  stmt.close();
  c.close();
%>
