@page "/Redistributables/{id:guid}"
@page "/Redistributables/{id:guid}/{panel}"
@page "/Redistributables/Add"
@using LANCommander.Data.Enums;
@attribute [Authorize(Roles = "Administrator")]
@inject RedistributableService RedistributableService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager

<Layout Class="panel-layout" Style="padding: 24px 0;">
    <Sider Width="200">
        <Menu Mode="@MenuMode.Inline" Style="height: 100%;">
            <MenuItem RouterLink="@($"/Redistributables/{Redistributable.Id}/General")">General</MenuItem>

            @if (Redistributable.Id != Guid.Empty)
            {
                <MenuItem RouterLink="@($"/Redistributables/{Redistributable.Id}/Scripts")">Scripts</MenuItem>
                <MenuItem RouterLink="@($"/Redistributables/{Redistributable.Id}/Archives")">Archives</MenuItem>
            }
        </Menu>
    </Sider>

    <Content>
        <PageHeader>
            <PageHeaderTitle>
                @if (Panel == null)
                {
                    <Text>Add New Redistributable</Text>
                }
                else
                {
                    @Panel
                }
            </PageHeaderTitle>
            <PageHeaderExtra>
                <Button Type="@ButtonType.Primary" OnClick="Save">Save</Button>
            </PageHeaderExtra>
        </PageHeader>


        <div class="panel-layout-content">
            <div data-panel="General">
                <Form Model="@Redistributable" Layout="@FormLayout.Vertical">
                    <FormItem Label="Name">
                        <Input @bind-Value="@context.Name" />
                    </FormItem>
                    <FormItem Label="Notes">
                        <TextArea @bind-Value="@context.Notes" MaxLength=2000 ShowCount />
                    </FormItem>
                    <FormItem Label="Description">
                        <TextArea @bind-Value="@context.Description" MaxLength=500 ShowCount />
                    </FormItem>
                </Form>
             </div>

            @if (Redistributable != null && Redistributable.Id != Guid.Empty)
            {
                <div data-panel="Scripts">
                    <ScriptEditor RedistributableId="Redistributable.Id" ArchiveId="@LatestArchiveId" AllowedTypes="new ScriptType[] { ScriptType.Install, ScriptType.DetectInstall }" />
                </div>

                <div data-panel="Archives">
                    <ArchiveEditor RedistributableId="Redistributable.Id" />
                </div>
            }
        </div>
    </Content>
</Layout>

@if (!String.IsNullOrWhiteSpace(Panel))
{
    <style>
        .panel-layout [data-panel="@Panel"] {
            display: block;
        }
    </style>
}
else
{
    <style>
        .panel-layout [data-panel="General"] {
            display: block;
        }
    </style>
}

@code {
    [Parameter] public Guid Id { get; set; }
    [Parameter] public string Panel { get; set; }

    Redistributable Redistributable;

    private Guid LatestArchiveId
    {
        get
        {
            if (Redistributable != null && Redistributable.Archives != null && Redistributable.Archives.Count > 0)
                return Redistributable.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Id;
            else
                return Guid.Empty;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        if (Id == Guid.Empty)
            Redistributable = new Redistributable();
        else
            Redistributable = await RedistributableService.Get(Id);
    }

    private async Task Save()
    {
        try
        {
            if (Redistributable.Id != Guid.Empty)
            {
                Redistributable = await RedistributableService.Update(Redistributable);

                await MessageService.Success("Redistributable updated!");
            }
            else
            {
                Redistributable = await RedistributableService.Add(Redistributable);

                NavigationManager.LocationChanged += NotifyRedistributableAdded;

                NavigationManager.NavigateTo($"/Redistributables/{Redistributable.Id}");
            }
        }
        catch (Exception ex)
        {
            await MessageService.Error("Could not save!");
        }
    }

    private void NotifyRedistributableAdded(object? sender, LocationChangedEventArgs e)
    {
        NavigationManager.LocationChanged -= NotifyRedistributableAdded;

        MessageService.Success("Redistributable added!");
    }
}