/* 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 "../Components"

Rectangle {
    id: attachmentsPanel
    Rectangle {
        anchors {
            left: parent.left
            top: parent.top
            right: parent.right
        }
        height: units.dp(1)
        color: Qt.rgba(0,0,0,0.075)
    }

    MouseArea {
        anchors.fill: parent
        preventStealing: true
    }
    color: Style.common.background
    property int maxHeight
    readonly property int expandedHeight: internalHeight > maxHeight ? maxHeight : internalHeight
    readonly property int internalHeight: li.height + column.height
    readonly property int collapsedHeight: attachmentsPanel.visible ? li.height : 0
    readonly property bool expanded: state === "expanded"
    state: "collapsed"
    states: [
        State {
            name: "collapsed"
            PropertyChanges {
                target: attachmentsPanel
                height: collapsedHeight
            }
        },
        State {
            name: "expanded"
            PropertyChanges {
                target: attachmentsPanel
                height: expandedHeight
            }
        }
    ]
    // ensure we reset the state when the message is changed
    // otherwise if the next message has attachments this will already be
    // expanded.
    Connections {
        target: dekko.currentAccount.singleThreadModel
        onThreadRootChanged: {
            if (attachmentsPanel.expanded) {
                attachmentsPanel.state = "collapsed"
            }
        }
    }

    Connections {
        target: msgModel
        onAttachmentsChanged: {
            if (visible) {
                var size = msgModel.attachments.size()
                var title = qsTr("Attachments")
                if (size > 0) {
                    title += " (%1)".arg(size)
                }
                layout.title.text = title
            } else {
                layout.title.text = ""
            }
        }
    }

    Behavior on height {
        UbuntuNumberAnimation{}
    }

    ListItem {
        id: li
        width: parent.width
        height: layout.height
        ListItemLayout {
            id: layout
            title.font.weight: Font.Normal
            CachedImage {
                id: attachmentImg
                height: units.gu(3)
                width: height
                name: "attachment"
                color: "#888888"
                SlotsLayout.position: SlotsLayout.Leading
            }

            Icon {
                id: icon
                name: "up"
                color: Style.common.text
                rotation: attachmentsPanel.expanded ? 180 : 0
                height: units.gu(2); width: height
                state: attachmentsPanel.expanded ? "rotated" : "normal"
                states: [
                    State {
                        name: "rotated"
                        PropertyChanges { target: icon; rotation: 180 }
                    },
                    State {
                        name: "normal"
                        PropertyChanges { target: icon; rotation: 0 }
                    }
                ]
                transitions: Transition {
                    RotationAnimation {
                        duration: UbuntuAnimation.FastDuration
                        direction: icon.state === "normal"  ? RotationAnimation.Clockwise : RotationAnimation.Counterclockwise
                    }
                }

            }
        }
        onClicked: attachmentsPanel.state == "collapsed" ? attachmentsPanel.state = "expanded" : attachmentsPanel.state = "collapsed"
    }
    Flickable {
        id: flicker
        anchors {
            left: parent.left
            top: li.bottom
            right: parent.right
            bottom: parent.bottom
        }
        clip: true
        contentHeight: column.height
        Column {
            id: column
            anchors {
                left: parent.left
                right: parent.right
                top: parent.top
            }
            Repeater {
                id: repeater
                model: attachmentsPanel.visible ? msgModel.attachments : null
                delegate: AttachmentDelegate {
                    attachment: model.qtObject
                    onOpenAttachment: {
                        if (attachment.mimeType === "message/rfc822") {
                            // TODO: displayEmbeddedMessage(partId);
                        } else if (isRunningOnMir) {
                            PopupUtils.open(Qt.resolvedUrl("../Dialogs/ContentPickerDialog.qml"),
                                                                            oneMessagePage, {
                                                                                fileUrl: url
                                                                            });
                        } else {
                            Qt.openUrlExternally(url)
                        }
                    }
                }
            }
        }
    }
}

