@using RazorLight
@inherits TemplatePage<Elwig.Documents.WineQualityStatistics>
@model Elwig.Documents.WineQualityStatistics
@{ Layout = "Document"; }
<link rel="stylesheet" href="file:///@Raw(Model.DataPath)\resources\WineQualityStatistics.css"/>
<main>
    <h1>Qualitätsstatistik</h1>
    <h2>@Model.Filter</h2>
    @foreach (var sec in Model.Data.Sections) {
        <table>
            <colgroup>
                <col style="width: 25%;"/>
                <col style="width: 25%;"/>
                <col style="width: 25%;"/>
                <col style="width: 25%;"/>
            </colgroup>
            <thead>
                <tr>
                    <th colspan="4" class="header @(sec.Type == "R" ? "red" : sec.Type == "W" ? "green" : "")">
                        <h3>@sec.Name</h3>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    @foreach (var qualIds in Model.QualIds) {
                        <td class="container">
                            <div class="row">
                                <span class="units">[°Oe]</span>
                                <span class="units">[#]</span>
                                <span class="units">[kg]</span>
                            </div>
                            @foreach (var qualId in qualIds) {
                                <h4>@(Model.QualityLevels.GetValueOrDefault(qualId, qualId))</h4>
                                @foreach (var (oe, num, weight) in sec.Data.GetValueOrDefault(qualId, Array.Empty<(int, int, int)>())) {
                                    <div class="row">
                                        <span class="oe">@oe</span>
                                        <span class="number">@($"{num:N0}")</span>
                                        <span class="number">@($"{weight:N0}")</span>
                                    </div>
                                }
                            }
                        </td>
                    }
                </tr>
                <tr>
                    @foreach (var qualIds in Model.QualIds) {
                        var quals = qualIds.Select(q => sec.Data.GetValueOrDefault(q, Array.Empty<(int Oe, int Num, int Weight)>()));
                        var weight = quals.Sum(q => q.Sum(kv => kv.Weight));
                        var num = quals.Sum(q => q.Sum(kv => kv.Num));
                        var oe = quals.Sum(q => q.Sum(kv => (double)kv.Oe * kv.Weight)) / weight;
                        <td class="container bold">
                            <div class="row">
                                <span class="oe">@(weight == 0 ? "-" : $"{oe:N0}")</span>
                                <span class="number">@($"{num:N0}")</span>
                                <span class="number">@($"{weight:N0}")</span>
                            </div>
                        </td>
                    }
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    @{
                        var totalWeight = sec.Data.Values.Sum(q => q.Sum(kv => kv.Weight));
                        var totalNum = sec.Data.Values.Sum(q => q.Sum(kv => kv.Num));
                        var totalOe = sec.Data.Values.Sum(q => q.Sum(kv => (double)kv.Oe * kv.Weight)) / totalWeight;
                    }
                    <td colspan="4" class="container bold footer @(sec.Type == "R" ? "red" : sec.Type == "W" ? "green" : "")">
                        <div class="row" style="width: 24%; margin-left: 76%;">
                            <span class="oe">@(totalWeight == 0 ? "-" : $"{totalOe:N0}")</span>
                            <span class="number">@($"{totalNum:N0}")</span>
                            <span class="number">@($"{totalWeight:N0}")</span>
                        </div>
                    </td>
                </tr>
            </tfoot>
        </table>
    }
</main>
