@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model LinkModel

@if (!Model.IsValid)
{
    <div class="image-logo">
        <h1>Document expired!</h1>
    </div>
    <a class="btn btn-light share-button" asp-controller="Home" asp-action="Index">Share message securely!</a>
}
else
{
    <div class="image-logo">
        <h1>Document is ready!</h1>
    </div>
    <a href="/" class="share-more-files-button">Share more files</a>

    @if (Model.Share == true)
    {
        <div class="label-text">
            Link is ready:
        </div>
        <div id="div-link" class="label-text link-show">
            @{
                var link = Url.Action("Link", "Home", new { code = Model.MessageModel.Code }, "https");
            }
            <a id="a-link" href="@link">@link</a>
        </div>
        <a class="btn btn-light share-button" onclick="copyToClipboard()">Copy to clipboard!</a>
        @if (!string.IsNullOrWhiteSpace(Model.MessageModel.Text))
        {
            <div class="label-text">
                Message:
            </div>
            <div class="label-text link-show">
                @Model.MessageModel.Text
            </div>
        }
        @if (Model.MessageModel.FileNames.Count > 0)
        {
            <div class="label-text">
                Files:
            </div>
            <div class="label-text link-show">
                @foreach (var file in Model.MessageModel.FileNames)
                {
                    <a asp-action="Download" asp-route-filename="@file" asp-route-code="@Model.MessageModel.Code">@file</a><br/>
                }
            </div>
        }
    }
    else
    {
        <div>
            @if (Model.TimeLeft != null)
            {
                var time = Model.TimeLeft.Value;
                if (time.Days > 0)
                {
                    <div class="label-text-lower">
                        This message will expire in: @time.ToString("%d") days @time.ToString("%h") hours @time.ToString("%m") minutes
                    </div>
                }
                else
                {
                    <div class="label-text-lower">
                        This message will expire in: @time.ToString(@"hh\:mm\:ss")
                    </div>
                }
            }
            else
            {
                <div class="label-text-lower">
                    This message will expire when you leave this page
                </div>
            }
        </div>

        @if (!string.IsNullOrWhiteSpace(Model.MessageModel.Text))
        {
            <div class="label-text">
                Message:
            </div>
            <div class="label-text link-show">
                @Model.MessageModel.Text
            </div>
        }
        @if (Model.MessageModel.FileNames.Count > 0)
        {
            <div class="label-text">
                Files:
            </div>
            <div class="label-text link-show">
                @foreach (var file in Model.MessageModel.FileNames)
                {
                    <a asp-action="Download" asp-route-filename="@file" asp-route-code="@Model.MessageModel.Code">@file</a><br/>
                }
            </div>
        }

        <a class="btn btn-light share-button" asp-controller="Home" asp-action="Index">Share new message securely!</a>
    }
}

<script>
copyToClipboard = () => {
  // Get the text field
  const copyText = document.getElementById("a-link").innerText;
  
   // Copy the text inside the text field
  navigator.clipboard.writeText(copyText);
}
</script>