@using LANCommander.Data.Models
@using LANCommander.Extensions
@inject IDialogService DialogService

<MudTable Items="@OrderedActions" Elevation="0" Dense="true">
    <HeaderContent>
        <MudTh>Name</MudTh>
        <MudTh>Path</MudTh>
        <MudTh>Arguments</MudTh>
        <MudTh>Working Dir</MudTh>
        <MudTh>Primary</MudTh>
        <MudTh></MudTh>
    </HeaderContent>

    <RowTemplate>
        <MudTd><MudTextField @bind-Value="context.Name" /></MudTd>
        <MudTd><MudTextField @bind-Value="context.Path" Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Folder" OnAdornmentClick="() => BrowseForActionPath(context)" /></MudTd>
        <MudTd><MudTextField @bind-Value="context.Arguments" /></MudTd>
        <MudTd><MudTextField @bind-Value="context.WorkingDirectory" /></MudTd>
        <MudTd><MudCheckBox @bind-Checked="context.PrimaryAction" Color="Color.Primary" /></MudTd>
        <MudTd Class="d-flex flex-nowrap justify-end">
            <MudIconButton OnClick="() => MoveUp(context)" Icon="@Icons.Material.Filled.ArrowUpward"></MudIconButton>
            <MudIconButton OnClick="() => MoveDown(context)" Icon="@Icons.Material.Filled.ArrowDownward"></MudIconButton>
            <MudIconButton OnClick="() => RemoveAction(context)" Color="Color.Error" Icon="@Icons.Material.Filled.Close"></MudIconButton>
        </MudTd>
    </RowTemplate>
</MudTable>

<MudPaper Elevation="0" Class="d-flex justify-end mt-3">
    <MudButton Variant="Variant.Filled" Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add" OnClick="AddAction">Add</MudButton>
</MudPaper>

@code {
    [Parameter] public IEnumerable<Data.Models.Action> Actions { get; set; }
    [Parameter] public Guid ArchiveId { get; set; }

    private List<Data.Models.Action> OrderedActions { get; set; }

    protected override async Task OnInitializedAsync()
    {
        OrderedActions = Actions.OrderBy(a => a.SortOrder).ToList();
        FixSortOrder();
    }

    private void AddAction()
    {
        if (OrderedActions == null)
            OrderedActions = new List<Data.Models.Action>();

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

    private void RemoveAction(Data.Models.Action action)
    {
        OrderedActions.Remove(action);
    }

    private void MoveUp(Data.Models.Action action)
    {
        if (action.SortOrder > 0)
            OrderedActions.Move(action, action.SortOrder - 1);

        FixSortOrder();
    }

    private void MoveDown(Data.Models.Action action)
    {
        if (action.SortOrder < OrderedActions.Count + 1)
            OrderedActions.Move(action, action.SortOrder + 1);

        FixSortOrder();
    }

    private async void BrowseForActionPath(Data.Models.Action action)
    {
        var parameters = new DialogParameters
        {
            ["ArchiveId"] = ArchiveId
        };

        var dialog = await DialogService.ShowAsync<ArchiveFileSelectorDialog>("File Selector", parameters);
        var result = await dialog.Result;

        if (!result.Canceled)
        {
            action.Path = result.Data as string;

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

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

            StateHasChanged();
        }
    }

    private void FixSortOrder()
    {
        int i = 0;

        foreach (var action in OrderedActions)
        {
            action.SortOrder = i;

            i++;
        }

        Actions = OrderedActions;
    }
}
