import QtQuick
import qs.Common
import qs.Services
import qs.Widgets
import "../../../Common/Format.js" as Format

Rectangle {
    id: root

    LayoutMirroring.enabled: I18n.isRtl
    LayoutMirroring.childrenInherit: true

    signal dismissed

    readonly property bool currentlyActive: SessionData.doNotDisturb
    readonly property real currentRemainingMs: SessionData.doNotDisturbUntil > 0 ? Math.max(0, SessionData.doNotDisturbUntil - nowMs) : 0
    property real nowMs: Date.now()

    Timer {
        interval: 1000
        repeat: true
        running: root.visible && root.currentlyActive && SessionData.doNotDisturbUntil > 0
        onTriggered: root.nowMs = Date.now()
    }

    function formatRemaining(ms) {
        return Format.formatRemaining(ms, I18n.tr("Off"), I18n.tr("%1 min left"), I18n.tr("%1 h left"), I18n.tr("%1 h %2 m left"));
    }

    function minutesUntilTomorrowMorning() {
        const now = new Date();
        const target = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 8, 0, 0, 0);
        return Math.max(1, Math.round((target.getTime() - now.getTime()) / 60000));
    }

    readonly property var presetOptions: [
        {
            "label": I18n.tr("For 15 minutes"),
            "minutes": 15
        },
        {
            "label": I18n.tr("For 30 minutes"),
            "minutes": 30
        },
        {
            "label": I18n.tr("For 1 hour"),
            "minutes": 60
        },
        {
            "label": I18n.tr("For 3 hours"),
            "minutes": 180
        },
        {
            "label": I18n.tr("For 8 hours"),
            "minutes": 480
        },
        {
            "label": I18n.tr("Until tomorrow, 8:00 AM"),
            "minutesFn": true
        },
        {
            "label": I18n.tr("Until I turn it off"),
            "minutes": 0
        }
    ]

    function selectPreset(option) {
        let minutes = option.minutes;
        if (option.minutesFn) {
            minutes = minutesUntilTomorrowMorning();
        }
        SessionData.setDoNotDisturb(true, minutes);
        root.dismissed();
    }

    function turnOff() {
        SessionData.setDoNotDisturb(false);
        root.dismissed();
    }

    implicitWidth: Math.max(220, menuColumn.implicitWidth + Theme.spacingM * 2)
    implicitHeight: menuColumn.implicitHeight + Theme.spacingM * 2
    color: Theme.floatingSurface
    radius: Theme.cornerRadius
    border.color: BlurService.borderColor
    border.width: BlurService.borderWidth

    Column {
        id: menuColumn
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.margins: Theme.spacingM
        spacing: Theme.spacingXS

        Row {
            width: parent.width
            spacing: Theme.spacingS

            DankIcon {
                name: SessionData.doNotDisturb ? "notifications_off" : "notifications_paused"
                size: Theme.iconSize - 2
                color: SessionData.doNotDisturb ? Theme.primary : Theme.surfaceText
                anchors.verticalCenter: parent.verticalCenter
            }

            Column {
                width: parent.width - Theme.iconSize - parent.spacing
                anchors.verticalCenter: parent.verticalCenter
                spacing: 0

                StyledText {
                    text: I18n.tr("Do Not Disturb")
                    font.pixelSize: Theme.fontSizeMedium
                    font.weight: Font.Medium
                    color: Theme.surfaceText
                    elide: Text.ElideRight
                    width: parent.width
                }

                StyledText {
                    visible: root.currentlyActive
                    text: {
                        if (SessionData.doNotDisturbUntil > 0) {
                            return root.formatRemaining(root.currentRemainingMs) + " · " + I18n.tr("until %1").arg(Format.formatUntil(SessionData.doNotDisturbUntil, SettingsData.use24HourClock));
                        }
                        return I18n.tr("On indefinitely");
                    }
                    font.pixelSize: Theme.fontSizeSmall
                    color: Theme.surfaceVariantText
                    elide: Text.ElideRight
                    width: parent.width
                }
            }
        }

        Rectangle {
            width: parent.width
            height: 1
            color: Theme.outlineStrong
        }

        Repeater {
            model: root.presetOptions

            Rectangle {
                id: optionRect
                required property var modelData
                width: menuColumn.width
                height: 32
                radius: Theme.cornerRadius
                color: optionArea.containsMouse ? BlurService.hoverColor(Theme.widgetBaseHoverColor) : Theme.withAlpha(BlurService.hoverColor(Theme.widgetBaseHoverColor), 0)

                StyledText {
                    anchors.left: parent.left
                    anchors.leftMargin: Theme.spacingS
                    anchors.right: parent.right
                    anchors.rightMargin: Theme.spacingS
                    anchors.verticalCenter: parent.verticalCenter
                    text: optionRect.modelData.label
                    font.pixelSize: Theme.fontSizeSmall
                    color: Theme.surfaceText
                    elide: Text.ElideRight
                }

                MouseArea {
                    id: optionArea
                    anchors.fill: parent
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    onClicked: root.selectPreset(optionRect.modelData)
                }
            }
        }

        Rectangle {
            visible: root.currentlyActive
            width: parent.width
            height: 1
            color: Theme.outlineStrong
        }

        Rectangle {
            visible: root.currentlyActive
            width: menuColumn.width
            height: 32
            radius: Theme.cornerRadius
            color: offArea.containsMouse ? Theme.errorPressed : Theme.withAlpha(Theme.errorPressed, 0)

            Row {
                anchors.left: parent.left
                anchors.leftMargin: Theme.spacingS
                anchors.right: parent.right
                anchors.rightMargin: Theme.spacingS
                anchors.verticalCenter: parent.verticalCenter
                spacing: Theme.spacingS

                DankIcon {
                    anchors.verticalCenter: parent.verticalCenter
                    name: "notifications_active"
                    size: Theme.iconSizeSmall
                    color: offArea.containsMouse ? Theme.error : Theme.surfaceText
                }

                StyledText {
                    anchors.verticalCenter: parent.verticalCenter
                    text: I18n.tr("Turn off now")
                    font.pixelSize: Theme.fontSizeSmall
                    color: offArea.containsMouse ? Theme.error : Theme.surfaceText
                    font.weight: Font.Medium
                }
            }

            MouseArea {
                id: offArea
                anchors.fill: parent
                hoverEnabled: true
                cursorShape: Qt.PointingHandCursor
                onClicked: root.turnOff()
            }
        }
    }
}
