@using Microsoft.EntityFrameworkCore
@using Microsoft.Extensions.Caching.Memory
@using Microsoft.Extensions.Options
@using OpenArchival.Blazor.Components.CustomComponents
@using OpenArchival.Blazor.Config;
@using OpenArchival.Blazor.Blog
@using OpenArchival.Blazor.ArchiveDisplay

@page "/"

<PageTitle>Home</PageTitle>

<style>
    .banner-container {
        position: relative;
        width: calc(100% + 32px);
        margin-left: -16px;
        margin-right: -16px;
        margin-top: -16px;
        overflow: hidden;
    }

    .banner-container img {
        width: 100% !important;
        height: auto !important;
        display: block;
    }

    .floating-buttons {
        position: absolute;
        bottom: 24px;
        left: 0;
        right: 0;
        z-index: 10;
    }

    @@media (max-width: 600px) {
        .floating-buttons {
            position: static;
            padding-top: 16px;
            padding-bottom: 16px;
        }
    }
</style>

@if (_homePageConfig is null)
{
    <p>Loading content...</p> 
}
else
{
    
    @if (_homePageConfig.HomePageBanner is not null)
    {
        <div class="banner-container">
            <MudImage 
                ObjectPosition=ObjectPosition.Center 
                Src=@($"/api/files/{_homePageConfig.HomePageBanner.Id}/large")></MudImage>
            
            <div class="floating-buttons">
                <HomePageButtons></HomePageButtons>
            </div>
        </div>
        <MudDivider></MudDivider>
    }
    else
    {
        <HomePageButtons></HomePageButtons>
    }
    
    @if (!string.IsNullOrEmpty(_homePageConfig.Content)) {
        // Uses a custom CSS class to force the content to the width of the page regardless of what is in it
        // ql-editor class tells the quill css that it should apply its classes in this div. Quill is used by the rich text editor this
        // project uses
        <div class="dynamic-content-container ql-editor">
            @((MarkupString)_homePageConfig.Content)
        </div>
    }

    <LatestBlogPostsSlider />

    @if (_homePageConfig.SliderEntries != null)
    {
        @foreach (var sliderEntry in _homePageConfig.SliderEntries)
        {
            <SearchPageSlider SliderEntry="sliderEntry" />
        }
    }
}

@inject IOptions<ApplicationOptions> AppOptions;
@inject IMemoryCache MemoryCache;
@inject IDbContextFactory<ApplicationDbContext> ContextFactory;

@code {
    private string _htmlContent = $"<h1>HTML file not found for homepage. Create one at /admin/homepageeditor</h1>";
    private HomePageConfiguration? _homePageConfig = null;

    protected override async Task OnInitializedAsync()
    {
        _homePageConfig = await MemoryCache.GetOrCreateAsync(OpenArchivalConstants.HomePageConfigurationCacheKey, async entry =>
        {
            await using var context = await ContextFactory.CreateDbContextAsync();
            HomePageConfiguration? config = await context.HomePageConfiguration
                .Include(conf=>conf.HomePageBanner)
                .Include(conf=>conf.SliderEntries)
                    .ThenInclude(se => se.FilterTags)
                .FirstOrDefaultAsync();

            if (config is null)
            {
                return new HomePageConfiguration() { Content = "Failed to load HomePageConfiguration from the database!" };
            }

            return config;
        });

        StateHasChanged();
    }
}
