@page "/script-editor"
@attribute [Authorize]
@implements IAsyncDisposable

@using RobotNet.Script.Shares
@using RobotNet.WebApp.Clients
@using RobotNet.WebApp.Scripts.Components
@using RobotNet.WebApp.Scripts.Models

@inject IHttpClientFactory httpFactory
@inject ProcessorHubClient processorHub

<PageTitle>Script Editor</PageTitle>

<CascadingValue Value="Workspace">
    <div class="editor-container" @onmouseup="DisableResize" @onmouseleave="DisableResize" @onmousemove="MouseMoveOnEditorContainer"
         @oncontextmenu:stopPropagation @oncontextmenu:preventDefault>
        <div class="editor-hierachy" style="width: @(HierachyWidth)px;">
            <SidebarActions />
            <EditorHierachy @ref="hierachyRef" />
        </div>
        <div class="split-vertical-view" @onmousedown:preventDefault @onmousedown:stopPropagation @onmousedown="EnableResizeHierachy"></div>
        <div class="editor-content">
            <Editor @ref="editorRef" />
            <div class="split-horizontal-view" @onmousedown:preventDefault @onmousedown:stopPropagation @onmousedown="EnableResizeConsole"></div>
            <div class="editor-console" style="height: @(ConsoleHeight)px;">
                <ProcessorControl @ref="processorControlRef" />
            </div>
        </div>
    </div>
</CascadingValue>


@code {
    private EditorHierachy hierachyRef = default!;
    private Editor editorRef = null!;
    private ProcessorControl processorControlRef = null!;

    private int HierachyWidth = 300;
    private int ConsoleHeight = 300;
    private bool isDraggingHierachy = false;
    private bool isDraggingConsole = false;
    private ScriptWorkspace Workspace = default!;


    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        Workspace = new(httpFactory);
        await Workspace.InitializeAsync();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if(firstRender)
        {
            processorHub.StateChanged += OnProcessStateChanged;
            await processorHub.StartAsync();

            OnProcessStateChanged(await processorHub.GetState());
        }
    }


    private void OnProcessStateChanged(ProcessorState state)
    {
        Workspace.ProcessorState = state;
        editorRef.OnProcessStateChanged(state);
    }

    private void EnableResizeHierachy()
    {
        isDraggingHierachy = true;
    }

    private void EnableResizeConsole()
    {
        isDraggingConsole = true;
    }

    private void DisableResize()
    {
        isDraggingHierachy = false;
        isDraggingConsole = false;
    }

    private void MouseMoveOnEditorContainer(MouseEventArgs e)
    {
        if (isDraggingHierachy)
        {
            HierachyWidth += (int)e.MovementX;

            if (HierachyWidth < 150)
            {
                HierachyWidth = 150;
            }
            else if (HierachyWidth > 500)
            {
                HierachyWidth = 500;
            }
        }
        else if (isDraggingConsole)
        {
            // ConsoleHeight -= (int)e.MovementY;
            // StateHasChanged();
            // Console.WriteLine($"ConsoleHeight={ConsoleHeight}");
        }
    }

    public async ValueTask DisposeAsync()
    {
        processorHub.StateChanged -= OnProcessStateChanged;
        await processorHub.StopAsync();

        Workspace.Dispose();
        GC.SuppressFinalize(this);
    }
}
