import QtQuick 2.15
import QtQuick.Layouts 1.15
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 3.0 as PC3

Item {
    id: root
    width: 160
    height: 32

    function parseKv(text) {
        var obj = {}
        var lines = text.split("\n")
        for (var i = 0; i < lines.length; i++) {
            var line = lines[i]
            if (!line) continue
            var idx = line.indexOf("=")
            if (idx <= 0) continue
            var k = line.substring(0, idx)
            var v = line.substring(idx + 1)
            obj[k] = v
        }
        return obj
    }

    function humanDuration(sec) {
        var s = parseInt(sec)
        if (isNaN(s) || s < 0) return "—"
        var d = Math.floor(s / 86400)
        var h = Math.floor((s % 86400) / 3600)
        var m = Math.floor((s % 3600) / 60)
        if (d > 0) return d + "д " + h + "ч"
        if (h > 0) return h + "ч " + m + "м"
        return m + "м"
    }

    function compute() {
        var kv = parseKv(execDS.data["stdout"] || "")

        var st = kv["status"] || ""
        var expired = kv["expired"] || ""
        var validUntil = kv["valid_until"] || ""
        var trialRem = kv["trial_remaining_seconds"] || ""

        var state = "UNKNOWN"
        var rem = "—"

        if (st === "trial") {
            state = "TRIAL"
            rem = humanDuration(trialRem)
        } else if (st === "missing") {
            state = "MISSING"
            rem = "—"
        } else {
            if (expired === "yes") state = "EXPIRED"
            else if (validUntil !== "") state = "ACTIVE"
        }

        labelState.text = state
        labelRem.text = rem

        if (state === "ACTIVE") {
            badge.color = "#2ecc71"
        } else if (state === "TRIAL") {
            badge.color = "#f1c40f"
        } else if (state === "EXPIRED") {
            badge.color = "#e74c3c"
        } else if (state === "MISSING") {
            badge.color = "#e67e22"
        } else {
            badge.color = "#95a5a6"
        }
    }

    PlasmaCore.DataSource {
        id: execDS
        engine: "executable"
        connectedSources: ["/usr/sbin/saikyo-license status"]
        interval: 60000
        onNewData: {
            compute()
        }
    }

    Component.onCompleted: {
        compute()
    }

    Rectangle {
        id: badge
        anchors.fill: parent
        radius: 6
        color: "#95a5a6"
        opacity: 0.18

        border.width: 1
        border.color: badge.color
    }

    RowLayout {
        anchors.fill: parent
        anchors.margins: 6
        spacing: 6

        Rectangle {
            width: 10
            height: 10
            radius: 5
            color: badge.color
            opacity: 0.95
            Layout.alignment: Qt.AlignVCenter
        }

        PC3.Label {
            id: labelState
            text: "…"
            font.bold: true
            Layout.alignment: Qt.AlignVCenter
        }

        PC3.Label {
            id: labelRem
            text: "—"
            opacity: 0.85
            Layout.alignment: Qt.AlignVCenter
        }

        Item { Layout.fillWidth: true }

        PC3.ToolButton {
            text: "⚙"
            display: PC3.AbstractButton.TextOnly
            onClicked: {
                // best-effort open GUI
                execDS.connectedSources = ["/usr/bin/saikyo-license-kde"]
                execDS.connectedSources = ["/usr/sbin/saikyo-license status"]
            }
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            execDS.connectedSources = ["/usr/bin/saikyo-license-kde"]
            execDS.connectedSources = ["/usr/sbin/saikyo-license status"]
        }
        hoverEnabled: true
        cursorShape: Qt.PointingHandCursor
    }
}
