<!DOCTYPE html>
  <head>
    <link rel="stylesheet" href="styles.css">
    <script src='shuffle.js'></script>
    <script src='vote.js'></script>
    {{if .Admin}}
      <script src='admin.js'></script>
    {{end}}
  </head>
  <body>
    {{template "header" .}}
    {{if .Admin}}
      <section class='admin'>
        <details class='admin__details'>
          <summary>Admin</summary>
          <label>
            <span title='ClientID for the OAuth app'>ClientID</span>
            <input name='ClientID' value={{.Config.ClientID}}>
          </label>
          <label>
            <span title='OAuth redirect/callback URL from the OAuth app'>OAuth Redirect</span>
            <input name='OAuthRedirect' value={{.Config.OAuthRedirect}}>
          </label>
          <hr>
          <label>
            <span title='Game jam, derived from jam URL. Refresh page after changing'>Game Jam</span>
            <input name='GameJam' value={{.Config.GameJam}}>
          </label>
          <hr>
          <label>
            <span title='Reload after adding or removing categories to refresh entries.'>Voting Categories</span>
          </label>
          {{range $index, $category := .Config.VoteCategories}}
            <span class='voteCategory'>
              <input class='VoteCategories' name='VoteCategories-{{$index}}' value={{.}}>
              <button class='RemoveVoteCategory'>Remove</button>
            </span>
          {{end}}
          <span>
            <input id='NewVoteCategory' name='NewVoteCategory'>
            <button id='AddVoteCategory'>Add Category</button>
          </span>
          <hr>
          {{range $index, $category := .Config.Badges}}
            <span class='badge'>
              <input class='Badges' name='Badges-{{$index}}' value={{.}}>
              <button class='RemoveBadge'>Remove Badge</button>
            </span>
          {{end}}
          <span>
            <input id='NewBadge' name='NewBadge'>
            <button id='AddBadge'>Add Badge</button>
          </span>
          <hr>
          {{range $index, $category := .Config.Tags}}
            <span class='tag'>
              <input class='Tags' name='Tags-{{$index}}' value={{.}}>
              <button class='RemoveTag'>Remove Tag</button>
            </span>
          {{end}}
          <span>
            <input id='NewTag' name='NewTag'>
            <button id='AddTag'>Add Tag</button>
          </span>
          <hr>
          <label>
            <span title='Enable/disable user voting. Refresh page after changing.'>Voting Enabled</span>
            <input name='VotingEnabled' type='checkbox'{{if .Config.VotingEnabled}} checked{{end}}>
          </label>
          <label>
            <span title='Prevent further voting and show average results. Refresh page after changing.'>Voting Finished</span>
            <input name='VotingFinished' type='checkbox'{{if .Config.VotingFinished}} checked{{end}}>
          </label>
        </details>
      </section>
    {{else}}
      <section></section>
    {{end}}
    <section class='entries'>
      {{$finalBadges := getFinalBadges}}
      {{$finalTags := getFinalTags}}
      {{range .Entries.Games}}
        <article class='entry' data-id={{.ID}}>
          {{template "entry" .}}
          {{if (and (not $.VotingFinished) (not $.Authed))}}
            Login to vote!
          {{else if or $.VotingFinished $.VotingEnabled}}
            {{$votes := ""}}
            {{$badges := ""}}
            {{$tags := ""}}
            {{if $.VotingFinished}}
              {{$votes = getFinalVotes .ID}}
              {{$badges = getActualFinalBadges $finalBadges .ID}}
              {{$tags = getActualFinalTags $finalTags .ID}}
            {{else}}
              {{$votes = getVotes $.User.Details .ID}}
              {{$badges = getBadges $.User.Details .ID}}
              {{$tags = getTags $.User.Details .ID}}
            {{end}}
            {{$ID := .ID}}
            <section class='entry__accolades'>
              {{if $.VotingFinished}}
                <img class='entry__badge' src='badge/{{.ID}}'>
                <img class='entry__tags' src='tags/{{.ID}}'>
              {{end}}
            </section>
            {{if or (and $.VotingEnabled (not (isOwnGame $.User.Details .ID))) $.VotingFinished}}
              <section class='badges'>
                {{if $.VotingFinished}}
                {{else}}
                  {{range $.Config.Badges}}
                    {{$badge := .}}
                    {{$value := false}}
                    {{if index $badges $badge}}
                      {{$value = index $badges $badge}}
                    {{end}}
                    <span class='badges__entry {{if $value}}-selected{{else}}{{end}}' data-badge='{{$badge}}'>
                      <a href='badge?id={{$ID}}&badge={{$badge}}' class='badges__entry__name'>
                        {{$badge}}
                      </a>
                    </span>
                  {{end}}
                {{end}}
              </section>
              <section class='tags'>
                {{if $.VotingFinished}}
                {{else}}
                  {{range $.Config.Tags}}
                    {{$tag := .}}
                    {{$value := false}}
                    {{if index $tags $tag}}
                      {{$value = index $tags $tag}}
                    {{end}}
                    <span class='tags__entry {{if $value}}-selected{{else}}{{end}}' data-tag='{{$tag}}'>
                      <a href='tag?id={{$ID}}&tag={{$tag}}' class='tags__entry__name'>
                        {{$tag}}
                      </a>
                    </span>
                  {{end}}
                {{end}}
              </section>
              <section class='ratings'>
                {{if $.VotingFinished}}
                  {{$key := "TOTAL"}}
                  {{$value := 0.0}}
                  {{if index $votes $key}}
                    {{$value = index $votes $key}}
                  {{end}}
                  <article class='ratings__entry'>
                    <header>{{$key}}</header>
                    <section class='ratings__entry__stars' data-category='{{$key}}'>
                      {{range $v := iterate 5}}
                        <a href='vote?id={{$ID}}&{{$key}}={{$v}}'>
                          {{if starSet $value $v}} ★ {{else}} ☆ {{end}}
                        </a>
                      {{end}}
                    </section>
                    <span class='ratings__entry__number' data-category='{{$key}}'>{{if $.VotingFinished}}{{niceFloat $value}}{{else}}{{$value}}{{end}}</span>
                  </article>
                {{end}}
                {{range $.Config.VoteCategories}}
                  {{$key := .}}
                  {{$value := 0.0}}
                  {{if index $votes $key}}
                    {{$value = index $votes $key}}
                  {{end}}
                  <article class='ratings__entry'>
                    <header>{{$key}}</header>
                    <section class='ratings__entry__stars' data-category='{{$key}}'>
                      {{range $v := iterate 5}}
                        <a href='vote?id={{$ID}}&{{$key}}={{$v}}'>
                          {{if starSet $value $v}} ★ {{else}} ☆ {{end}}
                        </a>
                      {{end}}
                    </section>
                    <span class='ratings__entry__number' data-category='{{$key}}'>{{if $.VotingFinished}}{{niceFloat $value}}{{else}}{{$value}}{{end}}</span>
                  </article>
                {{end}}
              </section>
            {{end}}
          {{else}}
          {{end}}
        </article>
      {{end}}
    </section>
  </body>
</html>