<%- include('../../functions') -%>

<%
  var
    card_url = "http://gatherer.wizards.com/Pages/Search/Default.aspx?name=+[%22CARD-NAME%22]",
    detail_url = "http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=",
    image_url = "/images/magic/commanders/",
    deck = page.deck || {},
    info = deck.info || {},
    cards = deck.cards || [],
    lands = deck.lands || [],
    changes = deck.changes || [],
    starting_lands = info.starting_lands || [],

    commander = info.commander && info.commander_id ?
      "<li><a href='" + detail_url + info.commander_id + "' target='_blank'>" + info.commander +
        "</a> <em>(Commander)</em></li>" :
      "",
    commander_img = info.commander && info.commander_img ?
      "<img src='" + image_url + info.commander_img + "' class='magic-commander-img' alt='" + info.commander + " card' />":
      ""
    ;
-%>

<h2>
  <a href="/<%= page.path %>" name="<%= snakeCase(page.title) %>" id="<%= snakeCase(page.title) %>">
    <%= page.title %>
  </a>
</h2>

<p>
  Below is my <strong><%= info.name %></strong> deck for <a href="https://magic.wizards.com/">Magic: the Gathering</a>
  (<%= info.format %> format).
  It was last updated on <%= info.date_upd %>.
</p>

<p>
  Each card listed below is linked to its entry on <a href="http://gatherer.wizards.com/">The Gatherer</a>
  (the official <abbr title="Magic: the Gatheirng">MtG</abbr> card database) and should open in a new window or tab in
  your browser.
</p>

<h3> Decklist </h3>

<%- commander_img %>

<ul>
  <%- commander %>
  <% cards.forEach(function(card) { %>
    <li><a href="<%- card_url.replace('CARD-NAME', card) %>" target="_blank"><%= card %></a></li>
  <% }) %>
  <% lands.forEach(function(land) { %>
    <li><a href="<%- card_url.replace('CARD-NAME', land.type) %>" target="_blank"><%= land.type %></a> (x<%=land.count%>)</li>
  <% }) %>
</ul>

<% if (starting_lands && starting_lands.length > 0) { %>
  <h4>Starting Lands</h4>
  <p>
    In order to speed our games along, my gaming group allows everyone to start with 3 basic lands.
    The lands listed below are included in the counts above.</p>
  <ul>
    <% starting_lands.forEach(function(land) { %>
      <% if (typeof land === 'string') { %>
        <li><a href="<%- card_url.replace('CARD-NAME', land) %>" target="_blank"><%= land %></a></li>
      <% } else {%>
        <li>
          <a href="<%- card_url.replace('CARD-NAME', land.type) %>" target="_blank"><%= land.type %></a> (x<%=land.count%>)
        </li>
      <% } %>
    <% }) %>
  </ul>
<% } %>

<% if (changes && changes.length > 0) { %>
  <h4>Changes from Previous Versions</h4>

  <ul>
    <% changes.forEach(function(change) { %>
      <li>
        Implemented <%= change.date_upd %>:
        <ul>
          <% if (change.adds) { %>
            <% change.adds.forEach(function(add) { %>
              <li><a href="<%- card_url.replace('CARD-NAME', add) %>" target="_blank"><%= add %></a> (added)</li>
            <% }) %>
          <% } %>
          <% if (change.dels) { %>
            <% change.dels.forEach(function(del) { %>
              <li><a href="<%- card_url.replace('CARD-NAME', del) %>" target="_blank"><%= del %></a> (removed)</li>
            <% }) %>
          <% } %>
        </ul>
      </li>
    <% }) %>
  </ul>
<% } %>

<%- content %>
