@using LANCommander.Data.Enums
@using LANCommander.SDK.Enums;

<Space Direction="DirectionVHType.Vertical" Size="@("large")" Style="width: 100%">
    <SpaceItem>
        <Table TItem="SavePath" DataSource="@Value" HidePagination="true" Responsive>
            <PropertyColumn Property="p => p.Type">
                <Select @bind-Value="context.Type" TItem="SavePathType" TItemValue="SavePathType" DataSource="Enum.GetValues<SavePathType>()" />
            </PropertyColumn>
            <PropertyColumn Property="p => p.Path">
                @if (context.Type == SavePathType.Registry)
                {
                    <InputRegistry @bind-Value="context.Path" />
                }
                else
                {
                    <FilePicker @bind-Value="context.Path" ArchiveId="@ArchiveId" AllowDirectories="true" />
                }
            </PropertyColumn>
            <PropertyColumn Property="p => p.IsRegex" Title="Regex">
                <Checkbox @bind-Checked="context.IsRegex" />
            </PropertyColumn>
            <ActionColumn>
                <Space Style="display: flex; justify-content: end">
                    <SpaceItem>
                        <Button OnClick="() => RemovePath(context)" Type="@ButtonType.Text" Danger Icon="@IconType.Outline.Close" />
                    </SpaceItem>
                </Space>
            </ActionColumn>
        </Table>
    </SpaceItem>

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

@code {
    [Parameter] public ICollection<SavePath> Value { get; set; } = new List<SavePath>();
    [Parameter] public EventCallback<ICollection<SavePath>> ValueChanged { get; set; }

    [Parameter] public Guid GameId { get; set; }
    [Parameter] public Guid ArchiveId { get; set; }

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

    private async Task AddPath()
    {
        if (Value == null)
            Value = new List<SavePath>();

        Value.Add(new SavePath()
        {
            GameId = GameId
        });
    }

    private async Task RemovePath(SavePath path)
    {
        Value.Remove(path);
    }
}
