@model SceneEditViewModel

<div class="scene-inspector-editor-form">
    <div class="scene-inspector-editor-fields">
        <section class="scene-structure-editor-section">
            <div class="scene-structure-editor-heading">
                <span>Current thread events</span>
                <strong>@Model.ThreadEvents.Count</strong>
            </div>

            @if (!Model.ThreadEvents.Any())
            {
                <p class="muted">No plot thread events are attached to this scene yet.</p>
            }
            else
            {
                <div class="scene-structure-editor-list">
                    @foreach (var threadEvent in Model.ThreadEvents)
                    {
                        <article class="scene-structure-editor-card">
                            <div class="scene-structure-card-header" style="--plot-colour:@threadEvent.Colour">
                                <div>
                                    <h4>@threadEvent.EventTitle</h4>
                                    <p>@threadEvent.PlotEventTypeName / @threadEvent.PlotLineName / @threadEvent.ThreadTitle</p>
                                </div>
                                <span class="scene-structure-marker">@threadEvent.MarkerText</span>
                            </div>

                            <form asp-controller="Scenes" asp-action="UpdateThreadEvent" method="post" class="scene-structure-editor-form" data-scene-editor-form>
                                @Html.AntiForgeryToken()
                                <input type="hidden" name="ThreadEventID" value="@threadEvent.ThreadEventID" />
                                <input type="hidden" name="SceneID" value="@Model.SceneID" />
                                <input type="hidden" name="PlotThreadID" value="@threadEvent.PlotThreadID" />
                                <input type="hidden" name="ReturnProjectID" value="@Model.ReturnProjectID" />
                                <input type="hidden" name="ReturnBookID" value="@Model.ReturnBookID" />

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

                                <div class="scene-inspector-editor-grid">
                                    <div class="form-group">
                                        <label for="ThreadEvent_@(threadEvent.ThreadEventID)__PlotEventTypeID">Event type</label>
                                        <select class="form-control form-control-sm" id="ThreadEvent_@(threadEvent.ThreadEventID)__PlotEventTypeID" name="PlotEventTypeID">
                                            <option value="">Choose event type</option>
                                            @foreach (var option in Model.PlotEventTypeOptions)
                                            {
                                                <option value="@option.Value" selected="@(option.Value == threadEvent.PlotEventTypeID.ToString())">@option.Text</option>
                                            }
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label for="ThreadEvent_@(threadEvent.ThreadEventID)__EventTypeID">Marker type</label>
                                        <select class="form-control form-control-sm" id="ThreadEvent_@(threadEvent.ThreadEventID)__EventTypeID" name="EventTypeID">
                                            @foreach (var option in Model.ThreadEventTypeOptions)
                                            {
                                                <option value="@option.Value" selected="@(option.Value == threadEvent.EventTypeID.ToString())">@option.Text</option>
                                            }
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="ThreadEvent_@(threadEvent.ThreadEventID)__TargetPlotLineID">Target plot line</label>
                                    <select class="form-control form-control-sm" id="ThreadEvent_@(threadEvent.ThreadEventID)__TargetPlotLineID" name="TargetPlotLineID">
                                        <option value="">No target plot line</option>
                                        @foreach (var plotLine in Model.PlotLineEventOptions)
                                        {
                                            <option value="@plotLine.PlotLineID" selected="@(threadEvent.TargetPlotLineID == plotLine.PlotLineID)">@plotLine.PlotLineName</option>
                                        }
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="ThreadEvent_@(threadEvent.ThreadEventID)__SelectedTargetPlotLineIDs">Split target plot lines</label>
                                    <select class="form-control form-control-sm" id="ThreadEvent_@(threadEvent.ThreadEventID)__SelectedTargetPlotLineIDs" name="SelectedTargetPlotLineIDs" multiple size="4">
                                        @foreach (var plotLine in Model.PlotLineEventOptions)
                                        {
                                            <option value="@plotLine.PlotLineID" selected="@threadEvent.TargetPlotLineIDs.Contains(plotLine.PlotLineID)">@plotLine.PlotLineName</option>
                                        }
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="ThreadEvent_@(threadEvent.ThreadEventID)__EventTitle">Event title</label>
                                    <input class="form-control form-control-sm" id="ThreadEvent_@(threadEvent.ThreadEventID)__EventTitle" name="EventTitle" value="@threadEvent.EventTitle" />
                                </div>

                                <div class="form-group">
                                    <label for="ThreadEvent_@(threadEvent.ThreadEventID)__EventDescription">Description</label>
                                    <textarea class="form-control form-control-sm" id="ThreadEvent_@(threadEvent.ThreadEventID)__EventDescription" name="EventDescription" rows="3">@threadEvent.EventDescription</textarea>
                                </div>

                                <div class="scene-structure-action-row">
                                    <button class="btn btn-sm btn-primary" type="submit" data-scene-editor-save>Save event</button>
                                </div>
                            </form>

                            <form asp-controller="Scenes" asp-action="DeleteThreadEvent" method="post" class="scene-structure-delete-form" data-scene-editor-form>
                                @Html.AntiForgeryToken()
                                <input type="hidden" name="id" value="@threadEvent.ThreadEventID" />
                                <input type="hidden" name="sceneId" value="@Model.SceneID" />
                                <input type="hidden" name="projectId" value="@Model.ReturnProjectID" />
                                <input type="hidden" name="bookId" value="@Model.ReturnBookID" />
                                <div class="scene-inspector-editor-error" data-scene-editor-error hidden></div>
                                <button class="btn btn-sm btn-outline-danger" type="submit" data-scene-editor-save>Remove event</button>
                            </form>
                        </article>
                    }
                </div>
            }
        </section>

        <section class="scene-structure-editor-section">
            <div class="scene-structure-editor-heading">
                <span>Add thread event</span>
            </div>

            <form asp-controller="Scenes" asp-action="AddThreadEvent" method="post" class="scene-structure-editor-card scene-structure-editor-form" data-scene-editor-form>
                @Html.AntiForgeryToken()
                <input type="hidden" name="SceneID" value="@Model.SceneID" />
                <input type="hidden" name="ReturnProjectID" value="@Model.ReturnProjectID" />
                <input type="hidden" name="ReturnBookID" value="@Model.ReturnBookID" />

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

                <div class="form-group">
                    <label for="PlotThreadID">Existing plot thread</label>
                    <select class="form-control form-control-sm" id="PlotThreadID" name="PlotThreadID">
                        <option value="">Choose a plot thread</option>
                        @foreach (var thread in Model.PlotThreadEventOptions)
                        {
                            <option value="@thread.PlotThreadID">@thread.PlotLineName: @thread.ThreadTitle</option>
                        }
                    </select>
                </div>

                <div class="scene-inspector-editor-grid">
                    <div class="form-group">
                        <label for="PlotEventTypeID">Event type</label>
                        <select class="form-control form-control-sm" id="PlotEventTypeID" name="PlotEventTypeID">
                            <option value="">Choose event type</option>
                            @foreach (var option in Model.PlotEventTypeOptions)
                            {
                                <option value="@option.Value" selected="@(option.Value == Model.NewThreadEvent.PlotEventTypeID.ToString())">@option.Text</option>
                            }
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="EventTypeID">Marker type</label>
                        <select class="form-control form-control-sm" id="EventTypeID" name="EventTypeID" asp-items="Model.ThreadEventTypeOptions"></select>
                    </div>
                </div>

                <div class="form-group">
                    <label for="TargetPlotLineID">Target plot line</label>
                    <select class="form-control form-control-sm" id="TargetPlotLineID" name="TargetPlotLineID">
                        <option value="">No target plot line</option>
                        @foreach (var plotLine in Model.PlotLineEventOptions)
                        {
                            <option value="@plotLine.PlotLineID">@plotLine.PlotLineName</option>
                        }
                    </select>
                </div>

                <div class="form-group">
                    <label for="SelectedTargetPlotLineIDs">Split target plot lines</label>
                    <select class="form-control form-control-sm" id="SelectedTargetPlotLineIDs" name="SelectedTargetPlotLineIDs" multiple size="4">
                        @foreach (var plotLine in Model.PlotLineEventOptions)
                        {
                            <option value="@plotLine.PlotLineID">@plotLine.PlotLineName</option>
                        }
                    </select>
                </div>

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

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

                <div class="scene-structure-action-row">
                    <button class="btn btn-sm btn-primary" type="submit" data-scene-editor-save>Add thread event</button>
                </div>
            </form>
        </section>
    </div>

    <div class="scene-inspector-editor-footer">
        <button type="button" class="btn btn-sm btn-outline-secondary" data-scene-editor-close>Close</button>
    </div>
</div>
