@using SharedLibraryCore.Interfaces;

@model IEnumerable<IClientMeta>
@{
    Layout = null;

    var lastHeaderEventDate = DateTime.UtcNow;

    TimeSpan TimeSpanForEvent(DateTime occuredAt)
    {
        var timePassed = DateTime.UtcNow - occuredAt;
        var daysPassed = timePassed.TotalDays;
        var minutesPassed = timePassed.TotalMinutes;

        if (minutesPassed <= 60)
        {
            return TimeSpan.FromMinutes(5);
        }

        if (minutesPassed > 60 && daysPassed <= 1)
        {
            return TimeSpan.FromHours(1);
        }

        if (daysPassed is > 1 and <= 7)
        {
            return TimeSpan.FromDays(1);
        }

        if (daysPassed is > 7 and <= 31)
        {
            return TimeSpan.FromDays(31);
        }

        if (daysPassed is > 31 and <= 365)
        {
            return TimeSpan.FromDays(31);
        }

        return TimeSpan.FromDays(365);
    }
}

@{ var start = 0; }
@foreach (var meta in Model.OrderByDescending(meta => meta.When))
{
    if (lastHeaderEventDate - meta.When > TimeSpanForEvent(lastHeaderEventDate))
    {
        TempData["ShowMetaHeader"] = true;
        lastHeaderEventDate = meta.When;
    }
    else
    {
        TempData["ShowMetaHeader"] = false;
    }
    start++;

    <div class="profile-meta-entry loader-data-time" data-time="@meta.When.ToFileTimeUtc()" onclick="$('#metaContextDateToggle@(meta.When.ToFileTimeUtc())').show()">
        <partial name="~/Views/Client/Profile/Meta/_@(meta.GetType().Name).cshtml" model="meta"/>
        <div style="display:none" id="metaContextDateToggle@(meta.When.ToFileTimeUtc())">
            @foreach (var match in Utilities.SplitTranslationTokens("WEBFRONT_META_TIME_CONTEXT"))
            {
                if (match.IsInterpolation && match.MatchValue == "event")
                {
                    <span class="text-light-dm text-dark-lm">@meta.When.ToStandardFormat()</span>
                }

                else
                {
                    <span>@match.MatchValue</span>
                }
            }
        </div>
    </div>
}
