// SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.1-or-later

import QtQuick
import QtQuick.Controls as QQC2
import QtQuick.Layouts

import org.kde.kirigami as Kirigami
import org.kde.kirigamiaddons.components as Components
import org.kde.kirigamiaddons.formcard as FormCard

import './private' as Private

/*!
   \qmltype FormCardDialog
   \inqmlmodule org.kde.kirigamiaddons.formcard
   \brief A dialog designed to use FormCard delegates as its content.

   \qml
   import org.kde.kirigamiaddons.formcard as FormCard
   import QtQuick.Controls

   FormCard.FormCardDialog {
       title: "Add Thingy"

       standardButtons: Dialog.Ok | Dialog.Cancel
       FormCard.FormTextFieldDelegate {
           label: i18nc("@label:textbox Notebook name", "Name:")
       }

       FormCard.FormDelegateSeparator {}

       FormCard.FormButtonDelegate {
           text: i18nc("@action:button", "Color")
           icon.name: "color-picker"
       }

       FormCard.FormDelegateSeparator {}

       FormCard.FormButtonDelegate {
           text: i18nc("@action:button", "Icon")
           icon.name: "preferences-desktop-emoticons"
       }
   }
   \endqml

   \image formcarddialog.png

   \since 1.1.0
 */
QQC2.Dialog {
    id: root

    default property alias content: columnLayout.data

    x: Math.round((parent.width - width) / 2)
    y: Math.round((parent.height - height) / 2)
    z: Kirigami.OverlayZStacking.z

    background: Components.DialogRoundedBackground {}

    parent: applicationWindow().QQC2.Overlay.overlay

    implicitWidth: if (parent.width > 576) {
        return Math.min(parent.width - Kirigami.Units.gridUnit * 2, Math.max(Kirigami.Units.gridUnit * 15, header.implicitWidth))
    } else {
        return parent.width - Kirigami.Units.gridUnit * 2;
    }

    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             contentHeight + topPadding + bottomPadding
                             + (implicitHeaderHeight > 0 ? implicitHeaderHeight + spacing : 0)
                             + (implicitFooterHeight > 0 ? implicitFooterHeight + spacing : 0))

    modal: true
    focus: true

    padding: 0

    header: RowLayout {
        spacing: Kirigami.Units.smallSpacing

	    Kirigami.Heading {
            text: root.title
            elide: QQC2.Label.ElideRight
            leftPadding: Private.FormCardUnits.horizontalPadding
            rightPadding: Private.FormCardUnits.horizontalPadding
            topPadding: Private.FormCardUnits.verticalPadding
            bottomPadding: 0

            Layout.fillWidth: true
        }

        QQC2.ToolButton {
            id: closeButton

            Layout.alignment: Qt.AlignVCenter
            Layout.rightMargin: Private.FormCardUnits.horizontalPadding
            Layout.topMargin: Private.FormCardUnits.horizontalPadding

            icon.name: hovered ? "window-close" : "window-close-symbolic"
            text: i18ndc("kirigami-addons6", "@action:button close dialog", "Close")
            display: QQC2.AbstractButton.IconOnly
            visible: root.standardButtons & QQC2.Dialog.Cancel
            enabled: visible && root.standardButton(QQC2.Dialog.Cancel).enabled

            onClicked: root.reject()
        }
    }

    contentItem: ColumnLayout {
        id: columnLayout

        spacing: 0
    }

    footer: QQC2.DialogButtonBox {
        leftPadding: Private.FormCardUnits.horizontalPadding
        rightPadding: Private.FormCardUnits.horizontalPadding
        bottomPadding: Private.FormCardUnits.verticalPadding
        topPadding: Kirigami.Units.smallSpacing
        spacing: Kirigami.Units.mediumSpacing

        standardButtons: root.standardButtons
        visible: count > 0
    }
}
