<template>
  <div>
    <div v-if="!$fetchState.pending" class="playbutton">
      <template v-if="user && user.name">
        <NuxtLink class="playbutton__playbutton" to="/play">
          Play!
        </NuxtLink>
        <div class="playbutton__logout" @click="logout">
          Logout
        </div>
      </template>
      <form v-else v-on:submit="login">
        <input
          v-model="authCode"
          class="playbutton__authinput"
          type="text"
          size="6"
          maxlength="6"
          placeholder="code"
        />
        <input
          type="submit"
          value="Go"
          :class="[ 'playbutton__authbutton', { disabled: loginDisabled }]"
        />
        <div class="playbutton__errormessage" v-if="errorMessage">
          {{ errorMessage }}
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      authCode: '',
      errorMessage: '',
    }
  },
  async fetch() {
    this.$engine.fetchUserInfo()
  },
  computed: {
    loginDisabled() {
      return this.authCode.length < 6
    },
    user() {
      return this.$store.state.engine.user
    },
  },
  methods: {
    async login(e) {
      e.preventDefault()
      try {
        await this.$axios.$get(`/api/login?code=${this.authCode}`)
        this.$fetch()
      } catch(e) {
        this.errorMessage = 'login failed'
      }
      this.authCode = ''
    },
    async logout() {
      this.authCode = ''
      try {
        await this.$axios.$get('/api/logout')
        window.location.reload()
      } catch(e) {
        this.errorMessage = 'logout failed'
      }
    },
  },
}
</script>

<style lang="scss">
@import '~/assets/css/components';

.playbutton {
  font-family: Dosis;
  font-weight: 800;
  font-size: 36px;
  text-align: center;

  &__playbutton {
    width: 200px;
    height: 48px;
    padding: 4px 36px;
    border: 4px solid $button-border-color;
    border-radius: 8px;
    background-color: $button-background-color;
    color: $button-text-color;

    &:hover {
      background-color: $button-hover-background-color;
      border-color: $button-hover-border-color;
      color: $button-hover-text-color;
    }
  }

  &__logout {
    margin-top: 36px;
    font-size: 24px;
    color: $button-secondary-text-color;
    cursor: pointer;

    &:hover {
      color: $button-secondary-hover-text-color;
    }
  }

  &__authinput {
    display: inline;
    height: 36px;
    border: 4px solid $button-border-color;
    border-radius: 8px;
    font-family: Dosis;
    font-weight: 800;
    font-size: 24px;
  }

  &__authbutton {
    display: inline;
    width: 80px;
    height: 48px;
    background-color: $button-background-color;
    border: 4px solid $button-border-color;
    border-radius: 8px;
    color: $button-text-color;
    font-size: 24px;

    &.disabled,
    &:hover.disabled {
      background-color: $button-disabled-background-color;
      border-color: $button-disabled-border-color;
      color: $button-disabled-text-color;
    }
    &:hover {
      background-color: $button-hover-background-color;
      border-color: $button-hover-border-color;
      color: $button-hover-text-color;
    }
  }

  &__errormessage {
    padding: 8px;
    font-size: 18px;
    color: $error-text-color;
  }
}
</style>
