@page
@using System.Text.Json
@using ExtensiblePortfolioSite.SDK.Git
@model ProjectsModel
@{
    ViewData["Title"] = "Projects";
    Layout = "_Layout";
}

<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<div class="container repo-container">
        @{
            //var githubService = GitManager.GetService("github.com");
            //var user = githubService.GetUser(new Uri("https://github.com/KoromaruKoruko"));
            //Console.WriteLine("FOUND USER");
            //Console.WriteLine(user.Name);
            //var repos = user.GetUserRepositories();
            //Console.WriteLine("REPOSITORIES");
            //foreach(var repo in repos)
            //    Console.WriteLine(repo.Name);
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("User-Agent", "KoromaruKoruko");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer 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");
            HttpResponseMessage response = await client.GetAsync("https://api.github.com/users/KoromaruKoruko/repos");
            JsonDocument json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());

            foreach(JsonElement elem in json.RootElement.EnumerateArray())
            {
                int popout_id = Random.Shared.Next();
                HttpResponseMessage commitResponse = await client.GetAsync(elem.GetProperty("commits_url").GetString().Split('{')[0]);
                JsonDocument commitInfo = await JsonDocument.ParseAsync(await commitResponse.Content.ReadAsStreamAsync());
                JsonElement commitJson = commitInfo.RootElement.EnumerateArray().First();
                String[] lastCommitMessage = commitJson.GetProperty("commit").GetProperty("message")!.GetString()!.Split("\n").Where(s => s != "").ToArray();

                // Begin repo   
                                <div class="repo">
                                <div class="repo-title">
                                    <span class="eps-lineclamp-1">KoromaruKoruko/</span>
                                    <span class="eps-lineclamp-1">@elem.GetProperty("name")</span>
                                </div>
                                <div class="repo-description eps-lineclamp-2">
                                    @elem.GetProperty("description")
                                </div>
                                <div class="popout">
                                    <span class="popout-title">@elem.GetProperty("description")</span>
                                </div>
                                <div class="commit">
                                    <img class="commit-author" src="@commitJson.GetProperty("author").GetProperty("avatar_url").GetString()"/>
                                    <div class="commit-info">
                                    <div class="commit-title eps-lineclamp-1">
                                    @if(lastCommitMessage.Length > 0)
                                {
                                        @lastCommitMessage[0]
                                }
                                </div>
                                    <div class="commit-description eps-lineclamp-1">
                                        @if(lastCommitMessage.Length > 1)
                                {
                                     @lastCommitMessage[1]   
                                }
                                    </div>
                                    </div>
                                    <div class="popout popout-commit">
                                @if (lastCommitMessage.Length > 0)
                            {
                                    <span class="popout-title">@lastCommitMessage[0]</span>
                                    <br />
                                var commitDescription = lastCommitMessage.Skip(1).ToHashSet();
                                    <div class="popout-content">
                                        @if(commitDescription.Count > 0)
                                        @commitDescription.Aggregate((a, b) => a + "\n" + b)
                                    </div>
                            }
                            </div>
                                </div>
                                <div class="repo-footer">
                                    <a class="repo-link" href="@elem.GetProperty("html_url").GetString()">
                                    <span>visit repo >></span>
                                    </a>
                                    <img class="repo-icon" src="/img/logos/github/Light-64px.png"/>
                                </div>
                                </div>
                // End repo
                }
                    <script>
                        function processMarkdown(element) {
                            element.getElementsByClassName('popout-content')[0].innerHTML = marked.parse(element.getElementsByClassName('popout-content')[0].innerHTML);
                        }
                        Array.from(document.getElementsByClassName('popout-commit')).forEach(processMarkdown);
                    </script>
        }
  </div>