/*
Copyright (c) 2007 Matt Williams matt@milliams.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License or (at
your option) version 3 or, at the discretion of KDE e.V. (which shall
act as a proxy as in section 14 of the GPLv3), any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "kqtideliveryengine.h"

#include "kqtitestpart.h"
#include "kqtiassessmenttest.h"
#include "kqtiassessmentsection.h"
#include "kqtiassessmentitemref.h"
#include "kqtiassessmentitem.h"
#include "kqtisectionpart.h"
#include "kqtiitemsession.h"
#include "kqtiitemresult.h"
#include "kqtiresponsevariable.h"

KQtiDeliveryEngine::KQtiDeliveryEngine()
{
    
}

KQtiDeliveryEngine::~KQtiDeliveryEngine()
{
    //Delete the KQtiItemSessions
}

void KQtiDeliveryEngine::setAssessmentTest(KQtiAssessmentTest* assessmentTest)
{
    mAssessmentTest = assessmentTest;
    
    foreach(KQtiTestPart* testPart, mAssessmentTest->testParts())
    {
        foreach(KQtiAssessmentSection* baseAssessmentSection, testPart->assessmentSections())
        {
            foreach(KQtiSectionPart* sectionPart, baseAssessmentSection->sectionParts())
            {
                if(dynamic_cast<KQtiAssessmentItemRef*>(sectionPart)) //If it's an item
                {
                    //create KQtiItemSession
                    KQtiAssessmentItem* assessmentItem = dynamic_cast<KQtiAssessmentItemRef*>(sectionPart)->item();
                    KQtiItemSession* itemSession = new KQtiItemSession(assessmentItem);
                    mItemSessions.insert(assessmentItem, itemSession);
                    mCurrentAssessmentItem = assessmentItem;
                    
                    //BEGIN Attempt
                    itemSession->setStatus(KQtiItemSession::Initial);
                    
                    //BEGIN Candidate Session
                    itemSession->setStatus(KQtiItemSession::Interacting);
                    
                    emit(itemBody(assessmentItem->itemBody()));
                }
                else
                {
                    //if it's another section, do nothing. i.e. move on to next.
                }
            }
        }
    }
}

void KQtiDeliveryEngine::itemResult(KQtiItemResult* itemResult)
{
    qDebug("received itemResult");
    
    //TODO If modal feedback need, do it here
    
    //Otherwise:
    //END Candidate Session
    mItemSessions.value(mCurrentAssessmentItem)->setStatus(KQtiItemSession::Suspended);
    mItemSessions.value(mCurrentAssessmentItem)->setItemResult(itemResult);
}

#include "kqtideliveryengine.moc"
