<template>
  <div id="workouts" v-if="authUser.username">
    <div class="container workouts-container">
      <div class="filters-container" :class="{ hidden: hiddenFilters }">
        <WorkoutsFilters :sports="translatedSports" @filter="updateParams" />
      </div>
      <div class="display-filters">
        <div @click="toggleFilters">
          <i
            :class="`fa fa-caret-${hiddenFilters ? 'down' : 'up'}`"
            aria-hidden="true"
          />
          <span>
            {{ $t(`workouts.${hiddenFilters ? 'DISPLAY' : 'HIDE'}_FILTERS`) }}
          </span>
        </div>
      </div>
      <div class="list-container">
        <WorkoutsList
          :user="authUser"
          :params="params"
          :sports="translatedSports"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts">
  import { ComputedRef, Ref, computed, defineComponent, ref } from 'vue'
  import { useI18n } from 'vue-i18n'

  import WorkoutsFilters from '@/components/Workouts/WorkoutsFilters.vue'
  import WorkoutsList from '@/components/Workouts/WorkoutsList.vue'
  import { USER_STORE, SPORTS_STORE } from '@/store/constants'
  import { ISport, ITranslatedSport } from '@/types/sports'
  import { IAuthUserProfile } from '@/types/user'
  import { useStore } from '@/use/useStore'
  import { translateSports } from '@/utils/sports'

  export default defineComponent({
    name: 'WorkoutsView',
    components: {
      WorkoutsFilters,
      WorkoutsList,
    },
    setup() {
      const { t } = useI18n()
      const store = useStore()
      const authUser: ComputedRef<IAuthUserProfile> = computed(
        () => store.getters[USER_STORE.GETTERS.AUTH_USER_PROFILE]
      )
      const sports: ComputedRef<ISport[]> = computed(
        () => store.getters[SPORTS_STORE.GETTERS.SPORTS]
      )
      const translatedSports: ComputedRef<ITranslatedSport[]> = computed(() =>
        translateSports(sports.value, t)
      )
      const params: Ref<Record<string, string>> = ref({})
      const hiddenFilters = ref(true)

      function updateParams(filters: Record<string, string>) {
        params.value = filters
        hiddenFilters.value = true
      }
      function toggleFilters() {
        hiddenFilters.value = !hiddenFilters.value
      }

      return {
        authUser,
        hiddenFilters,
        params,
        translatedSports,
        toggleFilters,
        updateParams,
      }
    },
  })
</script>

<style lang="scss" scoped>
  @import '~@/scss/base';
  #workouts {
    height: 100%;

    .workouts-container {
      display: flex;
      flex-direction: row;
      @media screen and (max-width: $medium-limit) {
        flex-direction: column;
      }

      .filters-container,
      .list-container {
        display: flex;
        flex-direction: column;
      }

      .filters-container {
        width: 25%;
        @media screen and (max-width: $medium-limit) {
          width: 100%;

          @media screen and (max-width: $small-limit) {
            &.hidden {
              display: none;
            }
          }
        }
      }

      .display-filters {
        display: none;
        font-size: 0.8em;
        padding: 0 $default-padding * 2;

        span {
          cursor: pointer;
          font-weight: bold;
          padding-left: $default-padding * 0.5;
        }
        .fa {
          cursor: pointer;
        }

        @media screen and (max-width: $small-limit) {
          display: flex;
          justify-content: flex-end;
          align-items: center;
        }
      }

      .list-container {
        width: 75%;
        @media screen and (max-width: $medium-limit) {
          width: 100%;
        }
      }
    }
  }
</style>
