/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.assessment;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.bodington.assessment.QuestionnaireFacility.QuestionnaireRenderer;
import org.bodington.server.BuildingContext;
import org.bodington.server.BuildingServerException;
import org.bodington.server.realm.User;
import org.bodington.servlet.Request;

/**
 * Class for rendering questionnaires in the <em>analysis</em> state. This
 * class is used to render the complete set of user responses to a questionnaire
 * for the purposes of analysis (or summarising).
 * @author Alexis O'Connor
 */
class AnalysisQuestionnaireRenderer extends AbstractQuestionnaireRenderer
    {
    private QuestionnaireAnalysis analysis;
    
    public void paperToHTML( Request request, PrintWriter out,
        QuestionnaireSession session ) throws IOException,
        BuildingServerException
        {
        if ( !session.isReviewable() )
            {
            out.println( "<P><strong>The results can not currently be "
                + "reviewed.</strong> The most likely reason for this is that "
                + "the questionnaire is a partially anonymous one and the "
                + "submission deadline has not yet been reached.</P>" );
            return;
            }
        
        List questions_in_order = session.getQuestionnaireQuestionsInOrder();

        analysis = new QuestionnaireAnalysis();
        session.analyse( null, analysis );

        for ( int i = 0; i < questions_in_order.size(); i++ )
            {
            QuestionnaireQuestion question 
                = (QuestionnaireQuestion)questions_in_order.get( i );

            String identifier = "Q" + question.getQuestionnaireQuestionId();
            out.println( "<HR><TABLE id=" + identifier
                + "><TR><TD COLSPAN=3><H4>Question " + (i + 1)
                + "</H4></TD></TR>" );

            questionToHTML( question, null, out );

            out.println( "</TABLE>" );
            }

        if ( questions_in_order.size() == 0 )
            out.println( "<P>There are currently no questions in this "
                + "questionnaire.</P>" );
        }

    public void questionToHTML( QuestionnaireQuestion question,
        QuestionnaireResponse response, PrintWriter out ) throws IOException,
        BuildingServerException
        {
        out.println( "<TR><TD COLSPAN=3>" );
        out.println( question.getIntroduction() );
        out.println( "</TD></TR>" );

        int[] indices = question.getValidStatementIndices();
        int[] counts = new int[indices.length];
        int total = 0;
        int max = 0;
        for ( int i = 0; i < indices.length; i++ )
            {
            char letter = (char)('A' + i);
            out.println( "<TR><TD VALIGN=TOP WIDTH=20><B>" );
            out.println( letter );
            out.println( "</B></TD>" );
            counts[i] = analysis.getItemCounts( 
                question.getQuestionnaireQuestionId(), i );
            out.print( "<TD WIDTH=20>" );
            out.print( counts[i] );
            out.print( "</TD>" );
            total += counts[i];
            max = counts[i] > max ? counts[i] : max;
            out.println( "<TD VALIGN=TOP WIDTH=1000>" );
            out.println( question.getStatement( indices[i] ) );
            out.println( "</TD></TR>" );
            Vector comments = analysis.getComments( 
                question.getQuestionnaireQuestionId(), i );
            for ( int j = 0; j < comments.size(); j++ )
                {
                out.print( "<TR><TD COLSPAN=2></TD><TD><BLOCKQUOTE>" );
                out.print( (String)comments.elementAt( j ) );
                out.println( "</BLOCKQUOTE></TD></TR>" );
                }
            }

        if ( question.isStandardQuestion() )
            {
            max = (counts[0] + counts[1] > max) ? counts[0] + counts[1] : max;
            max = (counts[3] + counts[4] > max) ? counts[3] + counts[4] : max;
            }

        double scale = (max < 10) ? 25.0 : 250.0 / max;
        out.println( "<TR><TD COLSPAN=3>" );
        if ( question.isStandardQuestion() )
            {
            out.println( "<TABLE BORDER><TR><TD WIDTH=75>Agree</TD>" );
            if ( (int)(scale * counts[1]) > 0 )
                {
                out.println( "<TD BGCOLOR=GREEN WIDTH="
                    + (int)(scale * counts[1]) + ">.</TD>" );
                }
            if ( (int)(scale * counts[0]) > 0 )
                {
                out.println( "<TD BGCOLOR=RED WIDTH="
                    + (int)(scale * counts[0]) + ">.</TD>" );
                }
            out.println( "</TR></TABLE>" );
            out.println( "<TABLE BORDER><TR><TD WIDTH=75>Neutral</TD>" );
            if ( (int)(scale * counts[2]) > 0 )
                {
                out.println( "<TD BGCOLOR=GREEN WIDTH="
                    + (int)(scale * counts[2]) + ">.</TD>" );
                }
            out.println( "</TR></TABLE>" );
            out.println( "<TABLE BORDER><TR><TD WIDTH=75>Disagree</TD>" );
            if ( (int)(scale * counts[3]) > 0 )
                {
                out.println( "<TD BGCOLOR=GREEN WIDTH="
                    + (int)(scale * counts[3]) + ">.</TD>" );
                }
            if ( (int)(scale * counts[4]) > 0 )
                {
                out.println( "<TD BGCOLOR=RED WIDTH="
                    + (int)(scale * counts[4]) + ">.</TD>" );
                }
            out.println( "</TR></TABLE>" );
            }
        else
            {
            for ( int i = 0; i < indices.length; i++ )
                {
                char letter = (char)('A' + i);
                out.println( "<TABLE BORDER><TR><TD WIDTH=30><B>" + letter
                    + "</B></TD>" );
                if ( (int)(scale * counts[i]) > 0 )
                    {
                    out.println( "<TD BGCOLOR=GREEN WIDTH="
                        + (int)(scale * counts[i]) + ">.</TD>" );
                    }
                out.println( "</TR></TABLE>" );
                }
            }
        out.println( "</TD></TR>" );

        Vector comments = analysis.getComments( 
            question.getQuestionnaireQuestionId(), 10 );
        for ( int j = 0; j < comments.size(); j++ )
            {
            if ( indices.length > 0 && j == 0 )
                {
                String commentsText = question.isMultipleResponse()
                    ? "These are the comments people made."
                    : "Comments from people who didn't select an option.";                
                out.print( "<TR><TD COLSPAN=3>" + commentsText + "</TD></TR>" );
                }

            out.print( "<TR><TD COLSPAN=2></TD><TD><BLOCKQUOTE>" );
            out.print( (String)comments.elementAt( j ) );
            out.println( "</BLOCKQUOTE></TD></TR>" );
            }
        }
    }
