package com.bizofficer.util.module;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bizofficer.apiweb.testdetail.ColumnRowsBean;
import com.bizofficer.apiweb.testdetail.ColumnsBean;
import com.bizofficer.apiweb.testdetail.QuestionSetBean;
import com.bizofficer.apiweb.testdetail.ResponseChoiceBean;
import com.bizofficer.module.questionfib.QuestionMapperBeanFIB;
import com.bizofficer.module.questionfib.XmlToJsonFIB;
import com.bizofficer.module.questionmc.QuestionMapperBeanMC;
import com.bizofficer.module.questionmc.ResponseLabelBeanMC;
import com.bizofficer.module.questionmc.XmlToJsonMC;
import com.bizofficer.module.questionmcmt.MaterialTxtBeanMCMT;
import com.bizofficer.module.questionmcmt.QuestionMapperBeanMCMT;
import com.bizofficer.module.questionmcmt.ResponseLabelBeanMCMT;
import com.bizofficer.module.questionmcmt.XmlToJsonMCMT;
import com.bizofficer.module.questionmr.QuestionMapperBeanMR;
import com.bizofficer.module.questionmr.ResponseLabelBeanMR;
import com.bizofficer.module.questionmr.XmlToJsonMR;
import com.bizofficer.module.questionmrmt.MaterialTxtBeanMRMT;
import com.bizofficer.module.questionmrmt.QuestionMapperBeanMRMT;
import com.bizofficer.module.questionmrmt.ResponseLabelBeanMRMT;
import com.bizofficer.module.questionmrmt.XmlToJsonMRMT;
import com.bizofficer.module.questiontf.QuestionMapperBeanTF;
import com.bizofficer.module.questiontf.ResponseLabelBeanTF;
import com.bizofficer.module.questiontf.XmlToJsonTF;


public class CreateQuestionBody {

	public QuestionSetBean getQuestionBody(BeanQuestion beanQuesObj)
    {
		BeanQuestion bqObj = (BeanQuestion) beanQuesObj;
		
		QuestionSetBean questionSetBeanObj = new QuestionSetBean();
        
        try{
        	
			List<ResponseChoiceBean> responseChoiceList = new ArrayList<ResponseChoiceBean>();
			List<ResponseLabelBeanMC> responseLabelList = new ArrayList<ResponseLabelBeanMC>();	
			List<ResponseLabelBeanMR> responseLabelListMR = new ArrayList<ResponseLabelBeanMR>();
			List<ResponseLabelBeanMCMT> responseLabelListMCMT = new ArrayList<ResponseLabelBeanMCMT>();
			List<ResponseLabelBeanMRMT> responseLabelListMRMT = new ArrayList<ResponseLabelBeanMRMT>();
			List<ResponseLabelBeanTF> responseLabelListTF = new ArrayList<ResponseLabelBeanTF>();
			List<ColumnsBean> columnsListMCMT = new ArrayList<ColumnsBean>();
			
			ResponseChoiceBean responseChoiceBeanObj;
			ColumnsBean columnsBeanObj = null;
			ColumnRowsBean columnRowsBeanObj = null;
			String shuffle="No";

        	
			questionSetBeanObj = new QuestionSetBean();
			questionSetBeanObj.setQuestionId(bqObj.getQuestionId());
			questionSetBeanObj.setItemId(bqObj.getItemId());
			questionSetBeanObj.setQuestionDisplayId(bqObj.getQuestionDisplayId());
			questionSetBeanObj.setQuestionType(bqObj.getQuestionType().replaceAll(" ", "-"));
			questionSetBeanObj.setQuestionTypeLabel(bqObj.getQuestionTypeLabel());
			questionSetBeanObj.setCommonDataDescription(bqObj.getCommonDataDescription());
			
			/**************************************************************************
			 * 		MULTIPLE CHOICE   
			 * ************************************************************************/
			if("Integer Type".equals(bqObj.getQuestionType()) || "Selection Type".equals(bqObj.getQuestionType()) || "Multiple Choice".equals(bqObj.getQuestionType()) || "Multiple Choice (5)".equals(bqObj.getQuestionType()) || "MC Assertion Reason".equals(bqObj.getQuestionType())  ) {

				QuestionMapperBeanMC questionObj = (QuestionMapperBeanMC) new XmlToJsonMC().parse(bqObj.getQuestionBody());
										
				///System.out.println("Mattext >> " + questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext() );

				questionSetBeanObj.setQuestionBody(questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext());
				
				shuffle = questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getShuffle();
				
				responseChoiceList = new ArrayList<ResponseChoiceBean>();
				responseLabelList =  (List<ResponseLabelBeanMC>) questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getResponse_label();
				
				if("Yes".equals(shuffle)) {
					Collections.shuffle(responseLabelList);
				}							
				for(int i=0; i<responseLabelList.size(); i++) {
					responseChoiceBeanObj = new ResponseChoiceBean();
					responseChoiceBeanObj.setResponseId(responseLabelList.get(i).getIdent());
					responseChoiceBeanObj.setResponseText(responseLabelList.get(i).getFlow_mat().getMaterial().getMattext());
					responseChoiceList.add(responseChoiceBeanObj);	
				}
				questionSetBeanObj.setResponseChoice(responseChoiceList);

			/**************************************************************************
			 * 		MULTIPLE RESPONSE   
			 * ************************************************************************/
			}else if("Multiple Response".equals(bqObj.getQuestionType())  ) {
				
				QuestionMapperBeanMR questionObj = (QuestionMapperBeanMR) new XmlToJsonMR().parse(bqObj.getQuestionBody());
				
				////System.out.println("Mattext >> " + questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext() );
				questionSetBeanObj.setQuestionBody(questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext());
				
				shuffle = questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getShuffle();
				
				responseChoiceList = new ArrayList<ResponseChoiceBean>();
				responseLabelListMR =  (List<ResponseLabelBeanMR>) questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getResponse_label();
				
				if("Yes".equals(shuffle)) {
					Collections.shuffle(responseLabelListMR);
				}							
				for(int i=0; i<responseLabelListMR.size(); i++) {
					responseChoiceBeanObj = new ResponseChoiceBean();
					responseChoiceBeanObj.setResponseId(responseLabelListMR.get(i).getIdent());
					responseChoiceBeanObj.setResponseText(responseLabelListMR.get(i).getFlow_mat().getMaterial().getMattext());
					responseChoiceList.add(responseChoiceBeanObj);	
				}
				questionSetBeanObj.setResponseChoice(responseChoiceList);
				
				/////System.out.println("ANS >> " + questionObj.getItem().getResprocessing().getRespcondition().get(0).getConditionvar().getVarequal().get(0).getContent() );

			/**************************************************************************
			 * 		MC Matching Type   
			 * ************************************************************************/
			}else if("MC Matching Type".equals(bqObj.getQuestionType())   ) {
					
					QuestionMapperBeanMCMT questionObj = (QuestionMapperBeanMCMT) new XmlToJsonMCMT().parse(bqObj.getQuestionBody());
					
					////System.out.println("Mattext >> " + questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext() );
					questionSetBeanObj.setQuestionBody(questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext());
					
					/************************
					 * COLUMNS START
					 ************************/
					columnsListMCMT = new ArrayList<ColumnsBean>();
					
					/************************
					 * LEFT COLUMN
					 ************************/
					columnsBeanObj = new ColumnsBean();								
					columnsBeanObj.setColumnHeader(questionObj.getItem().getPresentation().getFlow().getLeft_lid().getIdent());
					List<ColumnRowsBean> columnRowsListOne = new ArrayList<ColumnRowsBean>();
					List<MaterialTxtBeanMCMT> materialLeftList = questionObj.getItem().getPresentation().getFlow().getLeft_lid().getMaterial();
					if(materialLeftList!=null && materialLeftList.size()>0) {
						for(int i=0; i<materialLeftList.size(); i++) {
							columnRowsBeanObj = new ColumnRowsBean();
							columnRowsBeanObj.setRowId(materialLeftList.get(i).getIdent());
							columnRowsBeanObj.setRowName(materialLeftList.get(i).getMattext());
							columnRowsListOne.add(columnRowsBeanObj);
						}
					}								
					columnsBeanObj.setColumnRows(columnRowsListOne);								
					columnsListMCMT.add(columnsBeanObj);
					
					/************************
					 * RIGHT COLUMN
					 ************************/
					columnsBeanObj = new ColumnsBean();								
					columnsBeanObj.setColumnHeader(questionObj.getItem().getPresentation().getFlow().getRight_lid().getIdent());
					List<ColumnRowsBean> columnRowsListTwo = new ArrayList<ColumnRowsBean>();
					List<MaterialTxtBeanMCMT> materialRightList = questionObj.getItem().getPresentation().getFlow().getRight_lid().getMaterial();
					if(materialRightList!=null && materialRightList.size()>0) {
						for(int i=0; i<materialRightList.size(); i++) {
							columnRowsBeanObj = new ColumnRowsBean();
							columnRowsBeanObj.setRowId(materialRightList.get(i).getIdent());
							columnRowsBeanObj.setRowName(materialRightList.get(i).getMattext());
							columnRowsListTwo.add(columnRowsBeanObj);
						}
					}								
					columnsBeanObj.setColumnRows(columnRowsListTwo);								
					columnsListMCMT.add(columnsBeanObj);
					
					
					questionSetBeanObj.setColumns(columnsListMCMT);
					
					/************************
					 * COLUMNS END 
					 ************************/
					
					
					shuffle = questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getShuffle();
					
					responseChoiceList = new ArrayList<ResponseChoiceBean>();
					responseLabelListMCMT =  (List<ResponseLabelBeanMCMT>) questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getResponse_label();
					
					if("Yes".equals(shuffle)) {
						Collections.shuffle(responseLabelListMCMT);
					}
					for(int i=0; i<responseLabelListMCMT.size(); i++) {
						responseChoiceBeanObj = new ResponseChoiceBean();
						responseChoiceBeanObj.setResponseId(responseLabelListMCMT.get(i).getIdent());
						responseChoiceBeanObj.setResponseText(responseLabelListMCMT.get(i).getFlow_mat().getMaterial().getMattext());
						responseChoiceList.add(responseChoiceBeanObj);	
					}
					questionSetBeanObj.setResponseChoice(responseChoiceList);
					
					/////System.out.println("ANS >> " + questionObj.getItem().getResprocessing().getRespcondition().get(0).getConditionvar().getVarequal().get(0).getContent() );
				
			/**************************************************************************
			* 		MR Matching Type   
			*************************************************************************/
			}else if( "MR Matching Type".equals(bqObj.getQuestionType())  ) {
							
							QuestionMapperBeanMRMT questionObj = (QuestionMapperBeanMRMT) new XmlToJsonMRMT().parse(bqObj.getQuestionBody());
							
							////System.out.println("Mattext >> " + questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext() );
							questionSetBeanObj.setQuestionBody(questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext());
							
							/************************
							 * COLUMNS START
							 ************************/
							columnsListMCMT = new ArrayList<ColumnsBean>();
							
							/************************
							 * LEFT COLUMN
							 ************************/
							columnsBeanObj = new ColumnsBean();								
							columnsBeanObj.setColumnHeader(questionObj.getItem().getPresentation().getFlow().getMaterial().getLeft_lid().getIdent());
							List<ColumnRowsBean> columnRowsListOne = new ArrayList<ColumnRowsBean>();
							List<MaterialTxtBeanMRMT> materialLeftList = questionObj.getItem().getPresentation().getFlow().getMaterial().getLeft_lid().getMaterial();
							if(materialLeftList!=null && materialLeftList.size()>0) {
								for(int i=0; i<materialLeftList.size(); i++) {
									columnRowsBeanObj = new ColumnRowsBean();
									columnRowsBeanObj.setRowId(materialLeftList.get(i).getIdent());
									columnRowsBeanObj.setRowName(materialLeftList.get(i).getMattext());
									columnRowsListOne.add(columnRowsBeanObj);
								}
							}								
							columnsBeanObj.setColumnRows(columnRowsListOne);								
							columnsListMCMT.add(columnsBeanObj);
							
							/************************
							 * RIGHT COLUMN
							 ************************/
							columnsBeanObj = new ColumnsBean();								
							columnsBeanObj.setColumnHeader(questionObj.getItem().getPresentation().getFlow().getMaterial().getRight_lid().getIdent());
							List<ColumnRowsBean> columnRowsListTwo = new ArrayList<ColumnRowsBean>();
							List<MaterialTxtBeanMRMT> materialRightList = questionObj.getItem().getPresentation().getFlow().getMaterial().getRight_lid().getMaterial();
							if(materialRightList!=null && materialRightList.size()>0) {
								for(int i=0; i<materialRightList.size(); i++) {
									columnRowsBeanObj = new ColumnRowsBean();
									columnRowsBeanObj.setRowId(materialRightList.get(i).getIdent());
									columnRowsBeanObj.setRowName(materialRightList.get(i).getMattext());
									columnRowsListTwo.add(columnRowsBeanObj);
								}
							}								
							columnsBeanObj.setColumnRows(columnRowsListTwo);								
							columnsListMCMT.add(columnsBeanObj);
							
							
							questionSetBeanObj.setColumns(columnsListMCMT);
							
							/************************
							 * COLUMNS END 
							 ************************/
							
							
							shuffle = questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getShuffle();
							
							responseChoiceList = new ArrayList<ResponseChoiceBean>();
							responseLabelListMRMT =  (List<ResponseLabelBeanMRMT>) questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getResponse_label();
							
							if("Yes".equals(shuffle)) {
								Collections.shuffle(responseLabelListMRMT);
							}
							for(int i=0; i<responseLabelListMRMT.size(); i++) {
								responseChoiceBeanObj = new ResponseChoiceBean();
								responseChoiceBeanObj.setResponseId(responseLabelListMRMT.get(i).getIdent());
								responseChoiceBeanObj.setResponseText(responseLabelListMRMT.get(i).getFlow_mat().getMaterial().getMattext());
								responseChoiceList.add(responseChoiceBeanObj);	
							}
							questionSetBeanObj.setResponseChoice(responseChoiceList);
							
							/////System.out.println("ANS >> " + questionObj.getItem().getResprocessing().getRespcondition().get(0).getConditionvar().getVarequal().get(0).getContent() );

			/**************************************************************************
			* 		FILL IN BLANK   
			*************************************************************************/
			}else if("Fill in Blank".equals(bqObj.getQuestionType())   ) {

				QuestionMapperBeanFIB questionObj = (QuestionMapperBeanFIB) new XmlToJsonFIB().parse(bqObj.getQuestionBody());
										
				///System.out.println("Mattext >> " + questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext() );
				questionSetBeanObj.setQuestionBody(questionObj.getItem().getPresentation().getResponse_str().getRender_fib().getMaterial().get(0).getMattext());
				
			/**************************************************************************
			* 		TRUE FALSE   
			*************************************************************************/
			}else if("True False".equals(bqObj.getQuestionType())   ) {

				QuestionMapperBeanTF questionObj = (QuestionMapperBeanTF) new XmlToJsonTF().parse(bqObj.getQuestionBody());
											
				///System.out.println("Mattext >> " + questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext() );
				questionSetBeanObj.setQuestionBody(questionObj.getItem().getPresentation().getFlow().getMaterial().getMattext());

				shuffle = questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getShuffle();
				
				responseChoiceList = new ArrayList<ResponseChoiceBean>();
				responseLabelListTF =  (List<ResponseLabelBeanTF>) questionObj.getItem().getPresentation().getFlow().getResponse_lid().getRender_choice().getResponse_label();
				
				if("Yes".equals(shuffle)) {
					Collections.shuffle(responseLabelListTF);
				}							
				for(int i=0; i<responseLabelListTF.size(); i++) {
					responseChoiceBeanObj = new ResponseChoiceBean();
					responseChoiceBeanObj.setResponseId(responseLabelListTF.get(i).getIdent());
					responseChoiceBeanObj.setResponseText(responseLabelListTF.get(i).getFlow_mat().getMaterial().getMattext());
					responseChoiceList.add(responseChoiceBeanObj);	
				}
				questionSetBeanObj.setResponseChoice(responseChoiceList);
				
			}
			
			
			
  	    }catch(Exception ex){
    	   ex.printStackTrace();
   	    }

        
        return questionSetBeanObj;
        
  }

	
	
}
