@using LANCommander.Components.FileManagerComponents;
@using LANCommander.Data.Models
@using LANCommander.Extensions
@using LANCommander.Models;
@using System.IO.Compression;
@inject ModalService ModalService

<Space Direction="DirectionVHType.Vertical" Size="@("large")" Style="width: 100%">
    <SpaceItem>
        <Table TItem="Data.Models.Action" DataSource="@Actions" HidePagination="true" Style="border: 1px solid #f0f0f0" Responsive>
            <PropertyColumn Property="a => a.Name">
                <Input Type="text" @bind-Value="context.Name" />
            </PropertyColumn>
            <PropertyColumn Property="a => a.Path">
                <FilePicker @bind-Value="context.Path" ArchiveId="@ArchiveId" AllowDirectories="true" />
            </PropertyColumn>
            <PropertyColumn Property="a => a.Arguments">
                <Input Type="text" @bind-Value="context.Arguments" />
            </PropertyColumn>
            <PropertyColumn Property="a => a.WorkingDirectory" Title="Working Dir">
                <Input Type="text" @bind-Value="context.WorkingDirectory" />
            </PropertyColumn>
            <PropertyColumn Property="a => a.PrimaryAction" Title="Primary" Style="text-align: center">
                <Checkbox @bind-Checked="context.PrimaryAction" />
            </PropertyColumn>
            <ActionColumn>
                <Space Style="display: flex; justify-content: end">
                    <SpaceItem>
                        <Button OnClick="() => MoveUp(context)" Icon="@IconType.Outline.Up" Type="@ButtonType.Text" />
                        <Button OnClick="() => MoveDown(context)" Icon="@IconType.Outline.Down" Type="@ButtonType.Text" />

                        <Popconfirm OnConfirm="() => RemoveAction(context)" Title="Are you sure you want to remove this action?">
                            <Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
                        </Popconfirm>
                    </SpaceItem>
                </Space>
            </ActionColumn>
        </Table>
    </SpaceItem>

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

@code {
    [Parameter] public ICollection<Data.Models.Action> Actions { get; set; }
    [Parameter] public EventCallback<ICollection<Data.Models.Action>> ActionsChanged { get; set; }

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

    protected override async Task OnInitializedAsync()
    {
        if (Actions == null)
            Actions = new List<Data.Models.Action>();
    }

    private async Task AddAction()
    {
        if (Actions == null)
            Actions = new List<Data.Models.Action>();

        Actions.Add(new Data.Models.Action()
        {
            PrimaryAction = Actions.Count == 0,
            SortOrder = Actions.Count,
            GameId = GameId
        });
    }

    private async Task RemoveAction(Data.Models.Action action)
    {
        Actions.Remove(action);
    }

    private async Task MoveUp(Data.Models.Action action)
    {
        if (action.SortOrder > 0)
            Move(action.SortOrder, action.SortOrder - 1);
    }

    private async Task MoveDown(Data.Models.Action action)
    {
        if (action.SortOrder < Actions.Count + 1)
            Move(action.SortOrder, action.SortOrder + 1);
    }

    private async void BrowseForActionPath(Data.Models.Action action)
    {
        var modalOptions = new ModalOptions()
            {
                Title = "Choose Action Executable",
                Maximizable = false,
                DefaultMaximized = true,
                Closable = true,
                OkText = "Select File"
            };

        var browserOptions = new FilePickerOptions()
        {
            ArchiveId = ArchiveId,
            Select = true,
            Multiple = false
        };

        var modalRef = await ModalService.CreateModalAsync<FilePickerDialog, FilePickerOptions, IEnumerable<IFileManagerEntry>>(modalOptions, browserOptions);

        modalRef.OnOk = (results) =>
        {
            action.Path = results.FirstOrDefault().Path;

            var parts = action.Path.Split('/');

            if (parts.Length > 1)
            {
                action.Path = parts.Last();
                action.WorkingDirectory = "{InstallDir}/" + String.Join('/', parts.Take(parts.Length - 1));
            }

            StateHasChanged();
            return Task.CompletedTask;
        };
    }

    private void Move(int oldIndex, int newIndex)
    {
        foreach (var action in Actions)
        {
            if (action.SortOrder == oldIndex)
                action.SortOrder = newIndex;

            if (action.SortOrder == newIndex)
                action.SortOrder = oldIndex;
        }
    }
}
