/* 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
    height: units.gu(5)

    property alias model: repeater.model
    signal addAttachment()

    function collapse() {
        attachmentsPanel.state = "collapsed"
    }

    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: Qt.inputMethod.visible ? 0 : collapsedHeight
            }
        },
        State {
            name: "expanded"
            PropertyChanges {
                target: attachmentsPanel
                height: expandedHeight
            }
        }
    ]

    Behavior on height {
        UbuntuNumberAnimation{}
    }

    ListItemWithActions {
        id: li
        showDivider: true
        width: parent.width
        height: layout.height
        ListItemLayout {
            id: layout
            property string trText: qsTr("Attachments")
            title.text: repeater.count > 0 ? trText + " (%1)".arg(repeater.count) : trText
            title.font.weight: Style.common.fontWeight
            CachedImage {
                id: attachmentImg
                height: units.gu(3)
                width: height
                name: "attachment"
                color: "#888888"
                SlotsLayout.position: SlotsLayout.Leading
            }

            AbstractButton {
                height: units.gu(5)
                width: height
                // We have to override the vertical position otherwise setting
                // a larger height results in the LayoutHeight growing to add
                // the default padding which we don't want. But we do want the same behaviour
                // of being vertically centered so re-apply that
                SlotsLayout.overrideVerticalPositioning: true
                anchors {
                    verticalCenter: parent.verticalCenter
                }
                Icon {
                    id: addAttachmentIcon
                    name: "add"
                    color: Style.common.text
                    height: units.gu(2)
                    width: height
                    anchors {
                        right: parent.right
                        verticalCenter: parent.verticalCenter
                        rightMargin: units.gu(2)
                    }
                }
                onClicked: attachmentsPanel.addAttachment()
            }
        }
        onItemClicked: attachmentsPanel.state == "collapsed" && repeater.count ?
                           attachmentsPanel.state = "expanded" :
                           attachmentsPanel.state = "collapsed"
    }
    ScrollView {
        id: scrollView
        anchors {
            left: parent.left
            top: li.bottom
            right: parent.right
            bottom: parent.bottom
        }
        Flickable {
            id: flicker
            anchors.fill: parent
            clip: true
            contentHeight: column.height
            Column {
                id: column
                anchors {
                    left: parent.left
                    right: parent.right
                    top: parent.top
                }
                Repeater {
                    id: repeater
                    onCountChanged: {
                        if (!count) {
                            attachmentsPanel.collapse()
                        }
                    }
                    delegate: AttachmentsDelegate {
                        name: model.caption
                        mimeType: model.mimeType
                        size: model.humanReadableSize
                        iconName: model.mimeTypeIconName
                        onRemove: attachmentsPanel.model.removeAttachmentAtIndex(model.index)
                    }
                }
            }
        }
    }
}

