@using LANCommander.Components
@using LANCommander.Data.Models
@model LANCommander.Models.GameViewModel

@{
    ViewData["Title"] = "Edit";
}

<div class="container-xl">
    <!-- Page title -->
    <div class="page-header d-print-none">
        <div class="row align-items-center">
            <div class="col">
                <h2 class="page-title">
                    Edit Game
                </h2>
            </div>
        </div>
    </div>
</div>

<div class="page-body">
    <div class="container-xl">
        <div class="row row-cards">
            <div class="col-12">
                <form asp-action="Edit" class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12">
                                <div asp-validation-summary="ModelOnly" class="text-danger"></div>

                                <div class="mb-3">
                                    <label asp-for="Game.Title" class="control-label"></label>
                                    <div class="input-group">
                                        <input asp-for="Game.Title" class="form-control" />
                                        <button class="btn" type="submit" asp-action="Lookup">Lookup</button>
                                    </div>
                                    <span asp-validation-for="Game.Title" class="text-danger"></span>
                                </div>
                                <div class="mb-3">
                                    <label asp-for="Game.SortTitle" class="control-label"></label>
                                    <input asp-for="Game.SortTitle" class="form-control" />
                                    <span asp-validation-for="Game.SortTitle" class="text-danger"></span>
                                </div>
                                <div class="mb-3">
                                    <label asp-for="Game.Icon" class="control-label"></label>
                                    <input asp-for="Game.Icon" class="form-control" />
                                    <span asp-validation-for="Game.Icon" class="text-danger"></span>
                                </div>
                                <div class="mb-3">
                                    <label asp-for="Game.Description" class="control-label"></label>
                                    <textarea asp-for="Game.Description" class="form-control" data-bs-toggle="autosize"></textarea>
                                    <span asp-validation-for="Game.Description" class="text-danger"></span>
                                </div>
                                <div class="mb-3">
                                    <label asp-for="Game.ReleasedOn" class="control-label"></label>
                                    <input asp-for="Game.ReleasedOn" class="form-control" />
                                    <span asp-validation-for="Game.ReleasedOn" class="text-danger"></span>
                                </div>
                                <div class="mb-3">
                                    <label class="form-check">
                                        <input asp-for="Game.Singleplayer" type="checkbox" class="form-check-input" />
                                        <span class="form-check-label">Singleplayer</span>
                                        <span class="form-check-description">Game has a singleplayer mode</span>
                                    </label>
                                </div>
                                <div class="mb-3">
                                    <label asp-for="Developers" class="control-label"></label>
                                    <input type="text" class="developer-select" />
                                    <select asp-for="SelectedDevelopers" class="d-none"></select>
                                </div>
                                <div class="mb-3">
                                    <label asp-for="Publishers" class="control-label"></label>
                                    <input type="text" class="publisher-select" />
                                    <select asp-for="SelectedPublishers" class="d-none"></select>
                                </div>

                                <div class="mb-3">
                                    <label asp-for="Genres" class="control-label"></label>
                                    <input type="text" class="genre-select" />
                                    <select asp-for="SelectedGenres" class="d-none"></select>
                                </div>

                                <div class="mb-3">
                                    <label asp-for="Tags" class="control-label"></label>
                                    <input type="text" class="tag-select" />
                                    <select asp-for="SelectedTags" class="d-none"></select>
                                </div>

                                <input type="hidden" asp-for="Game.Id" />
                            </div>
                        </div>
                    </div>

                    <div class="card-header">
                        <h3 class="card-title">Actions</h3>
                    </div>

                    <component type="typeof(ActionEditor)" render-mode="Server" param-Actions="Model.Game.Actions.ToList()" param-GameId="Model.Game.Id" />

                    <div class="card-header">
                        <h3 class="card-title">Multiplayer Modes</h3>
                    </div>

                    <component type="typeof(MultiplayerModeEditor)" render-mode="Server" param-MultiplayerModes="Model.Game.MultiplayerModes" param-GameId="Model.Game.Id" />

                    <div class="card-footer">
                        <div class="d-flex">
                            <a asp-action="Index" class="btn btn-ghost-primary">Cancel</a>
                            <button type="submit" class="btn btn-primary ms-auto">Save</button>
                        </div>
                    </div>
                </form>
            </div>

            <div class="col-12">
                <div class="card">
                    @if (Model.Game.Keys != null && Model.Game.Keys.Count > 0)
                    {
                        <div class="card-header">
                            <h3 class="card-title">Keys</h3>
                            <div class="card-actions">
                                <a asp-action="Details" asp-controller="Keys" asp-route-id="@Model.Game.Id" class="btn btn-ghost-primary">
                                    Details
                                </a>
                            </div>
                        </div>

                        var keysAvailable = Model.Game.Keys.Count(k =>
                        {
                            return (k.AllocationMethod == KeyAllocationMethod.MacAddress && String.IsNullOrWhiteSpace(k.ClaimedByMacAddress)) ||
                                (k.AllocationMethod == KeyAllocationMethod.UserAccount && k.ClaimedByUser == null);
                        });

                        <div class="card-body">
                            <div class="datagrid text-center">
                                <div class="datagrid-item">
                                    <div class="datagrid-title">Available</div>
                                    <div class="datagrid-content">
                                        @keysAvailable
                                    </div>
                                </div>
                                <div class="datagrid-item">
                                    <div class="datagrid-title">Claimed</div>
                                    <div class="datagrid-content">
                                        @(Model.Game.Keys.Count - keysAvailable)
                                    </div>
                                </div>
                                <div class="datagrid-item">
                                    <div class="datagrid-title">Total</div>
                                    <div class="datagrid-content">
                                        @Model.Game.Keys.Count
                                    </div>
                                </div>
                            </div>
                        </div>
                    }
                    else
                    {
                        <div class="empty">
                            <p class="empty-title">No Keys</p>
                            <p class="empty-subtitle text-muted">There have been no keys added for this game.</p>
                            <div class="empty-action">
                                <a asp-action="Edit" asp-controller="Keys" asp-route-id="@Model.Game.Id" class="btn btn-primary">Edit Keys</a>
                            </div>
                        </div>
                    }
                </div>
            </div>

            <div class="col-12">
                <div class="card">
                    @if (Model.Game.Archives != null && Model.Game.Archives.Count > 0)
                    {
                        <div class="card-header">
                            <h3 class="card-title">Archives</h3>
                            <div class="card-actions">
                                <a asp-action="Add" asp-controller="Archives" asp-route-id="@Model.Game.Id" class="btn btn-primary">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>
                                    Add
                                </a>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-vcenter table-mobile-md card-table">
                                <thead>
                                    <tr>
                                        <th>Version</th>
                                        <th>Uploaded By</th>
                                        <th>Uploaded On</th>
                                        <th>Size</th>
                                        <th></th>
                                    </tr>
                                </thead>

                                <tbody>
                                    @foreach (var archive in Model.Game.Archives.OrderByDescending(a => a.CreatedOn))
                                    {
                                        <tr>
                                            <td>@Html.DisplayFor(m => archive.Version)</td>
                                            <td>@Html.DisplayFor(m => archive.CreatedBy.UserName)</td>
                                            <td>@Html.DisplayFor(m => archive.CreatedOn)</td>
                                            <td>@ByteSizeLib.ByteSize.FromBytes(new FileInfo(System.IO.Path.Combine("Upload", archive.ObjectKey)).Length)</td>
                                            <td>
                                                <div class="btn-list flex-nowrap justify-content-end">
                                                    <a asp-action="Download" asp-controller="Archives" asp-route-id="@archive.Id" class="btn">Download</a>
                                                    <a asp-action="Delete" asp-controller="Archives" asp-route-id="@archive.Id" class="btn btn-danger">Delete</a>
                                                </div>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="empty">
                            <p class="empty-title">No Archives</p>
                            <p class="empty-subtitle text-muted">There have been no archives uploaded for this game.</p>
                            <div class="empty-action">
                                <a asp-action="Add" asp-controller="Archives" asp-route-id="@Model.Game.Id" class="btn btn-primary">Upload Archive</a>
                            </div>
                        </div>
                    }
                </div>
            </div>

            <div class="col-12">
                <div class="card">
                    @if (Model.Game.Scripts != null && Model.Game.Scripts.Count > 0)
                    {
                        <div class="card-header">
                            <h3 class="card-title">Scripts</h3>
                            <div class="card-actions">
                                <a asp-action="Add" asp-controller="Scripts" asp-route-id="@Model.Game.Id" class="btn btn-primary">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>
                                    Add
                                </a>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-vcenter table-mobile-md card-table">
                                <thead>
                                    <tr>
                                        <th>Type</th>
                                        <th>Name</th>
                                        <th>Created On</th>
                                        <th>Created By</th>
                                        <th></th>
                                    </tr>
                                </thead>

                                <tbody>
                                    @foreach (var script in Model.Game.Scripts.OrderBy(s => s.Type).ThenByDescending(s => s.CreatedOn))
                                    {
                                        <tr>
                                            <td>@Html.DisplayFor(m => script.Type)</td>
                                            <td>@Html.DisplayFor(m => script.Name)</td>
                                            <td>@Html.DisplayFor(m => script.CreatedOn)</td>
                                            <td>@Html.DisplayFor(m => script.CreatedBy.UserName)</td>
                                            <td>
                                                <div class="btn-list flex-nowrap justify-content-end">
                                                    <a asp-action="Edit" asp-controller="Scripts" asp-route-id="@script.Id" class="btn">Edit</a>
                                                    <a asp-action="Delete" asp-controller="Scripts" asp-route-id="@script.Id" class="btn btn-danger">Delete</a>
                                                </div>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    }
                    else
                    {
                        <div class="empty">
                            <p class="empty-title">No Scripts</p>
                            <p class="empty-subtitle text-muted">There have been no scripts added for this game.</p>
                            <div class="empty-action">
                                <a asp-action="Add" asp-controller="Scripts" asp-route-id="@Model.Game.Id" class="btn btn-primary">Add Script</a>
                            </div>
                        </div>
                    }
                </div>
            </div>

        </div>
    </div>
</div>

@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }

    <script>
        new Select('.developer-select', @Html.Raw(Json.Serialize(Model.Developers)));
        new Select('.publisher-select', @Html.Raw(Json.Serialize(Model.Publishers)));
        new Select('.genre-select', @Html.Raw(Json.Serialize(Model.Genres)));
        new Select('.tag-select', @Html.Raw(Json.Serialize(Model.Tags)));
    </script>
}
