import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.ListItems 1.0 as ListItem
import Dekko.Settings 0.2
import Dekko.Theme 0.2
import "../../AccountsView"
import "../../Components"

VisualItemModel {
    id: globSettingsModel

    SectionDivider {
        anchors {
            left: parent.left
            right: parent.right
        }
        text: qsTr("Account settings")
    }

    Column {
        anchors {
            left: parent.left
            right: parent.right
        }
        Repeater {
            width: parent.width
            model: dekko.accountsManager
            delegate: AccountsDelegate {
                id: delegate
                // TODO: replace with correctIcon
//                accountImagePath: Qt.resolvedUrl(model.domainIcon)
                imageName: "account"
                organization: model.organization
                description: model.displayName
                showDivider: true
                anchors {
                    left: parent.left
                    right: parent.right
                }

                onItemClicked: {
                    internalStack.push(Qt.resolvedUrl("../user/AccountsListPage.qml"), {account: dekko.accountsManager.get(model.index)})
                }

                leftSideAction: Action {
                    text: qsTr("Delete")
                    iconName: "delete"
                    onTriggered: dekko.accountsManager.removeRow(model.index)
                }
            }
        }
    }
    SectionDivider {
        anchors {
            left: parent.left
            right: parent.right
        }
        text: qsTr("Display settings")
    }
    Selector {
        id: markAsReadSelector
        anchors {
            left: parent.left
            right: parent.right
        }
        description: qsTr("Mark messages read")
        contents: MarkReadSelector {
            onSelectedTextChanged: markAsReadSelector.selectedText = selectedText
        }
    }
    Selector {
        id: previewSelector
        anchors {
            left: parent.left
            right: parent.right
        }
        description: qsTr("Show preview")
        contents: PreviewSelector {
            onSelectedTextChanged: previewSelector.selectedText = selectedText
        }
    }
    ListItemSwitch {
        text: qsTr("Prefer plain text messages")
        onCheckedChanged: GlobalSettings.preferences.hideMarkedDeleted = checked
        Component.onCompleted: checked = GlobalSettings.preferences.hideMarkedDeleted
    }

    ListItemSwitch {
        text: qsTr("Auto load images")
        onCheckedChanged: GlobalSettings.preferences.autoLoadImages = checked
        Component.onCompleted: checked = GlobalSettings.preferences.autoLoadImages
    }
    ListItemSwitch {
        text: qsTr("Hide messages marked for deletion")
        onCheckedChanged: GlobalSettings.preferences.hideMarkedDeleted = checked
        Component.onCompleted: checked = GlobalSettings.preferences.hideMarkedDeleted
    }
    ListItemSwitch {
        text: qsTr("Enable avatars")
        onCheckedChanged: GlobalSettings.preferences.gravatarAllowed = checked
        Component.onCompleted: checked = GlobalSettings.preferences.gravatarAllowed
    }

    SectionDivider {
        anchors {
            left: parent.left
            right: parent.right
        }
        text: qsTr("Privacy and security")
    }
    ListItemSwitch {
        text: qsTr("Show you are using Dekko")
        onCheckedChanged: GlobalSettings.preferences.imapEnableId = checked
        Component.onCompleted: checked = GlobalSettings.preferences.imapEnableId
    }

    SectionDivider {
        anchors {
            left: parent.left
            right: parent.right
        }
        text: qsTr("Offline settings")
    }
    ListItemSwitch {
        text: qsTr("Start offline")
        onCheckedChanged: GlobalSettings.offline.startOffline = checked
        Component.onCompleted: checked = GlobalSettings.offline.startOffline
    }
    Selector {
        id: cacheSelector
        anchors {
            left: parent.left
            right: parent.right
        }
        description: qsTr("Cache offline")
        contents: CachePolicySelector {
            onSelectedTextChanged: cacheSelector.selectedText = selectedText
        }
    }
}
