/* 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 QtQuick.Layouts 1.1
import Ubuntu.Components 1.3
import Dekko.Theme 0.2
import Dekko.Utils 0.2
import "../Components"
import "../Utils/Utils.js" as Utils

Item {
    id: header

    height: dekko.viewState.isSmallFF ? units.gu(7) : units.gu(10.075)
    anchors {
        left: parent.left
        right: parent.right
        top: parent.top
    }

    property bool showBack: false
    readonly property bool hasSingleThreadModel: dekko.currentAccount && dekko.currentAccount.singleThreadModel

    HeaderButton {
        id: inner_button
        anchors {
            top: parent.top
            topMargin: units.gu(1)
            left: parent.left
        }
        visible: showBack
        width: showBack ? units.gu(5) : 0
        action: Action {
            iconName: "back"
            onTriggered: {
                dekko.currentAccount.singleThreadModel.invalidateModel()
            }
        }
    }

    Label {
        id: fromLabel
        anchors {
            left: inner_button.right
            leftMargin: showBack ? 0 : units.gu(2)
            top: parent.top
            topMargin: units.gu(2)
            right: starButton.left
            rightMargin: units.gu(1)
        }
        clip: true
        text: (hasSingleThreadModel && dekko.currentAccount.singleThreadModel.hasValidIndex) ?
                  dekko.currentAccount.singleThreadModel.baseSubject : ""
        elide: Text.ElideRight
        font.weight: Font.Light
        fontSize: "large"
    }

    RowLayout {
        anchors {
            left: fromLabel.left
            top: fromLabel.bottom
            topMargin: units.gu(1)
            right: starButton.left
            rightMargin: units.gu(1)
        }

        Label {
            id: subjectLabel
            text: (hasSingleThreadModel && dekko.currentAccount.singleThreadModel.hasValidIndex) ?
                      dekko.currentAccount.singleThreadModel.sendersInThread : ""
            clip: true
            elide: Text.ElideRight
            visible: !dekko.viewState.isSmallFF
            font.weight: Font.Normal

        }
    }

    HeaderButton {
        id: starButton
        anchors {
            right: deleteButton.left
            top: parent.top
            topMargin: units.gu(1)
        }
        visible: hasSingleThreadModel && dekko.currentAccount.singleThreadModel.hasValidIndex
        iconColor: hasSingleThreadModel && dekko.currentAccount.singleThreadModel.threadRootFlagged ? Style.actions.starred : Style.header.icons
        action: Action {
            iconSource: hasSingleThreadModel && dekko.currentAccount.singleThreadModel.threadRootFlagged ? "qrc:///actions/starred.svg" : "qrc:///actions/non-starred.svg"
            onTriggered: dekko.currentAccount.singleThreadModel.markThreadFlagged()
        }
    }

    HeaderButton {
        id: deleteButton
        anchors {
            right: parent.right
            rightMargin: units.gu(1)
            top: parent.top
            topMargin: units.gu(1)
        }
        visible: hasSingleThreadModel && dekko.currentAccount.singleThreadModel.hasValidIndex
        action: Action {
            iconName: "delete"
            shortcut: "Delete"
            onTriggered: {
                if (!dekko.currentAccount.singleThreadModel.hasValidIndex)
                    return
                // FIXME: We need to select the next message after this delete action
                // It's not so trivial to just base this on UID as there's no guarantee they are in order so we need a query
                // adding to the MsgListModel. as it the next message may not be in this thread.
                // We also need to handle deleting a whole thread, maybe Ctrl+Delete? This approach is blindly
                // ignoring the fact this is actually a thread model,
                mboxUtils.markMessageDeleted(mboxUtils.deproxifiedIndex(dekko.currentAccount.singleThreadModel.threadRootIndex), true)
                dekko.currentAccount.singleThreadModel.invalidateModel()
            }
        }
    }

    MailboxUtils {
        id: mboxUtils
        account: dekko.currentAccount
    }

    Rectangle {
        id: btmMargin
        anchors {
            left: parent.left
            right: parent.right
            bottom: header.bottom
        }
        color: Qt.rgba(0,0,0,0.075)
        height: units.gu(0.075)
        visible: true
    }
}
