import Quickshell
import Quickshell.Io
import QtQuick

import qs.services

ShellRoot {
    id: root

    property int refreshCalls: 0

    FileView {
        id: tokenFile
        path: Quickshell.env("PANAMA_TEST_TOKEN_FILE")
        blockLoading: true
        printErrors: false
    }

    Component.onCompleted: {
        HomeAssistant.fixtureMode = true;
        HomeAssistantConfig.refreshHomeAssistant = function() { root.refreshCalls++; };
    }

    IpcHandler {
        target: "home-assistant-config-test"

        function save(url: string, entities: string): bool {
            return HomeAssistantConfig.save(url, entities, "");
        }

        function saveWithToken(url: string, entities: string): bool {
            return HomeAssistantConfig.save(url, entities, tokenFile.text());
        }

        function clearToken(): bool {
            return HomeAssistantConfig.clearToken();
        }

        function refresh(): void {
            HomeAssistantConfig.refresh();
        }

        function status(): string {
            return JSON.stringify({
                url: HomeAssistantConfig.url,
                entities: HomeAssistantConfig.entities,
                tokenConfigured: HomeAssistantConfig.tokenConfigured,
                configured: HomeAssistantConfig.configured,
                busy: HomeAssistantConfig.busy,
                lastError: HomeAssistantConfig.lastError,
                pendingPayloadEmpty: HomeAssistantConfig.pendingPayload === "",
                refreshCalls: root.refreshCalls
            });
        }
    }
}
