@using Newtonsoft.Json
@using StardewModdingAPI.Web.Framework.LogParsing.Models
@model StardewModdingAPI.Web.ViewModels.LogParserModel

@{
    ViewData["Title"] = "SMAPI log parser";
    IDictionary<string, LogModInfo[]> contentPacks = Model.GetContentPacksByMod();
}

@section Head {
    @if (Model.PasteID != null)
    {
        <meta name="robots" content="noindex" />
    }
    <link rel="stylesheet" href="~/Content/css/log-parser.css?r=20180611" />
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" crossorigin="anonymous"></script>
    <script src="~/Content/js/log-parser.js?r=20180611"></script>
    <script>
        $(function() {
            smapi.logParser({
                logStarted: new Date(@Json.Serialize(Model.ParsedLog?.Timestamp)),
                showPopup: @Json.Serialize(Model.ParsedLog == null),
                showMods: @Json.Serialize(Model.ParsedLog?.Mods?.Select(p => Model.GetSlug(p.Name)).Distinct().ToDictionary(slug => slug, slug => true), new JsonSerializerSettings { Formatting = Formatting.None }),
                showLevels: {
                    @LogLevel.Trace.ToString().ToLower(): false,
                    @LogLevel.Debug.ToString().ToLower(): true,
                    @LogLevel.Info.ToString().ToLower(): true,
                    @LogLevel.Alert.ToString().ToLower(): true,
                    @LogLevel.Warn.ToString().ToLower(): true,
                    @LogLevel.Error.ToString().ToLower(): true
                }
            }, '@Model.SectionUrl');
        });
    </script>
}

@* upload result banner *@
@if (Model.UploadError != null)
{
    <div class="banner error" v-pre>
        <strong>Oops, the server ran into trouble saving that file.</strong><br />
        <small v-pre>Error details: @Model.UploadError</small>
    </div>
}
else if (Model.ParseError != null)
{
    <div class="banner error" v-pre>
        <strong>Oops, couldn't parse that log. (Make sure you upload the log file, not the console text.)</strong><br />
        Share this URL when asking for help: <code>@(new Uri(new Uri(Model.SectionUrl), Model.PasteID))</code><br />
        (Or <a href="@Model.SectionUrl">upload a new log</a>.)<br />
        <br />
        <small v-pre>Error details: @Model.ParseError</small>
    </div>
}
else if (Model.ParsedLog?.IsValid == true)
{
    <div class="banner success" v-pre>
        <strong>Share this link to let someone else see the log:</strong> <code>@(new Uri(new Uri(Model.SectionUrl), Model.PasteID))</code><br />
        (Or <a href="@Model.SectionUrl">upload a new log</a>.)
    </div>
}

@* upload new log *@
@if (Model.ParsedLog == null)
{
    <h2>Where do I find my SMAPI log?</h2>
    <div>What system do you use?</div>
    <ul id="os-list">
        <li><input type="radio" name="os" value="linux" id="os-linux" /> <label for="os-linux">Linux</label></li>
        <li><input type="radio" name="os" value="mac" id="os-mac" /> <label for="os-mac">Mac</label></li>
        <li><input type="radio" name="os" value="windows" id="os-windows" /> <label for="os-windows">Windows</label></li>
    </ul>
    <div data-os="linux">
        On Linux:
        <ol>
            <li>Open the Files app.</li>
            <li>Click the options menu (might be labeled <em>Go</em> or <code>⋮</code>).</li>
            <li>Choose <em>Enter Location</em>.</li>
            <li>Enter this exact text: <pre>~/.config/StardewValley/ErrorLogs</pre></li>
            <li>The log file is <code>SMAPI-latest.txt</code>.</li>
        </ol>
    </div>
    <div data-os="mac">
        On Mac:
        <ol>
            <li>Open the Finder app.</li>
            <li>Click <em>Go</em> at the top, then <em>Enter Location</em>.</li>
            <li>Enter this exact text: <pre>~/.config/StardewValley/ErrorLogs</pre></li>
            <li>The log file is <code>SMAPI-latest.txt</code>.</li>
        </ol>
    </div>
    <div data-os="windows">
        On Windows:
        <ol>
            <li>Press the <code>Windows</code> and <code>R</code> buttons at the same time.</li>
            <li>In the 'run' box that appears, enter this exact text: <pre>%appdata%\StardewValley\ErrorLogs</pre></li>
            <li>The log file is <code>SMAPI-latest.txt</code>.</li>
        </ol>
    </div>

    <h2>How do I share my log?</h2>
    <form action="@Model.SectionUrl" method="post">
        <ol>
            <li>
                Drag the file onto this textbox (or paste the text in):<br />
                <textarea id="input" name="input" placeholder="paste log here"></textarea>
            </li>
            <li>
                Click this button:<br />
                <input type="submit" id="submit" value="save log" />
            </li>
            <li>On the new page, copy the URL and send it to the person helping you.</li>
        </ol>
    </form>
}

@* parsed log *@
@if (Model.ParsedLog?.IsValid == true)
{
    <h2>Log info</h2>
    <div id="output">
        <table id="metadata">
            <caption>Game info:</caption>
            <tr>
                <th>Stardew Valley:</th>
                <td v-pre>@Model.ParsedLog.GameVersion on @Model.ParsedLog.OperatingSystem</td>
            </tr>
            <tr>
                <th>SMAPI:</th>
                <td v-pre>@Model.ParsedLog.ApiVersion</td>
            </tr>
            <tr>
                <th>Folder:</th>
                <td v-pre>@Model.ParsedLog.GamePath</td>
            </tr>
            <tr>
                <th>Log started:</th>
                <td>@Model.ParsedLog.Timestamp.UtcDateTime.ToString("yyyy-MM-dd HH:mm") UTC ({{localTimeStarted}} your time)</td>
            </tr>
        </table>
        <br />
        <table id="mods">
            <caption>
                Installed mods:
                <span class="notice txt"><i>click any mod to filter</i></span>
                <span class="notice btn txt" v-on:click="showAllMods" v-show="stats.modsHidden > 0">show all</span>
                <span class="notice btn txt" v-on:click="hideAllMods" v-show="stats.modsShown > 0 && stats.modsHidden > 0">hide all</span>
            </caption>
            @foreach (var mod in Model.ParsedLog.Mods.Where(p => p.ContentPackFor == null))
            {
                <tr v-on:click="toggleMod('@Model.GetSlug(mod.Name)')" class="mod-entry" v-bind:class="{ hidden: !showMods['@Model.GetSlug(mod.Name)'] }">
                    <td><input type="checkbox" v-bind:checked="showMods['@Model.GetSlug(mod.Name)']" v-show="anyModsHidden" /></td>
                    <td v-pre>
                        <strong>@mod.Name</strong> @mod.Version
                        @if (contentPacks != null && contentPacks.TryGetValue(mod.Name, out LogModInfo[] contentPackList))
                        {
                            <div class="content-packs">
                                @foreach (var contentPack in contentPackList)
                                {
                                    <text>+ @contentPack.Name @contentPack.Version</text><br />
                                }
                            </div>
                        }
                    </td>
                    <td v-pre>@mod.Author</td>
                    @if (mod.Errors == 0)
                    {
                        <td v-pre class="color-green">no errors</td>
                    }
                    else if (mod.Errors == 1)
                    {
                        <td v-pre class="color-red">@mod.Errors error</td>
                    }
                    else
                    {
                        <td v-pre class="color-red">@mod.Errors errors</td>
                    }
                </tr>
            }
        </table>
        <div id="filters">
            Filter messages:
            <span v-bind:class="{ active: showLevels['trace'] }" v-on:click="toggleLevel('trace')">TRACE</span> |
            <span v-bind:class="{ active: showLevels['debug'] }" v-on:click="toggleLevel('debug')">DEBUG</span> |
            <span v-bind:class="{ active: showLevels['info'] }" v-on:click="toggleLevel('info')">INFO</span> |
            <span v-bind:class="{ active: showLevels['alert'] }" v-on:click="toggleLevel('alert')">ALERT</span> |
            <span v-bind:class="{ active: showLevels['warn'] }" v-on:click="toggleLevel('warn')">WARN</span> |
            <span v-bind:class="{ active: showLevels['error'] }" v-on:click="toggleLevel('error')">ERROR</span>
        </div>

        <table id="log">
            @foreach (var message in Model.ParsedLog.Messages)
            {
                string levelStr = message.Level.ToString().ToLower();

                <tr class="@levelStr mod" v-show="filtersAllow('@Model.GetSlug(message.Mod)', '@levelStr')">
                    <td v-pre>@message.Time</td>
                    <td v-pre>@message.Level.ToString().ToUpper()</td>
                    <td v-pre data-title="@message.Mod">@message.Mod</td>
                    <td v-pre>@message.Text</td>
                </tr>
                if (message.Repeated > 0)
                {
                    <tr class="@levelStr mod mod-repeat" v-show="filtersAllow('@Model.GetSlug(message.Mod)', '@levelStr')">
                        <td colspan="3"></td>
                        <td v-pre><i>repeats [@message.Repeated] times.</i></td>
                    </tr>
                }
            }
        </table>
    </div>
}
else if (Model.ParsedLog?.IsValid == false)
{
    <h3>Raw log</h3>
    <pre v-pre>@Model.ParsedLog.RawText</pre>
}