<template>
  <div id="app">
    <h2 id="header">Salut {{ name }} !</h2>
    <Sockets v-on:message="newMessage($event)" />
    <div id="players">
      <p>{{ players.length }} en ligne :</p>
      <div
        v-for="player in players" v-bind:key="player + ''">
        <Player :name="player"/>
      </div>
    </div>
    <div id="game">
      <label for="messages"> Choose a message:</label>
      <select v-model="message" name="message" id="messages">
        <option value="WAITING">J'attends</option>
        <option value="READY">Prêt à jouer !</option>
        <option value="BET">Pari plus haut</option>
        <option value="MENTEUR">Menteur !</option>
      </select>
      <button v-on:click="sendMessage(message)">Envoyer</button>
      <Hand :cards="currentCards()" />
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import "es6-promise/auto"; // Needed for Promise polyfill
import VueSocketIO from "vue-socket.io";
import Vuex, { mapGetters, mapMutations } from "vuex";
import Hand from "./components/Hand";
import Player from "./components/Player";
import Sockets from "./Sockets";
import { store } from "./vuex-store";
import { BootstrapVue, IconsPlugin } from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

Vue.use(Vuex);
Vue.use(BootstrapVue);
Vue.use(IconsPlugin);

Vue.use(
  new VueSocketIO({
    debug: true,
    connection: "http://localhost:9042",
    vuex: {
      store,
      actionPrefix: "SOCKET_",
      mutationPrefix: "SOCKET_"
    },
    options: { path: "/socket.io/" } //Optional options
  }), store
);

export default {
  name: "App",
  store: store,
  components: {
    Player,
    Sockets,
    Hand
  },
  data: function() {
    return {
      message: "WAITING"
    };
  },
  computed: {
    ...mapGetters(["isConnected", "name", "text",
      "cards", "players", "socketMessage"])
  },
  methods: {
    sendMessage: function(message) {
      console.log("User wants to send", message);
      this.$socket.emit("message", message);
    },
    newMessage: function(data) {
      console.log("App received new state:", data.data);
      this.handleState(data);
    },
    handleState: function(data) {
      console.log("hS: ", data);
      if (data.message === "NewGame") {
        console.log("New Game!");
      }
    },
    currentCards() {
      let currentState = this.socketMessage;
      console.log("curCards! Last message:", JSON.stringify(currentState));
      if (currentState["data"]) {
        if (currentState["data"]["cards"]) {
          console.log("Setting cards...");
          return this.cards;

        } else return this.mockCards();
      } else return this.mockCards();

      // console.log("Current socket.sMs now:", Sockets.sockets.socketMessages);
    },
    mockCards() {
      return [
        // { value: "ace", color: "hearts" },
        // { value: "ace", color: "clubs" },
        // { value: "ace", color: "diamonds" },
        { value: "ace", color: "spades" }
      ];
    },
    ...mapMutations(["setName"])
    // ...mapActions(['setNumberToRemoteValue']),
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

#game {
  margin-top: 20px;
}

.card-deck {
  justify-content: center;
}
</style>
