package com.mingrisoft.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mingrisoft.actionForm.MoreSelect;
import com.mingrisoft.actionForm.QuestionsForm;
import com.mingrisoft.core.ChStr;
import com.mingrisoft.dao.StartExamDAO;

public class StartExam extends Action {
	private StartExamDAO startExamDAO = null;
	ChStr chStr = new ChStr();

	public StartExam() {
		this.startExamDAO = new StartExamDAO();
	}

		public ActionForward execute(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			String action = request.getParameter("action");				//获取action参数的值
			System.out.println("获取的action："+action);
			if ("startExam".equals(action)) {
				return startExam(mapping, form, request, response);		//随机抽取试题
			}else if("submitTestPaper".equals(action)){				//提交试卷
				return submitTestPaper(mapping,form,request,response);
			}else if("showStartTime".equals(action)){					//显示考试计时
				return showStartTime(mapping,form,request,response);
			}else if("showRemainTime".equals(action)){				//显示考试剩余时间
				return showRemainTime(mapping,form,request,response);
			}else{
				request.setAttribute("error", "操作失败！");			//将错误信息保存到error中
				return mapping.findForward("error");				//转到显示错误信息的页面
			}
		}

	// 计时
	private ActionForward showStartTime(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		String startTime = session.getAttribute("startTime").toString();
		long a = Long.parseLong(startTime);
		long b = new java.util.Date().getTime();
		int h = (int) Math.abs((b - a) / 3600000);
		String hour = chStr.formatNO(h, 2);
		int m = (int) (b - a) % 3600000 / 60000;
		String minute = chStr.formatNO(m, 2);
		int s = (int) ((b - a) % 3600000) % 60000 / 1000;
		String second = chStr.formatNO(s, 2);
		String time = hour + ":" + minute + ":" + second;
		request.setAttribute("showStartTime", time);
		return mapping.findForward("showStartTime");
	}

	// 计算剩余时间
	private ActionForward showRemainTime(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession();
		String startTime = session.getAttribute("startTime").toString();
		long a = Long.parseLong(startTime);
		long b = new java.util.Date().getTime();
		long r = 20 * 60000 - (b - a - 1000);
		int h = (int) Math.abs(r / 3600000);
		String hour = chStr.formatNO(h, 2);
		int m = (int) (r) % 3600000 / 60000;
		String minute = chStr.formatNO(m, 2);
		int s = (int) ((r) % 3600000) % 60000 / 1000;
		String second = chStr.formatNO(s, 2);
		String time = hour + ":" + minute + ":" + second;
		request.setAttribute("showRemainTime", time);
		return mapping.findForward("showRemainTime");
	}

	// 获取考试题目信息
	private ActionForward startExam(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		if(session.getAttribute("student")==null || session.getAttribute("student").equals("")){	//判断准考证号是否为空
			return mapping.findForward("dealNull");								//转到前台登录页面
		}else{
			String student=session.getAttribute("student").toString();					//从Session中获取准考证号
			if(session.getAttribute("lessonID")==null || session.getAttribute("lessonID").equals("")){
				return mapping.findForward("dealNull");							//转到前台登录页面
			}else{
				int lessonID=Integer.parseInt(session.getAttribute("lessonID").toString());	//获取课程ID
				int questions=startExamDAO.randomGetQuestion(lessonID);			//随机抽取试题
				int ret=startExamDAO.startSaveResult(student,lessonID);			//开始考试时保存考试成绩
				List singleQue=(List)startExamDAO.queryExam(questions,0);			//获取单选试题
				QuestionsForm q=(QuestionsForm)form;			//将接收的表单信息强制转换成ActionForm类型
				q.setSize(singleQue.size());
				request.setAttribute("singleQue",singleQue);						//保存单选题到singleQue中
				List moreQue=(List)startExamDAO.queryExam(questions,1);			//获取多选题
				q.setMoreSize(moreQue.size());
				request.setAttribute("moreQue",moreQue); 						//保存多选题到moreQue中
				session.setAttribute("startTime",new java.util.Date().getTime());
				return mapping.findForward("testPaper");							//转到显示试题页面
			}
		}
	}

	// 提交试卷
	private ActionForward submitTestPaper(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		QuestionsForm q=(QuestionsForm)form;		//将接收的表单信息强制转换成ActionForm类型
		String rightAnswer="";										//定义保存正确答案的变量
		float singleMark=0;										//定义保存单选题分数的变量
		float moreMark=0; 										//定义保存多选题分数的变量
		/****************************统计单选题的得分**************************************/
		String[] single=q.getAnswerArrS();							//获取考生提交的单选题答案
		int[] singleId=q.getIdArrS();									//获取单选题的试题ID，并保存到数组中
		float markS=40/(single.length);								//计算多个单选题的分数
		for(int i=0;i<single.length;i++){
			rightAnswer=startExamDAO.getRightAnswer(singleId[i]);	//调用getRightAnswer()方法获取正确答案
			if(rightAnswer.equals(single[i])){
				singleMark=singleMark+markS;					//累加单选题的分数
			}
		}
		/*********************************************************************************/
		/****************************统计多选题的得分**************************************/
		MoreSelect[] more=q.getMoreSelect();							//获取考生提交的多选题答案
		float markM=60/(more.length); 								//计算每个多选题的分数
		String str="";												//定义用于记录多选题答案的字符串
		for(int i=0;i<more.length;i++){
			String[] ans=more[i].getAnswerArr();					//获取考生提交的指定多选题的答案
			int[] moreId=q.getIdArrM();								//获取多选题的试题ID，并保存到数组中
			rightAnswer=startExamDAO.getRightAnswer(moreId[i]);	//获取考生提交的指定多选题的答案
			for(int j=0;j<ans.length;j++){
				if(ans[j]!=null) str=str+ans[j]+",";					//将获取的答案连接成由逗号分隔的字符串
	 		}
	 		if(str.length()>1){
	 			str=str.substring(0,str.length()-1);					//去掉生成字符串中的最后一个逗号
	 		}
			if(rightAnswer.equals(str)){
				moreMark=moreMark+markM;					//累加多选题的分数
			}	
			str="";												//清空字符串str
		}
		/*********************************************************************************/
		HttpSession session = request.getSession();
		String student=session.getAttribute("student").toString();				//获取准考证号
		int lessonID=Integer.parseInt(session.getAttribute("lessonID").toString());	//获取课程ID
		int ret=startExamDAO.saveResult(student,lessonID,(int)Math.round(singleMark),
				(int)Math.round(moreMark));
		if(ret>0){
			request.setAttribute("submitTestPaperok", "试卷已提交，您本次考试的成绩为："+(Math.round(singleMark)+Math.round(moreMark))+"分！");
			return mapping.findForward("submitTestPaperok");				//转到显示考试成绩页面
		}else{
			return mapping.findForward("dealNull");						//转到错误处理页面
		}
	}


}
