<h1 class="fancy">
  <span><%= @contest.name %></span>
</h1>

<% content_for :sidebar do %>
  <div class="widget contest">
    <h4>Contest Details</h4>

    <dl>
      <dt>Start - End</dt>
      <dd>
        <%= longdate @contest.start %><br>
        <%= longdate @contest.end %>
      </dd>
      <dt>Rules</dt>
      <dd><%= namelink @contest.rules %></dd>
      <dt>Status</dt>
      <dd><%= @contest.statuses[@contest.status] %></dd>
      <dt>Default</dt>
      <dd>Sunday: <%= Time.use_zone(timezone_offset) { @contest.default_time.strftime("%H:%M %Z") } %></dd>
    </dl>

    <% if cuser and cuser.admin? %>
      <%= link_to 'Edit Contest', edit_contest_path(@contest), class: 'button' %>
    <% end %>
  </div>
<% end %>

<div class="standings">
  <% if @contest.contest_type == Contest::TYPE_BRACKET %>
    <%= render partial: 'bracket', locals: { contest: @contest } %>
  <% elsif @contest.contest_type == Contest::TYPE_LADDER %>
    <%= render partial: "contesters/ladder",
        locals: { actions: true, contest: @contest } %>
  <% else %>
    <%= render partial: 'normal' %>
  <% end %>
</div>

<div id="contest" class="tabbed">
  <ul id="contest-nav" class="tabs">
    <li><a href="#results">Matches</a></li>
    <li><a href="#predictions">Predictions</a></li>
    <li><a href="#maps">Maps</a></li>
  </ul>

  <div class="tabbed-contents">
    <div class="tab" id="results">
      <h3>
        Matches Played
      </h3>

      <% if @contest.weeks.count > 0 %>
        <table class="striped weeks">
          <% @contest.weeks.ordered.each do |week| %>
            <% next if week.matches.realfinished.size == 0 %>
            <tr>
              <th class="week"><%= week.name %></th>
              <th class="date">Date</th>
              <th class="score">Score</th>
              <% if @contest.contest_type == Contest::TYPE_LADDER %>
                <th class="points">Points</th>
              <% end %>
            </tr>
            <% week.matches.realfinished.ordered.reverse_each do |match| %>
              <tr>
                <td>
                  <%= namelink match.contester1.team if match.contester1 %>
                  vs
                  <%= namelink match.contester2.team if match.contester1 %>
                </td>
                <td>
                  <%= shorttime match.match_time %>
                </td>
                <td class="score">
                  <%= link_to match, :class => "bold #{match.score_color}" do %>
                    <b><%= h match.score1 %> - <%= h match.score2 %></b>
                  <% end %>

                </td>
                <% if @contest.contest_type == Contest::TYPE_LADDER %>
                  <td class="points">
                    <%= match.points1 %> / <%= match.points2 %>
                  </td>
                <% end %>
              </tr>
            <% end %>
          <% end %>
        </table>
      <% else %>
        <table class="striped teams">
          <tr>
            <th class="team">Teams</th>
            <th class="date">Date</th>
            <th class="score">Score</th>
            <% if @contest.contest_type == Contest::TYPE_LADDER %>
              <th class="points">Points</th>
            <% end %>
          </tr>

          <% @contest.matches.realfinished.ordered.each do |match| %>
            <tr>
              <td>
                <%= namelink match.contester1.team %>
                vs
                <%= namelink match.contester2.team %>
              </td>
              <td>
                <%= shorttime match.match_time %>
              </td>
              <td>
                <%= link_to(match) do %>
                  <b><%= h match.score1 %> - <%= h match.score2 %></b>
                <% end %>

              </td>
              <% if @contest.contest_type == Contest::TYPE_LADDER %>
                <td>
                  <%= match.points1 %> / <%= match.points2 %>
                </td>
              <% end %>
            </tr>
          <% end %>
        </table>
      <% end %>

      <% if @contest.matches.unfinished.ordered.count > 0 %>
        <h3>
          Matches to be played
        </h3>

        <table class="striped matches">
          <tr>
            <th class="team">Teams</th>
            <th class="date">Date</th>
          </tr>

          <% @contest.matches.unfinished.ordered.reverse_each do |match| %>
            <tr>
              <td>
                <%= namelink match.contester1.team if match.contester1 %>
                vs
                <%= namelink match.contester2.team if match.contester2 %>
              </td>
              <td class="date">
                <%= link_to shorttime(match.match_time), match %>
              </td>
            </tr>
          <% end %>
        </table>
      <% end %>
    </div>

    <div class="tab" id="predictions">
      <table class="striped predictions">
        <tr>
          <th class="user">User</th>
          <th class="result">Result</th>
        </tr>
        <% @contest.preds_with_score.each do |prediction| %>
          <tr>
            <td><%= namelink prediction.user %></td>
            <td class="result"><%= prediction.correct %> / <%= prediction.total %> correct (<%= prediction.score.to_f.round(2) %>%)</td>
          </tr>
        <% end %>
      </table>
    </div>

    <div class="tab" id="maps">
      <ul class="disc">
        <% @contest.maps.each do |map| %>
          <li><%= map %></li>
        <% end %>
      </ul>
    </div>
  </div>
</div>

<script type="text/javascript">
  new Yetii({
    id: 'contest'
  });
</script>
