@using System.Text.Json
<div class="data-preview">
    <div class="d-flex" style="height: fit-content;">
        <MudToggleGroup Class="w-100" T="DataPreviewType" SelectionMode="SelectionMode.SingleSelection" @bind-Value="DataType" Color="Color.Default"
                        Outlined="false" SelectedClass="custom-background" @bind-Value:after="DataTypeChanged" Disabled="!RobotState.Online">
            <MudToggleItem Value="@(DataPreviewType.Visualization)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Node)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Edge)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Order)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Action)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Infomation)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Battery)" Style="text-transform:none" />
            <MudToggleItem Value="@(DataPreviewType.Error)" Style="text-transform:none" />
        </MudToggleGroup>
    </div>
    <div class="content">
        @DataPreview
    </div>
</div>

<style>
    .custom-background {
        background-color: #283593;
        color: white !important;
    }

        .custom-background:hover {
            background-color: #283593;
            color: white !important;
        }

</style>

@code {
    private DataPreviewType DataType;
    private string DataPreview = "";

    private RobotVDA5050StateDto RobotState = new();

    public void UpdateState(RobotVDA5050StateDto robotstate)
    {
        RobotState = robotstate;
        DataTypeChanged();
        StateHasChanged();
    }

    private void DataTypeChanged()
    {
        switch (DataType)
        {
            case DataPreviewType.Visualization:
                DataPreview = JsonSerializer.Serialize(RobotState.Visualization, JsonOptionExtends.Read);
                break;
            case DataPreviewType.Node:
                DataPreview = JsonSerializer.Serialize(RobotState.State.NodeStates, JsonOptionExtends.Read);
                break;
            case DataPreviewType.Edge:
                DataPreview = JsonSerializer.Serialize(RobotState.State.EdgeStates, JsonOptionExtends.Read);
                break;
            case DataPreviewType.Order:
                DataPreview = JsonSerializer.Serialize(GetOrderState(RobotState), JsonOptionExtends.Read);
                break;
            case DataPreviewType.Action:
                DataPreview = JsonSerializer.Serialize(RobotState.State.ActionStates, JsonOptionExtends.Read);
                break;
            case DataPreviewType.Infomation:
                DataPreview = JsonSerializer.Serialize(RobotState.State.Information, JsonOptionExtends.Read);
                break;
            case DataPreviewType.Battery:
                DataPreview = JsonSerializer.Serialize(RobotState.State.BatteryState, JsonOptionExtends.Read);
                break;
            case DataPreviewType.Error:
                DataPreview = JsonSerializer.Serialize(RobotState.State.Errors, JsonOptionExtends.Read);
                break;
        };
        StateHasChanged();
    }

    private OrderState GetOrderState(RobotVDA5050StateDto state)
    {
        return new()
            {
                OrderId = state.State.OrderId,
                OrderUpdateId = state.State.OrderUpdateId,
                ZoneSetId = state.State.ZoneSetId,
                LastNodeId = state.State.LastNodeId,
                LastNodeSequenceId = state.State.LastNodeSequenceId,
                Driving = state.State.Driving,
                Paused = state.State.Paused,
                NewBaseRequest = state.State.NewBaseRequest,
                DistanceSinceLastNode = state.State.DistanceSinceLastNode,
                OperatingMode = state.State.OperatingMode,
                IsCanceled = state.OrderState.IsCanceled,
                IsCompleted = state.OrderState.IsCompleted,
                IsError = state.OrderState.IsError,
                IsProcessing = state.OrderState.IsProcessing,
            };
    }

    public enum DataPreviewType
    {
        Visualization,
        Node,
        Edge,
        Order,
        Action,
        Infomation,
        Battery,
        Error
    }

    public class OrderState
    {
        public string OrderId { get; set; } = "";
        public int OrderUpdateId { get; set; }
        public string ZoneSetId { get; set; } = "";
        public string LastNodeId { get; set; } = "";
        public int LastNodeSequenceId { get; set; }
        public bool Driving { get; set; }
        public bool Paused { get; set; }
        public bool NewBaseRequest { get; set; }
        public double DistanceSinceLastNode { get; set; }
        public string OperatingMode { get; set; } = "";
        public bool IsError { get; set; }
        public bool IsCompleted { get; set; }
        public bool IsProcessing { get; set; }
        public bool IsCanceled { get; set; }
    }
}
