/* Copyright (C) 2014-2016 Dan Chapman <dpniel@ubuntu.com>

   This file is part of the dekko

   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 any later version
   accepted by the membership of KDE e.V. (or its successor approved
   by the membership of KDE e.V.), which shall act as a proxy
   defined in Section 14 of version 3 of the license.

   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/>.
*/
import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.1
import Dekko.Theme 0.2
import Dekko.Settings 0.2
import Dekko.Composition 0.2
import "../Utils/Utils.js" as Utils
import "../Contact/imports"
import "../Components"

ListItem {
    id: h
    height: defaultLayout.height
    property bool detailsVisible: false
    property var model: msgModel
    signal clicked()
    signal showDetails()

    divider.visible: false

    ListItemLayout {
        id: defaultLayout
        title.text: msgModel.hasValidIndex ? msgModel.from.get(0).name : ""
        title.font.weight: (msgModel.hasValidIndex && msgModel.isMarkedRead) ? Font.Normal : Font.Bold
        title.color: (msgModel.hasValidIndex && msgModel.isMarkedRead) ? "#5d5d5d" : "#333333"
        title.clip: true
        title.elide: Text.ElideRight
        subtitle.text: msgModel.toRecipientsString
        subtitle.clip: true
        subtitle.elide: Text.ElideRight
        summary.text: detailsVisible ? qsTr("Hide details") : qsTr("View details")
        summary.color: UbuntuColors.blue
        // Hack to reparent the mousearea to the summary label.
        // so we can make it clickable.
        Component.onCompleted: ma.parent = defaultLayout.summary

        AbstractButton {
            id: ma
            parent: defaultLayout.summary
            anchors.fill: parent
            onClicked: h.showDetails()
        }

        Avatar {
            id: avatar
            anchors {
                left: parent.left
                top: parent.top
                leftMargin: units.gu(2)
                topMargin: units.gu(2)
            }
            name: defaultLayout.title ? defaultLayout.title.text : ""
            initials: msgModel.hasValidIndex ? msgModel.from.get(0).initials : ""
            email: msgModel.hasValidIndex ? msgModel.from.get(0).email : ""
            validContact: msgModel.hasValidIndex
            fontWeight: Font.Light
            SlotsLayout.position: SlotsLayout.Leading
        }

        Item {
            id: inner_infoCol

            property int iconsVerticalSpacing: units.gu(1)
            property int iconsHorizontalSpacing: units.gu(2)

            height: inner_timeLabel.height + units.gu(1) + ctxt.height
            width: Math.max(inner_timeLabel.width, ctxt.width + rply.width + units.gu(1))
            SlotsLayout.overrideVerticalPositioning: true

            Label {
                id: inner_timeLabel
                y: defaultLayout.mainSlot.y + defaultLayout.title.y
                   + defaultLayout.title.baselineOffset - baselineOffset
                anchors.right: parent.right
                text: msgModel.hasValidIndex ? Utils.prettyDate(model.date) : ""
                fontSize: "small"
            }

            CachedImage {
                id: ctxt
                height: units.gu(2.5)
                width: height
                anchors {
                    right: parent.right
                    top: inner_timeLabel.bottom
                    topMargin: parent.iconsVerticalSpacing
                }
                name: "contextual-menu"
                color: "#888888"
                AbstractButton {
                    anchors.fill: parent
                    onClicked: PopupUtils.open(Qt.resolvedUrl("MessageContextMenu.qml"),
                                               ctxt,
                                               {
                                                   zoomAction: zoomAction,
                                                   currentZoom: wv.currentZoomLevel,
                                                   viewSourceAction: viewSourceAction,
                                                   fullscreenAction: viewFullScreen
                                               })
                }
            }

            CachedImage {
                id: rply
                height: units.gu(2.5)
                width: visible ? height : 0
                anchors {
                    right: ctxt.left
                    rightMargin: parent.iconsHorizontalSpacing
                    top: inner_timeLabel.bottom
                    topMargin: parent.iconsVerticalSpacing
                }
                name: "mail-reply"
                color: "#888888"
                AbstractButton {
                    anchors.fill: parent
                    onClicked: {
                        if (msgModel.isListPost) {
                            mainStage.replyToMessage(ReplyMode.REPLY_LIST)
                        } else {
                            mainStage.replyToMessage(ReplyMode.REPLY_ALL_BUT_ME)
                        }
                    }
                }
            }
        }
    }
}
