/* Copyright (C) 2014-2016 Dan Chapman <dpniel@ubuntu.com>

   This file is part of 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 Ubuntu.Components.ListItems 1.0 as ListItem
import Ubuntu.Components.Popups 1.0
import Dekko.Theme 0.2
import Dekko.Models 0.2
import "../Components"
import "../Composer"
import "../UCSComponents"


DekkoPage {
    id: mailboxListPage
    pageTitle: dekko.currentAccount.profile.displayName

    // This sets the DekkoPage's internal state to set the correct actions
    // from this context.
    pageType: "mbox"
    property QtObject model: dekko.currentAccount.mailboxModel
    property string currentMailbox
    property string currentMailboxLong
    property string allFilterName: qsTr("All")
    property string subscribeFilterName: qsTr("Subscribed")

    signal mailboxSelected(string mailbox)
    onMailboxSelected: {
        internalStack.push(Qt.resolvedUrl("../MessageListView/MessageListPage.qml"), {accountsDrawerVisible: false})
        dekko.currentAccount.msgListModel.setMailbox(mailbox)
        // This ensures the Inbox is always visible in the accounts drawer. As any nestingDepth causes
        // It to be hidden.
        dekko.currentAccount.mailboxModel.setOriginalRoot()
    }
    enableSearching: true
    filters: dekko.currentAccount.imapSettings.filterSubscribed ? [subscribeFilterName, allFilterName] : [allFilterName, subscribeFilterName]
    onSelectedFilterIndexChanged: {
        var allFilterIndex = dekko.currentAccount.imapSettings.filterSubscribed ? 1 : 0
        var subscribeFilterIndex = !allFilterIndex
        if (selectedFilterIndex == allFilterIndex)
            filterModel.filterRole = MailboxFilterModel.FILTER_NONE
        else if (selectedFilterIndex == subscribeFilterIndex)
            filterModel.filterRole = dekko.currentAccount.imapSettings.filterSubscribed ? MailboxFilterModel.FILTER_SUBSCRIBED : MailboxFilterModel.FILTER_NONE
    }
    pageFlickable: scrollView
    secondaryActions: [
        Action {
            id: connectionDialogAction
            iconName: "nm-no-connection"
            visible: !dekko.currentAccount.imapModel.isNetworkOnline
            text: qsTr("Network")
            onTriggered: {
                dekko.currentAccount.network.setNetworkOnline()
            }
        }
    ]
    onSearchActivated: {
        searchFilterModel.queryString = searchString
        searchFilterModel.invalidate()
    }

    onSearchCanceled: {
        searchFilterModel.queryString = ""
        // Let's ensure it invalidated incase we search again and
        // the previous filter is still applied. Probably unlikely
        // but let's guard it.
        searchFilterModel.invalidate()
    }

    MailboxFilterModel {
        id: filterModel
        filterRole: dekko.currentAccount.imapSettings.filterSubscribed ? MailboxFilterModel.FILTER_SUBSCRIBED : MailboxFilterModel.FILTER_NONE
        sourceModel: dekko.currentAccount.mailboxModel
    }

    FlatteningProxyModel {
        id: flatModel
        sourceModel: filterModel
    }

    // Filter model for search queries
    MailboxFilterModel {
        id: searchFilterModel
        sourceModel: isSearchMode ? flatModel : null
        filterRole: MailboxFilterModel.FILTER_QUERY
    }

    ScrollView {
        id: scrollView
        MailboxListView {
            id: mboxListView
            property bool isSearchMode: mailboxListPage.isSearchMode
            anchors.fill: parent
            model: isSearchMode ? searchFilterModel : flatModel
            onOpenMailbox: {
                currentMailbox = shortMboxName
                currentMailboxLong = mboxName
                mailboxSelected(mboxName)
            }
        }
    }
    Rectangle {
        id: btmshadow
        anchors {
            left: parent.left
            right: parent.right
            top: scrollView.top
        }
        opacity: mboxListView.contentY > 10 ? 0.8 : 0.0
        Behavior on opacity {
            UbuntuNumberAnimation{}
        }

        height: units.gu(0.8)
        gradient: Gradient {
            GradientStop { position: 0.0; color: UbuntuColors.warmGrey }
            GradientStop { position: 1.0; color: "transparent" }
        }
    }
}
