@model Alpha.Coordinador.WebApp.Models.VirtualMachineListViewModel

<tr id="tr_@Model.Machine.ID">
    <td>
        @Html.DisplayFor(modelItem => Model.Machine.Nombre)
    </td>
    @if (ViewBag.IsAdmin)
    {
        <td>
            @Html.DisplayFor(modelItem => Model.Machine.IP)
        </td>
    }
    <td>
        @Html.DisplayFor(modelItem => Model.Machine.Usuario)
    </td>

    <td>
        @if (Model.CurrentSession == null || (Model.CurrentSession != null && Model.CurrentSession.TimeLeftSeconds <= 0))
        {
            var SessionDurationBoxName = $"sessionDuration_{Model.Machine.ID}";
            <a id="btnIngresar_@Model.Machine.ID" style="margin-top:1px;" class="btn btn-primary" onclick="goToVirtual(@Model.Machine.ID, @Model.RelatedUser.Id, '@SessionDurationBoxName')">Ingresar</a>
            <a id="btnCopyLink_@Model.Machine.ID" style="margin-top:1px;" class="btn btn-info" onclick="getVirtualLink(@Model.Machine.ID, @Model.RelatedUser.Id, '@SessionDurationBoxName', '@Model.Machine.Nombre')">Compartir</a>
            //Don't replace the timer if we're reloading.
            @Html.EditorFor(model => Model.SessionDuration, new { htmlAttributes = new { @id = SessionDurationBoxName, @class = "form-control", @Value = Model.MaxSessionDuration, @style = "width:170px; margin-top: 5px; height:34px; display: block;", } })
    <script>
                var existingInterval = window.UpdateSessionStateIntervals.find(x => x.machineId === @Model.Machine.ID);
                if (typeof existingInterval !== 'undefined') {
                    window.clearInterval(existingInterval.interval);
                }
                window.UpdateSessionStateIntervals = window.UpdateSessionStateIntervals.filter(x => x.machineId !== @Model.Machine.ID);
    </script>
        }
        else if (Model.CurrentSession.Active && Model.CurrentSession.TimeLeftSeconds > 0)
        {
            var TimeLeftSecs = Model.CurrentSession.TimeLeftSeconds;
            <p id="secondsLeft_@Model.Machine.ID" value="@TimeLeftSecs" style="display: none;"></p>
            <code id="countdown_@Model.Machine.ID" style="margin-top: 5px; color: #3432a8; background-color: #f9f2f4; font-weight: bold; padding:5px 4px">Sesión en curso. Tiempo restante:  @TimeSpan.FromSeconds(--TimeLeftSecs).ToString(@"hh\:mm\:ss")</code>
            <a id="btnIngresar_@Model.Machine.ID" style="margin-top:1px;" class="btn btn-primary" onclick="goToVirtualNewWindow(String.raw`@Model.CurrentSession.Url`)">Ingresar</a>
            <a id="btnCopyLink_@Model.Machine.ID" style="margin-top:1px;" class="btn btn-info" onclick="copyToClipboard(String.raw`@Model.CurrentSession.Url`)">Copiar link</a>
            <a id="btnResetSession_@Model.Machine.ID" style="margin-top:1px;" class="btn btn-warning" onclick="resetSession('@Model.CurrentSession.Token', @Model.RelatedUser.Id, @Model.Machine.ID)">Resetear</a>
            <a id="btnEndSession_@Model.Machine.ID" style="margin-top:1px;" class="btn btn-danger" onclick="endSession('@Model.CurrentSession.Token', @Model.RelatedUser.Id, @Model.Machine.ID)">Terminar</a>
            if (!Model.IsReload)
            {
                <script>
                    timerCountDown('@Model.CurrentSession.TimeLeftSeconds', 'countdown_@Model.Machine.ID');
                    //window.UpdateSessionStateIntervals = window.UpdateSessionStateIntervals.filter(x => x.machineId !== @Model.Machine.ID);
                </script>
            }
        <script>
            var existingInterval = window.UpdateSessionStateIntervals.find(x => x.machineId === @Model.Machine.ID);
            if (typeof existingInterval !== 'undefined') {
                window.clearInterval(existingInterval.interval);
            }
            window.UpdateSessionStateIntervals = window.UpdateSessionStateIntervals.filter(x => x.machineId !== @Model.Machine.ID);
            window.UpdateSessionStateIntervals.push({ machineId: @Model.Machine.ID, interval: setInterval(() => { updateSessionState('@Model.CurrentSession.Token', @Model.Machine.ID, @Model.RelatedUser.Id, true); }, 10000) });
        </script>
        }
        <script>
            setTimeout(() => {
                console.log('Check if ' + @Model.Machine.ID + ' is in UpdateSessionStateIntervals.');
                if (!window.UpdateSessionStateIntervals.some(x => x.machineId === @Model.Machine.ID)) {
                    console.log('Nope. UpdateSessionStateIntervals: ' + window.UpdateSessionStateIntervals);
                    window.UpdateSessionStateIntervals.push({ machineId: @Model.Machine.ID, interval: setInterval(() => { updateSessionState('', @Model.Machine.ID, @Model.RelatedUser.Id, false); }, 10000) });
                }
            }, 1000);
        </script>
    </td>
    <td>
        <div class="loader" style="opacity:0" id="Loader_@Model.Machine.ID"></div>
        <div><p style="text-align:left"><strong id="States_@Model.Machine.ID"></strong></p></div>
    </td>

</tr>
