<template>
  <nav class="navbar navbar-expand-md navbar-dark fixed-top">
    <div class="container">
      <button aria-controls="mainNav" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler"
              data-bs-target="#mainNav" data-bs-toggle="collapse" type="button">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div id="mainNav" class="collapse navbar-collapse navbar-nav justify-content-between">
        <ul class="list-unstyled">
          <li class="nav-item text-warning fw-bold" data-bs-target="#mainNav.show" data-bs-toggle="collapse"
              @click="GoToLink('/')">
            CSGO<span class="text-up text-white fw-bold">WTF</span>
          </li>
          <li class="nav-item" data-bs-target="#mainNav.show" data-bs-toggle="collapse" @click="GoToLink('/explore')">
            Explore
          </li>
        </ul>
        <form class="d-flex" data-bs-target="#mainNav.show" @keydown.enter.prevent="parseSearch">
          <label for="search">
            <svg class="bi bi-search" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
              <path
                  d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
            </svg>
          </label>

          <input id="search" v-model="data.searchInput" aria-label="Search"
                 class="form-control bg-transparent border-0"
                 placeholder="SteamID64, Profile Link or Custom URL"
                 type="search">
          <button
              class="btn border-2 btn-outline-info"
              type="button"
              @click="parseSearch"
          >
            Search!
          </button>
        </form>
      </div>

    </div>
  </nav>
</template>

<script>
import {reactive} from "vue";
import {useStore} from 'vuex'
import {sanitize} from 'string-sanitizer'
import router from "../router";
import {GoToLink} from '../utils'

export default {
  name: 'Nav',
  setup() {
    const store = useStore()
    const data = reactive({
      searchInput: ''
    })

    const parseSearch = () => {
      const input = data.searchInput
      const customUrlPattern = 'https://steamcommunity.com/id/'
      const profileUrlPattern = 'https://steamcommunity.com/profiles/'
      const id64Pattern = /^\d{17}$/

      store.state.id64 = ''
      store.state.vanityUrl = ''

      if (id64Pattern.test(input)) {
        store.state.id64 = input
      } else if (input.match(customUrlPattern)) {
        store.state.vanityUrl = sanitize(input.split('/')[4].split('?')[0])
      } else if (input.match(profileUrlPattern)) {
        const tmp = input.split('/')[4].split('?')[0]
        if (id64Pattern.test(tmp)) {
          store.state.id64 = tmp
        }
      } else {
        store.state.vanityUrl = sanitize(input)
      }

      if (store.state.id64 !== '' || store.state.vanityUrl !== '') {
        data.searchInput = ''

        if (store.state.id64) {
          router.push(`/player/${store.state.id64}`)
        } else if (store.state.vanityUrl) {
          router.push(`/player/${store.state.vanityUrl}`)
        }
      }

      document.activeElement.blur()
    }

    return {
      data, parseSearch, GoToLink
    }
  }
}
</script>

<style lang="scss" scoped>
nav {
  max-width: 100vw;
  height: 70px;
  width: 100vw;
  background: rgba(16, 18, 26, 1);
  z-index: 2;

  ul {
    display: flex;

    li {
      font-size: 1.5rem;
      font-weight: lighter;
      margin: 22px 10px 0 10px;
      cursor: pointer;
      transition: 100ms ease-in-out;

      &:first-child {
        font-size: 2rem;
        font-weight: bold;
        margin-top: 17px;
        margin-left: 0;
      }

      &:hover {
        color: var(--bs-info);
        transition: 100ms ease-in-out;
      }
    }
  }

  .text-up {
    font-size: 40%;
    vertical-align: top;
  }

  svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
  }

  label {
    padding-top: 6px;
  }

  input[type="search"] {
    min-width: 300px;
    max-width: 300px;

    &:focus {
      box-shadow: 0 4px 2px -2px rgba(95, 120, 146, 0.59);
      transition: .2s ease-in-out;
      transform: scale(.975);
    }

    &::placeholder {
      color: #aaa;
    }
  }
}

@media screen and (max-width: 768px) {
  nav {
    button {
      outline: 1px solid var(--bs-primary);

      &:focus {
        box-shadow: none;
        outline: 1px solid var(--bs-primary);
      }
    }

    .navbar-collapse {
      background: var(--bs-secondary);
      border-radius: 5px;
      border: 1px solid var(--bs-primary)
    }

    #mainNav {

      ul {
        display: flex;
        flex-direction: column;
        text-align: center;
        width: 100%;

        li {
          line-height: 1;
          padding: 0 0 20px 0;
          border-bottom: 1px solid rgba(255, 255, 255, .1);
        }
      }

      form {
        label {
          display: none;
        }

        input[type="search"] {
          margin-bottom: 15px;
          width: 100%;
        }

        button {
          display: block;
          margin-top: -15px;
        }
      }
    }
  }
}
</style>