@page
@model MyPage.IndexModel
@using Microsoft.AspNetCore.Http
@using JtkMainServer.Managers
@{
    ViewData["Title"] = "마이페이지";
    var loggedInUserId = HttpContext.Session.GetString("LoggedInUserId");
    ViewData["LoggedIn"] = loggedInUserId != null;
    ViewData["ActivatedTabIndex"] = 0;

    var userPingLogs = DbManager.GetUserPingLogs(loggedInUserId);
    var totalUsedTime = TimeSpan.FromSeconds((from tuple in userPingLogs select tuple.usedTime.TotalSeconds).Sum());
    var totalPaid = (from tuple in userPingLogs select tuple.totalCashUse).Sum();

    var userTotalCash = DbManager.GetUser(loggedInUserId)?.Cash;

    var mostFrequentPlaceNames = (from tuple in userPingLogs group tuple.loginTime by tuple.placeName)  // placeName별로 묶인 loginTime
        .OrderByDescending<IGrouping<string, DateTime>, int>(group => group.Count()) // 그룹의 요소 개수로 내림차순 정렬  <컬렉션요소타입, 정렬기준타입>
        .Take(5)  // 상위 5개 추출
        .Select(group => group.Key)  // 그룹의 키만 추출
        .ToArray();  // 배열화
}

<style type="text/css">
    .table-stats {
        width: 100%;
    }

    .table-stats th {
        font-weight: normal;
        font-size: large;
        padding: 8px 4px 8px 4px;
    }

    .table-stats td {
        text-align: right;
    }

    div .review {
        box-shadow: 2px 2px rgba(0, 0, 0, 0);
        font-weight: bold;
        border: solid 1.5px #ccc;
        border-radius: 5px;
        padding: 0 5px;
        background: #fff;
        margin-bottom: 7px;
    }

    .review .id {
        margin-left: 5px;
        font-weight: bold;
    }

    .review .date {
        float: right;
        margin-right: 10px;
        font-weight: lighter;
    }

    .review p {
        font-weight: lighter;
    }
</style>
<div class="row" style="margin-top:20px">
    <div class="col-md-6">
        @Html.Partial("~/Views/MyPageTabs.cshtml")
        <h3>현재 잔액</h3>
        <h2 style="text-align:right">￦@(userTotalCash.Value.ToString("N0"))</h2>
        <hr />
        <h3>통계</h3>
        <table class="table-stats">
            <tr>
                <th>총 이용 시간</th>
                <td>@($"{Math.Floor(totalUsedTime.TotalHours):N0}시간 {totalUsedTime.Minutes}분")</td>
            </tr>
            <tr>
                <th>총 사용 금액</th>
                <td>￦@totalPaid.ToString("N0")</td>
            </tr>
            <tr>
                <th>자주 가는 PC방</th>
                <td>@(mostFrequentPlaceNames.Length > 0 ? mostFrequentPlaceNames[0] : "없음")</td>
            </tr>
        </table>
        <hr />
        <h3>이용 내역</h3>
        <table class="table">
            <thead>
                <tr>
                    <th>일시</th>
                    <th>PC방</th>
                    <th>이용시간</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var tuple in userPingLogs) {
                    <tr>
                        <td>@tuple.loginTime.ToString("yyyy-MM-dd HH:mm")</td>
                        <td><a href="/PCBang/Details?id=@tuple.placeId">@tuple.placeName</a></td>
                        <td>@($"{Math.Floor(tuple.usedTime.TotalHours)}시간 {tuple.usedTime.Minutes}분 (￦{tuple.totalCashUse:N0})")</td>
                    </tr>
                }

            </tbody>
        </table>
        @if (userPingLogs?.Length == 0) {
            <center>이용 내역이 없습니다.</center>
        }
        <hr />
        <h3>작성한 평가</h3>
        @{ var reviews = DbManager.GetUserReviews(loggedInUserId); }
        @if (reviews.Length > 0) {
            foreach (var review in reviews) {
                var glyphClass = review.score > 0 ? "glyphicon-thumbs-up" : "glyphicon-thumbs-down"; ;
                var glyphColor = review.score > 0 ? "darkseagreen" : "indianred";
        <div class="review">
            <h5>@Html.Raw($"<span class=\"glyphicon {glyphClass}\" style=\"color: {glyphColor}\"></span>") <span class="id">@review.nickname (@review.placeName)</span><span class="date">@review.time</span></h5>
            <p>@review.text</p>
        </div>
            }
        } else {
            <center>작성한 평가가 없습니다.</center>
        }
        <hr />
    </div>
</div>
<div>
    <a asp-page="/Index">홈으로 이동</a>
</div>