import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import "../Components"

Popover {
    id: info
    property var address

    Column {
        anchors {
            left: parent.left
            top: parent.top
            right: parent.right
        }

        ListItem {
            height: layout.height
            color: UbuntuColors.porcelain
            ListItemLayout {
                id: layout
                title.text: address ? address.name : address.email
                title.elide: Text.ElideRight
                title.wrapMode: Text.NoWrap
                subtitle.text: {
                    if (address.name) {
                        if (address.name !== address.email)
                            return address.email
                        else
                            return ""
                    }/* else if (model.isInContacts) {
                        return model.contactInfo.organization
                    } else {
                        return ""
                    }*/
                }
//                summary.text: address.name && model.isInContacts ? model.contactInfo.organization : ""
                subtitle.elide: Text.ElideRight
                subtitle.wrapMode: Text.NoWrap

                Avatar {
                    id: avatar
                    name: address.name
                    initials: address.initials
                    email: address.email
                    validContact: true
                    fontSize: "large"
                    SlotsLayout.position: SlotsLayout.Leading
                }
            }
        }

        ListItem {
            height: clipboard.implicitHeight
            color: UbuntuColors.porcelain
            ListItemLayout {
                id: clipboard
                title.text: qsTr("Copy to clipboard")
                title.elide: Text.ElideRight
                title.wrapMode: Text.NoWrap
            }
            onClicked: {
                console.log("AddressDisplayName: ", address.fullDisplayName)
                Clipboard.push(["text/plain", address.fullDisplayName])
                PopupUtils.close(info)
            }
        }
        ListItem {
            height: addContactLayout.implicitHeight
            ListItemLayout {
                id: addContactLayout
                title.text: qsTr("Add to addressbook")
                title.elide: Text.ElideRight
                title.wrapMode: Text.NoWrap
            }
            visible: !dekko.addressbookModel.containsEmail(address.email)
            color: UbuntuColors.porcelain
            onClicked: {
                var emails = []
                emails.push(address.email)
                dekko.addressbookModel.newContact(address.name, "", "", emails);
                PopupUtils.close(info)
            }
        }
    }
}

