@model SceneEditViewModel
@{
    var currentAssetIds = Model.AssetEvents.Select(x => x.StoryAssetID).ToHashSet();
    var availableAssets = Model.AssetOptions
        .Where(x => int.TryParse(x.Value, out var id) && !currentAssetIds.Contains(id))
        .ToList();
}

<form asp-controller="Scenes" asp-action="SaveSceneAssets" method="post" class="scene-inspector-editor-form" data-scene-editor-form>
    @Html.AntiForgeryToken()
    <input type="hidden" asp-for="SceneID" />

    <div class="scene-inspector-editor-error" data-scene-editor-error hidden></div>

    <div class="scene-inspector-editor-fields">
        <section class="scene-world-editor-section">
            <div class="scene-world-editor-heading">
                <span>Current assets</span>
                <strong>@Model.AssetEvents.Select(x => x.StoryAssetID).Distinct().Count()</strong>
            </div>

            @if (!Model.AssetEvents.Any())
            {
                <p class="muted">No story assets appear in this scene yet.</p>
            }
            else
            {
                <div class="scene-world-editor-list">
                    @for (var index = 0; index < Model.AssetEvents.Count; index++)
                    {
                        var assetEvent = Model.AssetEvents[index];
                        <article class="scene-world-editor-card">
                            <input type="hidden" name="AssetEvents[@index].AssetEventID" value="@assetEvent.AssetEventID" />
                            <input type="hidden" name="AssetEvents[@index].StoryAssetID" value="@assetEvent.StoryAssetID" />
                            <div class="scene-world-editor-card-header">
                                <div>
                                    <h4>@assetEvent.AssetName</h4>
                                    <p>@assetEvent.EventTitle / @assetEvent.AssetEventTypeName</p>
                                    @if (!string.IsNullOrWhiteSpace(assetEvent.ToStateName))
                                    {
                                        <p>State: @(assetEvent.FromStateName ?? "Any") to @assetEvent.ToStateName</p>
                                    }
                                    @if (!string.IsNullOrWhiteSpace(assetEvent.EventDescription))
                                    {
                                        <p>@assetEvent.EventDescription</p>
                                    }
                                </div>
                                <label class="scene-world-remove">
                                    <input type="checkbox" name="AssetEvents[@index].Remove" value="true" />
                                    <input type="hidden" name="AssetEvents[@index].Remove" value="false" />
                                    <span>Remove</span>
                                </label>
                            </div>
                        </article>
                    }
                </div>
            }
        </section>

        <section class="scene-world-editor-section">
            <div class="scene-world-editor-heading">
                <span>Add asset</span>
            </div>

            <div class="form-group">
                <label for="NewAssetEvent_StoryAssetID">Asset</label>
                <select class="form-control form-control-sm" id="NewAssetEvent_StoryAssetID" name="NewAssetEvent.StoryAssetID">
                    <option value="">Choose a project asset</option>
                    @foreach (var option in availableAssets)
                    {
                        <option value="@option.Value">@option.Text</option>
                    }
                </select>
            </div>

            <div class="scene-inspector-editor-grid">
                <div class="form-group">
                    <label for="NewAssetEvent_AssetEventTypeID">Event type</label>
                    <select class="form-control form-control-sm" id="NewAssetEvent_AssetEventTypeID" name="NewAssetEvent.AssetEventTypeID" asp-items="Model.AssetEventTypeOptions"></select>
                </div>
                <div class="form-group">
                    <label for="NewAssetEvent_ToStateID">To state</label>
                    <select class="form-control form-control-sm" id="NewAssetEvent_ToStateID" name="NewAssetEvent.ToStateID" asp-items="Model.AssetStateOptions">
                        <option value="">No change</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label for="NewAssetEvent_EventTitle">Event title</label>
                <input class="form-control form-control-sm" id="NewAssetEvent_EventTitle" name="NewAssetEvent.EventTitle" placeholder="Defaults to event type if blank" />
            </div>

            <div class="form-group">
                <label for="NewAssetEvent_EventDescription">Description</label>
                <textarea class="form-control form-control-sm" id="NewAssetEvent_EventDescription" name="NewAssetEvent.EventDescription" rows="3"></textarea>
            </div>
        </section>
    </div>

    <div class="scene-inspector-editor-footer">
        <button type="button" class="btn btn-sm btn-outline-secondary" data-scene-editor-close>Cancel</button>
        <button type="submit" class="btn btn-sm btn-primary" data-scene-editor-save>Save assets</button>
    </div>
</form>
