@page "/lottohistory"

@using BlazorLottoPicker.Data.Model;
@using BlazorLottoPicker.Data.Services;
@inject HistoryService _historyService;

<h3>LottoHistory</h3>

<p>Please set start and end lottery</p>

<p>
    <button class="btn btn-primary" @onclick="OpenPopup">
        Search
    </button>
</p>

@if (_showPopup)
{
    <div class="modal" style="display:block" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="modal-title">Add/Update Game Result</h3>
                    <button type="button" class="btn btn-close" @onclick="ClosePopup" />
                </div>
                <div class="modal-body">
                    <label for="Start">Start</label>
                    <input class="form-control" type="text" placeholder="Start" @bind-value="_start" />
                    <label for="End">End</label>
                    <input class="form-control" type="text" placeholder="End" @bind-value="_end" />
                    <button class="btn btn-primary" @onclick="SearchHistory">
                        Search
                    </button>
                </div>
            </div>
        </div>
    </div>
}

@if (_loading)
{
    <p><em>Loading...</em></p>
}
else if (_stats == null)
{
    
}
else
{
    <p>Searched count: @_stats.Count</p>
    <table class="table">
        <thead>
            <tr>
                <th>Ball number</th>
                <th>Count</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var stat in _stats)
            {
                <tr>
                    <td>@stat.BallNumber</td>
                    <td>@stat.Count</td>
                </tr>
            }
        </tbody>
    </table>
}


@code {
    List<HistoryResult> _histories;
    List<HistoryStatistics> _stats;

    int _start = 0;
    int _end = 20;

    bool _loading = false;
    bool _showPopup;

    void OpenPopup()
    {
        _showPopup = true;
    }

    void ClosePopup()
    {
        _showPopup = false;
    }

    async void SearchHistory()
    {
        ClosePopup();
        _loading = true;
        _histories = await _historyService.GetHistoryListAsync(_start, _end);

        if (_histories == null || _histories.Count < 1)
        {
            _loading = false;
            return;
        }

        List<int> hist = new List<int>(6 * (_end - _start + 1));
        foreach (HistoryResult history in _histories)
        {
            hist.AddRange(GetBallValues(history));
        }

        hist.Sort();
        Dictionary<string, int> historyCounts = new Dictionary<string, int>();
        for (int i = 0; i < hist.Count; i++)
        {
            string key = $"No.{hist[i].ToString()}";
            if (!historyCounts.ContainsKey(key))
                historyCounts.Add(key, 0);

            historyCounts[key]++;
        }

        _stats = new List<HistoryStatistics>();
        foreach (var histCount in historyCounts)
        {
            _stats.Add(new HistoryStatistics() { BallNumber = histCount.Key, Count = histCount.Value });
        }

        _loading = false;

        StateHasChanged();
    }

    List<int> GetBallValues(HistoryResult result)
    {
        List<int> values = new List<int>();
        values.Add(result.drwtNo1);
        values.Add(result.drwtNo2);
        values.Add(result.drwtNo3);
        values.Add(result.drwtNo4);
        values.Add(result.drwtNo5);
        values.Add(result.drwtNo6);

        return values;
    }
}
