<template>
  <div id="app" :style="sideNav.active ? sideNav.style.app : ''">
    <div id="nav">
      <router-link :to="{ name: 'Home' }">Sākums</router-link> |
      <router-link :to="{ name: 'Works' }">Darbi</router-link> |
      <router-link :to="{ name: 'Estimate' }">Tāmes izveide</router-link>
      <router-link v-if="!$store.state.user" to="/auth" class="float-top-right auth-btn sign-in">Pieslēgties</router-link>
      <div class="float-top-right" v-else>
        <router-link :to="{ name: 'Profile' }">{{ $store.state.user.username }}</router-link>
        <div class="auth-btn sign-out" @click="signOut">Atslēgties</div>
      </div>
    </div>
    <div class="mobile-nav">
      <button class="ham-btn" @click="toggleSideNav">&#9776;</button>
      <router-link v-if="$store.state.user" to="/profile">{{ $store.state.user.username }}</router-link>
    </div>
    <div v-show="sideNav.active" class="side-nav">
      <router-link :to="{ name: 'Home' }">Sākums</router-link>
      <router-link :to="{ name: 'Works' }">Darbi</router-link>
      <router-link :to="{ name: 'Estimate' }">Tāmes izveide</router-link>
      <router-link v-if="!$store.state.user" to="/auth" class="auth-btn sign-in">Pieslēgties</router-link>
      <div v-else class="auth-btn sign-out" @click="signOut">Atslēgties</div>
    </div>
    <div v-show="sideNav.active" class="overlay"></div>
    <router-view />
  </div>
</template>

<script>
import Amplify, { Auth, Hub } from 'aws-amplify';
import aws_exports from "./aws-exports";

export default {
  name: "App",
  created() {
    Hub.listen('auth', data => {
      const authenticate = (data, type) => {
        this.$store.commit('setUser', data);
        aws_exports.aws_appsync_authenticationType = type;
        Amplify.configure(aws_exports);
      }
      if (data.payload.event === 'signIn') {
        Auth.currentUserInfo()
          .then(data => {
            authenticate(data, 'AMAZON_COGNITO_USER_POOLS');
            this.$router.push({ name: 'Home' });
          })
          .catch(() => this.$store.commit('setUser', null));
      } else if (data.payload.event === 'signOut') {
        authenticate(null, 'API_KEY');
        if (this.$route.path !== '/auth') this.$router.push({ name: 'Auth' });
      }
    });
  },
  data() {
    return {
      sideNav: {
        active: false,
        style: {
          app: {
            height: '100vh',
            width: '100vw',
            overflow: 'hidden'
          }
        }
      }
    }
  },
  watch:{
    $route() {
      this.sideNav.active = false;
    }
  }, 
  methods: {
    signOut() {
      Auth.signOut();
    },
    toggleSideNav() {
      this.sideNav.active = !this.sideNav.active;
    }
  }
};
</script>

<style>
@import url('css/global.css');

#app {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;
  position: relative;
}

#nav a {
  font-weight: bold;
  color: #2c3e50;
  text-decoration: none;
}

#nav a.router-link-exact-active {
  color: #42b983;
}

.float-top-right {
  position: absolute;
  top: 50%;
  right: 30px;
  transform: translate(0, -50%);
  -webkit-transform: translate(0,-50%);
  -ms-transform: translate(0,-50%);
}

.float-top-right a {
  margin-right: 10px;
}

.auth-btn {
  padding: 9px;
  border-radius: 4px;
  font-size: 15px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}

.sign-in {
  background: rgb(84 185 66 / 0.2);
  border: 1px solid #b1d4ad;
  color: #399a57 !important;
}

.sign-out {
  background: rgb(185 66 66 / 0.2);
  border: 1px solid #d4adb0;
  color: #9a3939;
}

.side-nav {
  z-index: 99999999;
  display: flex;
  position: fixed;
  background: #fff;
  flex-direction: column;
  width: 80%;
  height: calc(100% - 60px);
  overflow-x: hidden;
  overflow-y: overlay;
}

.side-nav a {
  padding: 20px;
  color: #2c3e50;
  text-decoration: none;
  font-weight: bold;
  position: relative;
}

.side-nav a:nth-child(n+2)::before {
  content: '';
  width: 70%;
  height: 1px;
  display: block;
  background: #d0d0d0;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%);
}

.side-nav .auth-btn {
  margin: 0 20px;
  padding: 20px;
}

.mobile-nav {
  display: none;
  padding: 10px;
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 999999;
  box-shadow: 0 0 20px rgb(44 62 80 / 50%);
}

.mobile-nav a {
  line-height: 40px;
  margin: 0 10px;
  color: #2c3e50;
  font-weight: bold;
  text-decoration: none;
}

.ham-btn {
  width: 40px;
  height: 40px;
  background: #2c3e50;
  color: #fff;
  border: none;
  border-radius: 5px;
  font-weight: bold;
  font-size: 17px;
}

@media screen and (min-width: 701px) {
  .side-nav, .overlay {
    display: none;
  }
}

@media screen and (max-width: 700px) {
  #nav {
    display: none;
  }

  .mobile-nav {
    display: flex;
  }
}

@media screen and (max-width: 400px) {
  .side-nav {
    width: 90%;
  }
}

@media screen and (max-width: 300px) {
  .side-nav {
    width: 100%;
  }
}
</style>