@using RobotNet.RobotShares.VDA5050.State

<div class="d-flex h-100 d-flex flex-column overflow-x-hidden overflow-y-auto p-2" style="width: 250px" hidden="@(!IsExpand)">
    <MudExpansionPanels Elevation="0" Style="border: 1px solid silver">
        <MudExpansionPanel Expanded="true">
            <TitleContent>
                <MudText>Visualization</MudText>
            </TitleContent>
            <ChildContent>
                <MudTextField Class="mb-2" Label="X" Value="RobotStateSelected.AgvPosition.X" Variant="Variant.Outlined" Margin="Margin.Dense" />
                <MudTextField Class="my-2" Label="Y" Value="RobotStateSelected.AgvPosition.Y" Variant="Variant.Outlined" Margin="Margin.Dense" />
                <MudTextField Class="my-2" Label="Theta" Value="RobotStateSelected.AgvPosition.Theta" Variant="Variant.Outlined" Margin="Margin.Dense" />
                <MudTextField Class="my-2" Label="Velocity X" Value="RobotStateSelected.AgvVelocity.Vx" Variant="Variant.Outlined" Margin="Margin.Dense" />
                <MudTextField Class="mt-2" Label="Velocity Y" Value="RobotStateSelected.AgvVelocity.Vy" Variant="Variant.Outlined" Margin="Margin.Dense" />
                <MudTextField Class="mt-2" Label="Angular Velocity" Value="RobotStateSelected.AgvVelocity.Omega" Variant="Variant.Outlined" Margin="Margin.Dense" />
            </ChildContent>
        </MudExpansionPanel>
    </MudExpansionPanels>

    <MudExpansionPanels Elevation="0" Class="mt-2" Style="border: 1px solid silver">
        <MudExpansionPanel Expanded="true">
            <TitleContent>
                <MudText>Localization</MudText>
            </TitleContent>
            <ChildContent>
                <MudText Class="ms-2">Initialized: @RobotStateSelected.AgvPosition.PositionInitialized</MudText>
                <MudText Class="ms-2">Quality: @RobotStateSelected.AgvPosition.LocalizationScore</MudText>
                <MudText Class="ms-2">DeviationRange: @RobotStateSelected.AgvPosition.DeviationRange</MudText>
            </ChildContent>
        </MudExpansionPanel>
    </MudExpansionPanels>

    <MudExpansionPanels Elevation="0" Class="mt-2" Style="border: 1px solid silver">
        <MudExpansionPanel Expanded="true">
            <TitleContent>
                <MudText>Battery</MudText>
            </TitleContent>
            <ChildContent>
                <MudProgressLinear Class="m-2" Color="@(RobotStateSelected.Battery.BatteryHealth < 10 ? Color.Error : RobotStateSelected.Battery.BatteryHealth > 20 ? Color.Success : Color.Warning)" Rounded Striped Size="Size.Large" Value="@RobotStateSelected.Battery.BatteryHealth" />
                <MudText Class="ms-2">@($"Health: {Math.Round(RobotStateSelected.Battery.BatteryHealth, 1)}%")</MudText>
                <MudText Class="ms-2">@($"Voltage: {Math.Round(RobotStateSelected.Battery.BatteryVoltage, 1)}V")</MudText>
                <MudText Class="ms-2">@($"State: {Math.Round(RobotStateSelected.Battery.BatteryCharge, 1)}")</MudText>
                <MudText Class="ms-2">@($"Charging: {RobotStateSelected.Battery.Charging}")</MudText>
            </ChildContent>
        </MudExpansionPanel>
    </MudExpansionPanels>

    <MudExpansionPanels Elevation="0" Class="mt-2" Style="border: 1px solid silver">
        <MudExpansionPanel>
            <TitleContent>
                <div class="d-flex">
                    <MudText>Errors</MudText>
                    <MudBadge Content="RobotStateSelected.Errors.Length" Color="Color.Info" Overlap="true" Class="d-flex ml-auto">
                        <MudIcon Icon="@Icons.Material.Filled.Info" Color="Color.Secondary" />
                    </MudBadge>
                </div>
            </TitleContent>
            <ChildContent>
                @foreach (var error in RobotStateSelected.Errors)
                {
                    <MudTooltip Text="@error.ErrorDescription" Placement="Placement.Top" Color="Color.Info">
                        <MudButton Class="m-2" Color="@(error.ErrorLevel == ErrorLevel.FATAL.ToString() ? Color.Error : Color.Warning)" Variant="Variant.Filled" Size="Size.Small" Style="text-transform:none">@error.ErrorType</MudButton>
                    </MudTooltip>
                }
            </ChildContent>
        </MudExpansionPanel>
    </MudExpansionPanels>

    <MudExpansionPanels Elevation="0" Class="mt-2" Style="border: 1px solid silver">
        <MudExpansionPanel>
            <TitleContent>
                <div class="d-flex">
                    <MudText>Notification</MudText>
                    <MudBadge Content="RobotStateSelected.Infomations.Length" Color="Color.Info" Overlap="true" Class="d-flex ml-auto">
                        <MudIcon Icon="@Icons.Material.Filled.Notifications" Color="Color.Warning" />
                    </MudBadge>
                </div>
            </TitleContent>
            <ChildContent>
                <div class="d-flex flex-column">
                    @foreach (var info in RobotStateSelected.Infomations)
                    {
                        <MudTooltip Text="@info.InfoDescription" Placement="Placement.Top" Color="Color.Info">
                            <MudButton Class="m-2" Color="@(info.InfoLevel == InfoLevel.INFO.ToString() ? Color.Info : Color.Default)" Variant="Variant.Filled" Size="Size.Small" Style="text-transform:none">@info.InfoType</MudButton>
                        </MudTooltip>
                    }
                </div>
            </ChildContent>
        </MudExpansionPanel>
    </MudExpansionPanels>
</div>

@code {
    private RobotInfomationDto RobotStateSelected = new();
    private bool IsExpand = true;

    public void ExpandedClick(bool value)
    {
        IsExpand = value;
        StateHasChanged();
    }

    public void UpdateState(RobotInfomationDto data)
    {
        RobotStateSelected.Battery.BatteryVoltage = data.Battery.BatteryVoltage;
        RobotStateSelected.Battery.BatteryHealth = data.Battery.BatteryHealth;
        RobotStateSelected.Battery.BatteryCharge = data.Battery.BatteryCharge;
        RobotStateSelected.Battery.Charging = data.Battery.Charging;

        RobotStateSelected.AgvPosition.LocalizationScore = data.AgvPosition.LocalizationScore;
        RobotStateSelected.AgvPosition.PositionInitialized = data.AgvPosition.PositionInitialized;
        RobotStateSelected.AgvPosition.DeviationRange = data.AgvPosition.DeviationRange;

        RobotStateSelected.AgvPosition.X = data.AgvPosition.X;
        RobotStateSelected.AgvPosition.Y = data.AgvPosition.Y;
        RobotStateSelected.AgvPosition.Theta = data.AgvPosition.Theta;
        RobotStateSelected.AgvVelocity.Vx = data.AgvVelocity.Vx;
        RobotStateSelected.AgvVelocity.Vy = data.AgvVelocity.Vy;
        RobotStateSelected.AgvVelocity.Omega = data.AgvVelocity.Omega;

        RobotStateSelected.Errors = [.. data.Errors];
        RobotStateSelected.Infomations = [.. data.Infomations];
        StateHasChanged();
    }
}