@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Auth
@using System.ComponentModel.DataAnnotations
@using System.Text.RegularExpressions
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navigationManager;


<div class="threadHeader">
    @if (showOpenThread)
    {
        <span>[</span>
        <a class="toggleOpened" onclick="@ToggleOpened">@toggleText</a>
        <span>] </span>
    }
    <span class="title">@post.Title</span>
    <span class="name">@post.Username </span>
    @if (post.User.Role != "User")
        {
            <span class="@post.User.Role">##@post.User.Role </span>
        }
    <span> </span>
    <span class="date"> @getTimeFromUnix(post.CreatedAt)</span>
    <span class="post-id">No.@post.GET</span>
    <AuthorizeView Roles="Admin,Mod">
        <Authorized>
            <span> </span>
            <span>(@value)</span>
        </Authorized>
    </AuthorizeView>
    @if (post.IsSticky)
    {
        <img src="img/static/sys/sticky.png" alt="Stickied"/>
    }
    @if (post.IsLocked)
    {
        <img src="img/static/sys/locked.png" alt="Locked"/>
    }
</div>
@if (opened) 
{
    <div class="threadContent">
        <div class="threadImage">
            @if (@post.Image != null)
            {
                string isActiveClass = isActive ? "active" : "";
                <img @onclick="() => isActive = !isActive" class="@isActiveClass" src="@($"{@post.Image.ImageLocation}?size=258x258")" alt="No Image found" />
            }
            else
            {
                <span>[No Image]</span>
            }
        </div>
        <div class="threadTextContainer">
            @foreach (string s in @post.Content.Split("\n"))
            {
                var className = "";
                @if (s.StartsWith(">") && !Regex.IsMatch(s, "^>{2,}"))
                {
                    className = "greenText";
                }
                
                <span class='threadText @className'>@s&nbsp;</span>
            }
        </div>
    </div>
    <div class="threadFooter">
        <div class="sticky">
                
        </div>
        <AuthorizeView Roles="Admin, Mod">
            <Authorized>
                <span>[ </span>
                @if (post.IsSticky)
                {
                    <a onclick="@stickyMe" href="javascript:void(0)">Unsticky</a>
                }
                else
                {
                    <a onclick="@stickyMe" href="javascript:void(0)">Sticky</a>
                }
                <span> ]</span>
                <span>[ </span>
                @if (post.IsLocked)
                {
                    <a onclick="@lockMe" href="javascript:void(0)">Unlock</a>
                }
                else
                {
                    <a onclick="@lockMe" href="javascript:void(0)">Lock</a>
                }
                <span> ]</span>
            </Authorized>
        </AuthorizeView>
        @if (canDel)
        {
            <span>[</span>
            <a @onclick="@deletePost" href="javascript:void(0)">Delete</a>
            <span>]</span>
        }
        <span>[</span>
        <a class="report" href="/sys/report/op/@post.Board/@post.PostID" target="_blank">Report</a>
        <span>] </span>
        @if (showOpenThread)
        {
            <span>[</span>
            <a class="openThread" href="/@post.Board/thread/@post.PostID">(@post.Comments.Count) View Thread</a>
            <span>]</span>
        }
        else
        {
            <span>[</span>
            <span class="openThread">@post.Comments.Count Comments</span>
            <span>]</span>
        }
    </div>
}



@code {

    public bool canDel { get; set; }
    public bool isActive { get; set; } = false;
    
    private async void lockMe()
    {
        post.IsLocked = !post.IsLocked;
        await PostsRepository.updatePostAsync(post);
    }

    private async void stickyMe()
    {
        post.IsSticky = !post.IsSticky;
        await PostsRepository.updatePostAsync(post);
    }
    
    private static DateTime getTimeFromUnix(double javaTimeStamp)
    {
        var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        dateTime = dateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
        return dateTime;
    }

    private async Task deletePost()
    {
        string boardTag = post.Board;
        var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
        var user = await cauthStateProvder.GetAuthenticationStateAsync();
        var usr = user.User;
        UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
        if (foundusr.Role != "User" || post.UserID == foundusr.UserID)
        {
            await TheManager.deleteThread(post);
            navigationManager.NavigateTo($"/{boardTag}");
        }
    }


    private bool opened = true;

    private string toggleText = "-";

    private string value;

    private void ToggleOpened()
    {
        opened = !opened;
        toggleText = opened ? "-" : "+";
    }


    protected override async Task OnParametersSetAsync()
    {
        await base.OnParametersSetAsync();
        value = TheManager.getBumpValue(post) + "v";
        string boardTag = post.Board;
        var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
        var user = await cauthStateProvder.GetAuthenticationStateAsync();
        var usr = user.User;
        UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name);
        if (foundusr != null && (foundusr.Role != "User" || post.UserID == foundusr.UserID))
        {
            canDel = true;
            return;
        }
        canDel = false;
    }

    [Parameter]
    [Required]
    public PostData post { get; set; }
    
    
    [Parameter]
    [Required]
    public bool showOpenThread { get; set; }
}