/* Copyright (C) 2015 Daniyaal Rasheed <radonapps@gmail.com>

   This file is part of the Dekko email client for Ubuntu Touch/

   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

   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 QtQuick.Layouts 1.1
import Dekko.Theme 0.2
import Dekko.Accounts 0.2
import Dekko.Components 0.2
import "../Components"

Panel {
    id: imapLogDrawer
    animate: true
    width: parent.width
    height: units.gu(40)
    anchors.bottom: parent.bottom
    visible: opened || animating
    Behavior on height {
        UbuntuNumberAnimation {duration: 200}
    }
    Rectangle {
        anchors.fill: parent
        color: "#2d2d2d"
    }
    Rectangle {
        id: topshadow
        anchors {
            left: parent.left
            right: parent.right
            bottom: parent.top
        }
        height: units.gu(0.8)
        gradient: Gradient {
            GradientStop { position: 0.0; color: "transparent" }
            GradientStop { position: 1.0; color: Qt.rgba(0, 0, 0, 0.1) }
        }
    }
    Item {
        id: internalStates
        state: "default"
        readonly property bool isExpanded: state === "expanded"
        states: [
            State {
                name: "default"
                PropertyChanges {
                    target: imapLogDrawer
                    height: units.gu(40)
                }
            },
            State {
                name: "expanded"
                PropertyChanges {
                    target: imapLogDrawer
                    height: dekko.height * 3/4
                }
            }
        ]
    }
    DekkoHeader {
        id: logHeader
        title: "IMAP Log Viewer"
        primaryAction: Action {
            id: dropOrRiseAction
            iconName: internalStates.isExpanded ? "down" : "up"
            onTriggered: {
                internalStates.isExpanded ? internalStates.state = "default" : internalStates.state = "expanded"
            }
        }
        anchors.top: topshadow.bottom
    }
    AccountsListModel {
        id: imapAccounts
        srcModel: dekko.accountsManager
        filterRole: AccountsListModel.FILTER_IMAP
    }
    ListView {
        id: accountsView
        orientation: ListView.Horizontal
        flickableDirection: Flickable.HorizontalFlick
        snapMode: ListView.SnapOneItem
        anchors {
            top: logHeader.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }
        model: imapAccounts
        cacheBuffer: accountsView.width * 10
        delegate: Item {
            width: accountsView.width
            height: accountsView.height
            ImapLog {
                id: imapLog
                account: dekko.accountsManager.count ? dekko.accountsManager.getFromId(model.accountId) : null
            }
            ListView {
                id: loggingArea
                anchors {
                    fill: parent
                    leftMargin: units.gu(1)
                    bottomMargin: units.gu(1)
                    rightMargin: units.gu(1)
                }

                orientation: ListView.Vertical
                flickableDirection: Flickable.VerticalFlick
                header: Item {
                    z: 2
                    height: units.gu(4)
                    width: parent.width
                    Rectangle {
                        color: "#2d2d2d"
                        anchors.fill: parent
                    }
                    Label {
                        anchors {left: parent.left; verticalCenter: parent.verticalCenter}
                        text: model.displayName
                        color: "#ffffff"
                        font.weight: Font.Bold
                        font.underline: true
                    }
                    Rectangle {
                        anchors {
                            left: parent.left
                            bottom: parent.bottom
                            right: parent.right
                        }
                        height: units.dp(1)
                        color: UbuntuColors.green
                    }
                }
                spacing: units.gu(0.5)
                headerPositioning: ListView.OverlayHeader
                model: imapLog.log
                clip: true
                delegate: Label {
                    anchors {
                        left: parent.left
                        right: parent.right
                    }
                    fontSize: "small"
                    text: model.qtObject.text
                    wrapMode: Text.Wrap
                    color: "white"
                    font.weight: Style.common.fontWeight
                }
                Connections {
                    target: imapLog.log
                    onCountChanged: {
                        if (!logPositionTimer.running)
                            logPositionTimer.start()
                    }
                }
                Timer {
                    id: logPositionTimer
                    interval: 300
                    repeat: false
                    onTriggered: loggingArea.positionViewAtEnd()
                }
            }
        }
    }
}
