<template>
  <div id="app" @keyup="keyup" tabindex="-1">
    <TownInfo :players="players" :edition="edition"></TownInfo>
    <TownSquare
      :is-public="isPublic"
      :players="players"
      :roles="roles"
    ></TownSquare>
    <Modal v-show="showEditionModal" @close="showEditionModal = false">
      <h2>Select an edition:</h2>
      <ul class="editions">
        <li
          v-for="edition in editions"
          class="edition"
          v-bind:class="['edition-' + edition.id]"
          v-bind:key="edition.id"
          @click="setEdition(edition.id)"
        >
          {{ edition.name }}
        </li>
      </ul>
    </Modal>
    <div class="controls">
      <font-awesome-icon icon="cogs" @click="isControlOpen = !isControlOpen" />
      <ul v-if="isControlOpen">
        <li @click="togglePublic">Toggle <em>G</em>rimoire</li>
        <li @click="addPlayer" v-if="players.length < 20">
          <em>A</em>dd Player
        </li>
        <li @click="randomizeSeatings" v-if="players.length > 2">
          <em>R</em>andomize Seatings
        </li>
        <li @click="clearPlayers" v-if="players.length">
          Clear Players
        </li>
        <li @click="showEditionModal = true" v-if="players.length > 4">
          Select Edition
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import TownSquare from "./components/TownSquare";
import TownInfo from "./components/TownInfo";
import Modal from "./components/Modal";
import rolesJSON from "./roles";
import editions from "./editions";

export default {
  components: {
    TownSquare,
    TownInfo,
    Modal
  },
  data: function() {
    return {
      editions,
      isPublic: true,
      isControlOpen: false,
      players: [],
      roles: this.getRolesByEdition(),
      edition: "tb",
      showEditionModal: false
    };
  },
  methods: {
    togglePublic() {
      this.isPublic = !this.isPublic;
      this.isControlOpen = false;
    },
    addPlayer() {
      const name = prompt("Player name");
      if (name) {
        this.players.push({
          name,
          role: {},
          reminders: []
        });
      }
    },
    randomizeSeatings() {
      if (confirm("Are you sure you want to randomize seatings?")) {
        this.players = this.players
          .map(a => [Math.random(), a])
          .sort((a, b) => a[0] - b[0])
          .map(a => a[1]);
      }
    },
    clearPlayers() {
      this.players = [];
    },
    keyup({ key }) {
      switch (key) {
        case "g":
          this.togglePublic();
          break;
        case "a":
          this.addPlayer();
          break;
        case "r":
          this.randomizeSeatings();
          break;
      }
    },
    getRolesByEdition(edition = "tb") {
      const selectedEdition = editions.find(({ id }) => id === edition);
      return new Map(
        rolesJSON
          .filter(
            r => r.edition === edition || selectedEdition.roles.includes(r.id)
          )
          .sort((a, b) => b.team.localeCompare(a.team))
          .map(role => [role.id, role])
      );
    },
    setEdition(edition) {
      this.edition = edition;
      this.showEditionModal = false;
    }
  },
  mounted() {
    if (localStorage.isPublic !== undefined) {
      this.isPublic = JSON.parse(localStorage.isPublic);
    }
    if (localStorage.edition) {
      this.edition = localStorage.edition;
      this.roles = this.getRolesByEdition(this.edition);
    }
    if (localStorage.players) {
      this.players = JSON.parse(localStorage.players).map(player => ({
        ...player,
        role: this.roles.get(player.role) || {}
      }));
    }
  },
  watch: {
    players: {
      handler(newPlayers) {
        localStorage.players = JSON.stringify(
          newPlayers.map(player => ({
            ...player,
            role: player.role.id || {}
          }))
        );
      },
      deep: true
    },
    edition(newEdition) {
      localStorage.edition = newEdition;
      this.roles = this.getRolesByEdition(newEdition);
    },
    isPublic(newIsPublic) {
      localStorage.isPublic = JSON.stringify(newIsPublic);
    }
  }
};
</script>

<style lang="scss">
@import "vars";

@font-face {
  font-family: "Papyrus";
  src: url("assets/fonts/papyrus.eot"); /* IE9*/
  src: url("assets/fonts/papyrus.eot?#iefix") format("embedded-opentype"),
    /* IE6-IE8 */ url("assets/fonts/papyrus.woff2") format("woff2"),
    /* chrome firefox */ url("assets/fonts/papyrus.woff") format("woff"),
    /* chrome firefox */ url("assets/fonts/papyrus.ttf") format("truetype"),
    /* chrome firefox opera Safari, Android, iOS 4.2+*/
      url("assets/fonts/papyrus.svg#PapyrusW01") format("svg"); /* iOS 4.1- */
}

html,
body {
  font-size: 1.2em;
  line-height: 1.4;
  background: url("assets/background.jpg") center center;
  background-size: cover;
  color: white;
  height: 100%;
  font-family: "Roboto Condensed", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding: 0;
  margin: 0;
  overflow: hidden;
}

* {
  box-sizing: border-box;
  position: relative;
}

#app {
  height: 100%;
}

// Controls
.controls {
  position: absolute;
  right: 0;
  top: 0;
  text-align: right;
  padding: 10px;
  svg {
    cursor: pointer;
  }
  ul {
    display: flex;
    list-style-type: none;
    padding: 0;
    margin: 0;
    flex-direction: column;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 10px black;
    li {
      padding: 5px 10px;
      color: white;
      text-align: center;
      background: rgba(0, 0, 0, 0.7);
      margin-bottom: 1px;
      cursor: pointer;
      &:hover {
        background-color: red;
      }
      em {
        text-decoration: underline;
        font-style: normal;
        font-weight: bold;
      }
    }
  }
}

// Editions
@each $img in $editions {
  .edition-#{$img} {
    background-image: url("./assets/edition-#{$img}.png");
  }
}

ul.editions .edition {
  text-align: center;
  padding-top: 100px;
  background-position: center center;
  background-size: cover;
  width: 200px;
  margin: 5px;
  font-size: 120%;
  font-weight: bold;
  text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000,
    1px 1px 0 #000, 0 0 5px rgba(0, 0, 0, 0.75);
  cursor: pointer;
}
</style>
