/*
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

#include "msgconversationviewitem.h"

// SYSTEM INCLUDES
#include <QDateTime>
#include "debugtraces.h"
#include <QDir>
#include <QChar>
#include <HbTextItem>
#include <HbIconItem>
#include <HbIconAnimationManager>
#include <HbIconAnimator>

// USER INCLUDES
#include "msgconversationwidget.h"
#include "msgviewdefines.h"
#include "msgviewutils.h"
#include "convergedmessage.h"
#include "conversationsenginedefines.h"

// LOCAL CONSTANTS
const QString MSG_OUTBOX_ICON("qtg_small_outbox");
const QString MSG_FAIL_ICON("qtg_small_fail");
const QString ANIMATION_FILE(":/qtg_anim_loading.axml");
const QString ANIMATION_ICON_NAME("qtg_anim_loading");
const QString VCARD_ICON("qtg_large_mycard");

// LOCALIZATION
#define LOC_RINGING_TONE hbTrId("txt_messaging_dpopinfo_ringing_tone")
#define LOC_BUSINESS_CARD hbTrId("txt_messaging_list_business_card")
#define LOC_CALENDAR_EVENT hbTrId("txt_messaging_list_calendar_event")
#define LOC_UNSUPPORTED_MSG_TYPE hbTrId("txt_messaging_list_listview_unsupported_message_type")
#define LOC_RESEND_AT hbTrId("Resend at ")

//---------------------------------------------------------------
// MsgConversationViewItem::MsgConversationViewItem
// Constructor
//---------------------------------------------------------------
MsgConversationViewItem::MsgConversationViewItem(QGraphicsItem* parent) :
    HbListViewItem(parent), mIncoming(false), mConversation(0),
            mMessageStateIconItem(0)
{
    mConversation = new MsgConversationWidget(this);
    HbStyle::setItemName(mConversation, "msgconvwidget");

    mMessageStateIconItem = new HbIconItem(this);
    HbStyle::setItemName(mMessageStateIconItem, "msgStateIcon");
}

//---------------------------------------------------------------
// MsgConversationViewItem::MsgConversationViewItem
// Destructor
//---------------------------------------------------------------
MsgConversationViewItem::~MsgConversationViewItem()
{

}

//---------------------------------------------------------------
// MsgConversationViewItem::createItem
// Create a new decorator item.
//---------------------------------------------------------------
MsgConversationViewItem* MsgConversationViewItem::createItem()
{
    return new MsgConversationViewItem();
}

//---------------------------------------------------------------
// MsgConversationViewItem::updateChildItems
//
//---------------------------------------------------------------
void MsgConversationViewItem::updateChildItems()
{
    QModelIndex index = modelIndex();

#ifdef _DEBUG_TRACES_
    qCritical() << "START MsgConversationViewItem::updateChildItems: "
            << index.data(ConvergedMsgId).toInt();
#endif

    // Create items common to each type of message

    QString bodyText = index.data(BodyText).toString();
    int messageType = index.data(MessageType).toInt();
    int messageSubType = index.data(MessageSubType).toInt();

    // Set message properties common to SMS/MMS/IM etc..

    int direction = index.data(Direction).toInt();

    if ( direction == ConvergedMessage::Incoming)
    {
        setIncoming(true);
        mConversation->setIncoming(true);
        
        if(messageType == ConvergedMessage::MmsNotification)
        {
            mConversation->setMMSNotification(true);
            int notificationState = index.data(NotificationStatus).toInt();
            mConversation->setNotificationState(notificationState);
            setNotificationStateIcon(notificationState);
        }
        else
        {
            mMessageStateIconItem->setVisible(false);
        }
    }
    else if (direction == ConvergedMessage::Outgoing)
    {
        setIncoming(false);
        mConversation->setIncoming(false);

        int sendingState = index.data(SendingState).toInt();
        mConversation->setSendingState(sendingState);
        setMessageStateIcon(sendingState);
    }

    bool unreadStatus = index.data(UnReadStatus).toBool();
    mConversation->setUnread(unreadStatus);
    // Create items common to SMS/MMS/IM etc...
    mConversation->drawBubbleFrame();
    mConversation->drawNewItemFrame();

    QDateTime dateTime;
    dateTime.setTime_t(index.data(TimeStamp).toUInt());
    QString resendStateNote((index.data(SendingState).toInt() == ConvergedMessage::Resend)? LOC_RESEND_AT : "");
    if (dateTime.date() == QDateTime::currentDateTime().date())
    {

        mConversation->setTimeStamp(resendStateNote + dateTime.toString(TIME_FORMAT));
    }
    else
    {
        mConversation->setTimeStamp(resendStateNote + dateTime.toString(DATE_FORMAT));
    }

    QDEBUG_WRITE_FORMAT("MsgConversationViewItem::updateChildItems msg type value",messageType)
    
    if (messageType == ConvergedMessage::Sms ||
            messageType == ConvergedMessage::IM)
    {
        mConversation->setMMS(false);
        bodyText.replace(QChar::ParagraphSeparator, QChar::LineSeparator);
        bodyText.replace('\r', QChar::LineSeparator);
        mConversation->setBodyText(bodyText);
    }
    else if (messageType == ConvergedMessage::Mms)
    {
        mConversation->setMMS(true);
        QString attachments = index.data(Attachments).toString();
        QString subject = index.data(Subject).toString();

        bool hasAttachments = (index.data(MessageProperty).toInt()
            & ConvergedMessage::Attachment) ? true : false;

        if (hasAttachments)
        {
            mConversation->setAttachment();
        }
        else
        {
            mConversation->setAttachment(false);
        }
        QStringList mediaFiles = attachments.split('|',
            QString::SkipEmptyParts);
        //Can be used for preview icon
        QStringList imageFiles;

        MsgViewUtils viewUtils;

        for (int i = 0; i < mediaFiles.size(); ++i)
        {
            QString type = viewUtils.mimeTypeL(QDir::toNativeSeparators(
                mediaFiles.at(i)));

            if (type.contains("image"))
            {
                imageFiles << mediaFiles.at(i);
                mConversation->setImage(true);
            }
            else if (type.contains("video"))
            {
                mConversation->setVideo(false);
            }
            else if (type.contains("audio"))
            {
                mConversation->setAudio(true);
            }
        }
        // Now set the media contents
        if (!imageFiles.isEmpty())
        {
            mConversation->setPreviewIconPath(imageFiles.at(0));
        }
        mConversation->displayAudioIcon();

        int priority = index.data(MessagePriority).toInt();
        mConversation->setPriority(priority);
        mConversation->setSubject(subject);
        mConversation->setBodyText(bodyText);        
    } // END of ConvergedMessage::Mms
    else if (messageType == ConvergedMessage::BioMsg)
    {
        if (messageSubType == ConvergedMessage::RingingTone)
        {
            mConversation->setMMS(true);
            mConversation->setImage(false);
            mConversation->setAudio(true);
            mConversation->displayAudioIcon();
            mConversation->setSubject(LOC_RINGING_TONE);
        }
        else if (messageSubType == ConvergedMessage::VCard)
        {
            mConversation->setMMS(true);
            mConversation->setImage(false);
            mConversation->setAudio(true);
            mConversation->displayAudioIcon(VCARD_ICON);
            mConversation->setSubject(LOC_BUSINESS_CARD);
            mConversation->setBodyText(bodyText);
        }
        else
        {
            mConversation->setMMS(false);
            mConversation->setBodyText(LOC_UNSUPPORTED_MSG_TYPE);
        }
    }
    else if (messageType == ConvergedMessage::BT)
    {
        mConversation->setMMS(true);
        QString deviceName = index.data(ConversationAddress).toString();
        mConversation->setSubject(deviceName);
        QString blueToothBody;
        if (messageSubType == ConvergedMessage::VCard)
        {
            mConversation->setImage(false);
            mConversation->setAudio(true);
            mConversation->displayAudioIcon(VCARD_ICON);
            blueToothBody.append(LOC_BUSINESS_CARD);
            blueToothBody.append(QChar::LineSeparator);
            blueToothBody.append(bodyText);
        }        
        else
        {
            blueToothBody.append(bodyText);
        }
        mConversation->setBodyText(blueToothBody);
    }
    else if(messageType == ConvergedMessage::MmsNotification)
    {
        mConversation->setMMS(true);
        
        QString subject = index.data(Subject).toString();
        int priority = index.data(MessagePriority).toInt();
        mConversation->setPriority(priority);
        mConversation->setSubject(subject);
        mConversation->setBodyText(bodyText);
    }

#ifdef _DEBUG_TRACES_
    qCritical() << "END MsgConversationViewItem::updateChildItems: "
            << index.data(ConvergedMsgId).toInt();
#endif

    HbListViewItem::updateChildItems();
}

//---------------------------------------------------------------
// MsgConversationViewItem::containsPoint
//
//---------------------------------------------------------------
bool MsgConversationViewItem::containsPoint(const QPointF& point)
{
    return mConversation->boundingRect().
    contains(mConversation->mapFromScene(point));
}

//---------------------------------------------------------------
// MsgConversationViewItem::setIncoming
// @see header file
//---------------------------------------------------------------
void MsgConversationViewItem::setIncoming(bool incoming)
{
    mIncoming = incoming;
}

//---------------------------------------------------------------
// MsgConversationViewItem::isIncoming
// @see header file
//---------------------------------------------------------------
bool MsgConversationViewItem::isIncoming()
{
    return mIncoming;
}

//---------------------------------------------------------------
// MsgConversationViewItem::setMessageStateIcon
// @see header file
//---------------------------------------------------------------
void MsgConversationViewItem::setMessageStateIcon(int messageState)
{
    HbIconAnimator& iconAnimator = mMessageStateIconItem->animator();
    HbIconAnimationManager* iconAnimationManager = HbIconAnimationManager::global();
    switch (messageState)
    {

        case ConvergedMessage::Waiting:
        case ConvergedMessage::Scheduled:
        case ConvergedMessage::Sending:
        { 
 	        bool defined = iconAnimationManager->addDefinitionFile(ANIMATION_FILE);
     	    HbIcon animIcon;
            animIcon.setIconName(ANIMATION_ICON_NAME);
            QSizeF size = mMessageStateIconItem->size();
            mMessageStateIconItem->setIcon(animIcon);
            mMessageStateIconItem->setVisible(true);
            iconAnimator.startAnimation();
            repolish();
            break;
        }     
        case ConvergedMessage::Suspended:
        {
           iconAnimator.stopAnimation();
           mMessageStateIconItem->setIcon(MSG_OUTBOX_ICON);
           mMessageStateIconItem->setVisible(true);
           break;
        
        }
        case ConvergedMessage::Resend:
        {
            iconAnimator.stopAnimation();
            mMessageStateIconItem->setIcon(MSG_OUTBOX_ICON);
            mMessageStateIconItem->setVisible(true);
            break;
        }
        case ConvergedMessage::Failed:
        {
            iconAnimator.stopAnimation();
            mMessageStateIconItem->setIcon(MSG_FAIL_ICON);
            mMessageStateIconItem->setVisible(true);
            break;
        }   
        case ConvergedMessage::Unknown:
        default:
        {
            iconAnimator.stopAnimation();
            mMessageStateIconItem->setVisible(false);
            break;
        }
    }
}

//---------------------------------------------------------------
// MsgConversationViewItem::setNotificationStateIcon
// @see header file
//---------------------------------------------------------------

void MsgConversationViewItem::setNotificationStateIcon(int notificationState)
{
    HbIconAnimator& iconAnimator = mMessageStateIconItem->animator();
    HbIconAnimationManager* iconAnimationManager = HbIconAnimationManager::global();
    switch (notificationState)
    {

        case ConvergedMessage::NotifRetrieving:
        case ConvergedMessage::NotifWaiting:
        { 
            //TODO: Temp icon until official icons are received
            bool defined = iconAnimationManager->addDefinitionFile(ANIMATION_FILE);
            HbIcon animIcon;
            animIcon.setIconName(ANIMATION_ICON_NAME);
            QSizeF size = mMessageStateIconItem->size();
            mMessageStateIconItem->setIcon(animIcon);
            mMessageStateIconItem->setVisible(true);
            iconAnimator.startAnimation();
            repolish();
            break;
        }     
        default:
        {
            iconAnimator.stopAnimation();
            mMessageStateIconItem->setVisible(false);
            break;
        }
    }
}

//---------------------------------------------------------------
// MsgConversationViewItem::pressStateChanged
// @see header file
//---------------------------------------------------------------
void MsgConversationViewItem::pressStateChanged (bool pressed, bool animate)
{
    mConversation->pressStateChanged(pressed, animate);
}

// EOF
