import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import "../Components"

ListItem {
    id: detailsPanel
    property var msgModel
    property bool showDetails: false
    property int detailsHeight: details.height > units.gu(30) ? units.gu(30) : details.height
    color: "#ffffff"

    height: 0
    expansion.height: detailsHeight
    expansion.expanded: showDetails
    divider.visible: showDetails
    Flickable {
        anchors.fill: parent
        contentHeight: details.height
        Column {
            id: details
            anchors {
                left: parent.left
                leftMargin: units.gu(1)
                right: parent.right
                rightMargin: units.gu(1)
                top: parent.top
            }
            ListItem {
                height: units.gu(5)
                Item {
                    id: p
                    width: tl.width + units.gu(1)
                    anchors {
                        left: parent.left
                        top: parent.top
                        bottom: parent.bottom
                    }

                    Label {
                        id: tl
                        anchors {
                            left: parent.left
                            top: parent.top
                            topMargin: units.gu(2)
                            leftMargin: units.gu(1)
                        }
                        text: qsTr("From:")
                    }
                }
                UbuntuShape {
                    id: delegate
                    aspect: UbuntuShape.Flat
                    color: Qt.rgba(0, 0, 0, 0.05)
                    radius: "small"
                    height: units.gu(3)
                    width: inner_avatar.width + label.width + units.gu(1.5)
                    anchors {
                        left: p.right
                        leftMargin: units.gu(1)
                        top: parent.top
                        topMargin: units.gu(1.5)
                    }

                    Avatar {
                        id: inner_avatar
                        width: height
                        anchors {
                            left: parent.left
                            top: parent.top
                            bottom: parent.bottom
                            margins: units.dp(1)
                        }
                        name: msgModel.hasValidIndex ? msgModel.from.get(0).name : ""
                        initials: msgModel.hasValidIndex ? msgModel.from.get(0).initials : ""
                        email: msgModel.hasValidIndex ? msgModel.from.get(0).email : ""
                        fontSize: "x-small"
                        validContact: true
                    }

                    Label {
                        id: label
                        anchors {
                            left: inner_avatar.right
                            leftMargin: units.gu(0.5)
                            verticalCenter: parent.verticalCenter
                        }
                        text: msgModel.hasValidIndex ? msgModel.from.get(0).name : ""
                    }

                    AbstractButton {
                        anchors.fill: parent
                        onClicked: {
                            PopupUtils.open(Qt.resolvedUrl("./RecipientInfo.qml"), delegate, {address: msgModel.from.get(0)})
                        }
                    }
                }
            }

            RecipientFlow {
                title: qsTr("To:")
                message: msgModel
                model: msgModel.to
            }
            RecipientFlow {
                visible: msgModel.cc.count
                title: qsTr("Cc:")
                message: msgModel
                model: msgModel.cc
            }
        }
    }
}

