@using LANCommander.Data.Enums
@using LANCommander.Data.Models
@using LANCommander.Extensions;



<Space Direction="DirectionVHType.Vertical" Size="@("large")" Style="width: 100%">
    @if (Value == null || Value.Count == 0)
    {
        <Empty />
    }

    @foreach (var console in Value)
    {
        <SpaceItem>
            @if (console.Type == ServerConsoleType.RCON)
            {
                <Card Size="small" Title="@console.Name">
                    <Extra>
                        <Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger OnClick="() => Remove(console)" />
                    </Extra>
                    <Body>
                        <Form Layout="@FormLayout.Vertical" Model="@console">
                            <GridRow Gutter="16">
                                <GridCol Span="8">
                                    <FormItem Label="Type">
                                        <Select @bind-Value="context.Type" TItem="ServerConsoleType" TItemValue="ServerConsoleType" DataSource="Enum.GetValues<ServerConsoleType>()">
                                            <LabelTemplate Context="Value">@Value.GetDisplayName()</LabelTemplate>
                                            <ItemTemplate Context="Value">@Value.GetDisplayName()</ItemTemplate>
                                        </Select>
                                    </FormItem>
                                    <FormItem Label="Name">
                                        <Input @bind-Value="context.Name" BindOnInput />
                                    </FormItem>
                                </GridCol>

                                <GridCol Span="16">
                                    <FormItem Label="Server">
                                        <InputGroup Compact>
                                            <Input @bind-Value="context.Host" Placeholder="Host" />
                                            <Input @bind-Value="context.Port" Placeholder="Port" Style="width: 25%" />
                                        </InputGroup>
                                    </FormItem>
                                    <FormItem Label="Password">
                                        <InputPassword @bind-Value="context.Password" Placeholder="Password" />
                                    </FormItem>
                                </GridCol>
                            </GridRow>
                        </Form>
                    </Body>
                </Card>
            }
            else if (console.Type == ServerConsoleType.LogFile)
            {
                <Card Size="small" Title="@console.Name">
                    <Extra>
                        <Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger OnClick="() => Remove(console)" />
                    </Extra>
                    <Body>
                        <Form Layout="@FormLayout.Vertical" Model="@console">
                            <GridRow Gutter="16">
                                <GridCol Span="8">
                                    <FormItem Label="Type">
                                        <Select @bind-Value="context.Type" TItem="ServerConsoleType" TItemValue="ServerConsoleType" DataSource="Enum.GetValues<ServerConsoleType>()">
                                            <LabelTemplate Context="Value">@Value.GetDisplayName()</LabelTemplate>
                                            <ItemTemplate Context="Value">@Value.GetDisplayName()</ItemTemplate>
                                        </Select>
                                    </FormItem>
                                    <FormItem Label="Name">
                                        <Input Type="text" @bind-Value="context.Name" />
                                    </FormItem>
                                </GridCol>
                                <GridCol Span="16">
                                    <FormItem Label="Path">
                                        <Input Type="text" @bind-Value="context.Path" />
                                    </FormItem>
                                </GridCol>
                            </GridRow>
                        </Form>
                    </Body>
                </Card>
            }
        </SpaceItem>
    }

    <SpaceItem>
        <GridRow Justify="end">
            <GridCol>
                <Button OnClick="Add" Type="@ButtonType.Primary">Add Console</Button>
            </GridCol>
        </GridRow>
    </SpaceItem>
</Space>

@code {
    [Parameter] public ICollection<ServerConsole> Value { get; set; }
    [Parameter] public EventCallback<ICollection<ServerConsole>> ValueChanged { get; set; }
    [Parameter] public Guid ServerId { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (Value == null)
            Value = new List<ServerConsole>();

        if (ValueChanged.HasDelegate)
            await ValueChanged.InvokeAsync(Value);

        StateHasChanged();
    }

    private async Task Add()
    {
        if (Value == null)
            Value = new List<ServerConsole>();

        Value.Add(new ServerConsole
        {
            ServerId = ServerId
        });

        if (ValueChanged.HasDelegate)
            await ValueChanged.InvokeAsync(Value);

        StateHasChanged();
    }

    private async Task Remove(ServerConsole console)
    {
        Value.Remove(console);

        if (ValueChanged.HasDelegate)
            await ValueChanged.InvokeAsync(Value);

        StateHasChanged();
    }
}
