import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

import clide.module 1.0
import Logger 1.0

Item {
    id: root

    property var fullPaths: []
    required property string path
    property var segments: []

    signal crumbClicked(string path)

    function rebuildSegments() {
        var cleaned = path;
        if (cleaned.endsWith("/"))
            cleaned = cleaned.slice(0, -1);
        var parts = cleaned.split("/");
        root.segments = [];
        root.fullPaths = [];
        var current = "";
        Logger.trace("Building segments for path: " + cleaned);
        for (var i = 0; i < parts.length; ++i) {
            if (parts[i] === "") {
                current = "/";
                root.segments.push("/");
                root.fullPaths.push("/");
            } else {
                if (current === "/")
                    current += parts[i];
                else
                    current += "/" + parts[i];
                Logger.trace("Pushing path: " + parts[i] + " Current: " + current);
                root.segments.push(parts[i]);
                root.fullPaths.push(current);
            }
        }
        rep.model = root.segments;
    }

    anchors.leftMargin: 20
    height: breadcrumbRow.implicitHeight
    width: parent.width

    Component.onCompleted: rebuildSegments()
    onPathChanged: rebuildSegments()

    Flow {
        id: breadcrumbRow

        Repeater {
            id: rep

            model: root.segments

            delegate: Text {
                id: linkText

                required property string modelData

                function getText() {
                    if (modelData === "/") {
                        return modelData;
                    }
                    Logger.trace("Getting valid text:" + modelData);
                    return modelData + "/";
                }

                color: mouseArea.containsMouse ? "#2a7fff" : RustColors.explorer_text
                font.underline: mouseArea.containsMouse
                text: getText()

                MouseArea {
                    id: mouseArea

                    anchors.fill: parent
                    hoverEnabled: true

                    onClicked: {
                        console.log("Breadcrumb clicked:", root.fullPaths[root.segments.indexOf(modelData)]);
                        root.crumbClicked(root.fullPaths[root.segments.indexOf(modelData)]);
                    }
                }
            }
        }
    }
    TapHandler {
        acceptedButtons: Qt.RightButton

        onSingleTapped: (eventPoint, button) => contextMenu.popup()
    }
    ClideMenu {
        id: contextMenu

        ClideMenuItem {
            action: Action {
                text: qsTr("Reset root")

                onTriggered: {
                    Logger.log("Resetting root directory: " + clideTreeView.originalRootDirectory);
                    clideTreeView.rootDirectory = clideTreeView.originalRootDirectory;
                }
            }
        }
    }
}
