<template>
  <div v-show="false" class="soundscape-wrapper">
    <!-- Main Audio Processing Components -->
    <div v-if="userStore.soundMode !== 'music'">
      <MusicGain id="music" ref="musicGain" :src="src" :title="currentSoundscape" />
    </div>
    <div v-if="adaptive">
      <AdaptiveNoiseGain id="noise" />
    </div>
    <div v-else>
      <NoiseGain id="noise" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, computed } from 'vue'

import AdaptiveNoiseGain from '../experiments/homepages/AdaptiveNoiseGain.vue'
import MusicGain from '../experiments/homepages/MusicGain.vue'
import NoiseGain from '../experiments/homepages/NoiseGain.vue'

import { usePlayerControls } from '~/composables/usePlayerControls'
import { useAudioReplacer } from '~/composables/useAudioReplacer'
import { useUserStore } from '~/stores/user'
import { useAudioStore } from '~/stores/audio'

import tracksConfig, { getSoundscapeByTitle } from '~/tracks.config'

// Refs
const musicGain = ref(null) as any

// Store & Composables
const userStore = useUserStore()
const { addSpaceListener, addMediaControls, getNextSoundscape } = usePlayerControls()
const { getAudioElementForTitle } = useMediaProvider()
const { fadeVolume, replaceAudioWithFade } = useAudioReplacer()
const artworkManager = useArtWorkManager()

const defaultSoundscape = 'Lagoon'
const currentSoundscape = ref(useUserStore().user.settings.soundscape)

// Current state if the user wishes adaptive adaptions
const adaptive = computed(() => userStore.user?.settings?.adaptive_sound_scape || 'no')
const src = computed(() => getSoundscapeByTitle(currentSoundscape.value))

// Watch for changes in the selected soundscape
// Current soundscape (reactive)

const replaceSrc = () => {
  const { replaceAudioWithFade } = useAudioReplacer()
  const title = getNextSoundscape()
  const newSrc = getSoundscapeByTitle(title)
  const duration = 5
  replaceAudioWithFade(title, newSrc, 5)
}

// Mount logic: Register keyboard and media controls
onMounted(() => {
  addSpaceListener()
  addMediaControls()
})
</script>

<style style="scss" scoped>

.nav {
  position: fixed;
  bottom: 3em;
  text-align: center;
  width: 100%;
  padding-right: 0;
  z-index: -1;

  @media only screen and (max-width: 576px) {
    bottom: 1.5em;
  }

  .nav__list {
    box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.1);
    -webkit-box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.1);
    -moz-box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    list-style: none;
    padding: 0;
    margin: 0 auto;
    align-items: center;
    position: relative;
    border-radius: 6px;
    background-color: white;
    flex-basis: 100%;
        max-width: 350px;

    @media only screen and (max-width: 576px) {
      width: 100%;
      margin: 0 auto;
    }
  }

  .nav__item {
    height: 50px;
    padding: 0.25em;
    flex: 1;

    &:hover:not(.nav__item--active) {
      color: #e9c046;

      /* .nav__dropdown {
        display: block;
      } */
    }

    &:first-of-type {
      border-top-left-radius: 6px;
      border-bottom-left-radius: 6px;
    }

    &:last-of-type {
      border-top-right-radius: 6px;
      border-bottom-right-radius: 6px;
    }

    &.nav__item--toggle {
      cursor: pointer;
      padding-left: 0;
      padding-right: 0;

      &:first-of-type {
        margin-left: 0.25em;
      }

      &:last-of-type {
        margin-right: 0.25em;
      }

      &.nav__item--active {

        .nav__item-link {
          background-color: #fff;
        }
      }

      .nav__item-link {
        transition: background-color 0.25s, box-shadow 0.25s, color 0.25s;
        background-color: #F4F5F8;
        box-shadow: inset 0 0 3px 0 rgba(0, 0, 0, 0.1);
      }
    }

    &.nav__item--timer-bg {
      padding: 0;
      border-left: 2px solid #F4F5F8;
      overflow: hidden;

      .nav__item-link {
        position: relative;

        svg {
          z-index: 2;
          position: relative;
        }

        &::after {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          right: calc(100% - var(--progress-width));
          transition: right 0.25s ease-in-out;
          bottom: 0;
          background-color: var(--progress-color);
          opacity: 0.5;
        }
      }
    }

    .nav__item-link {
      color: #585C5E;
      cursor: pointer;
      transition: 250ms ease-in-out;
      height: 100%;
      width: 100%;
      border: none;
      background-color: transparent;

      &:hover {
        color: #e9c046;
      }

      &.nav__item-link--play {
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }

    .nav__item-link--timer {
      &:hover {
        color: #fff;
      }
    }
  }

  .nav__dropdown{
    background-color: #F4F5F8;
    position: absolute;
    bottom: 0;
    padding: 0.75em 1em 4em 1em;
    left: 0;
    right: 0;
    display: none;
    list-style: none;
    border-radius: 6px;
    box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.1);
    -webkit-box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.1);
    -moz-box-shadow: 0 0 14px 0 rgba(0, 0, 0, 0.1);
    z-index: -1;

    &.nav__dropdown--no-padding {
      padding-left: 0;
      padding-right: 0;
    }

    &.open {
      display: block;
    }

    .nav__dropdown-listItem {
      display: flex;
      justify-content: space-between;
      width: 100%;
      padding: 0.5em 1em;
      text-decoration: none;
      cursor: pointer;
      color: #585C5E;
      transition: 250ms ease-in-out;

      &.active, &.is-select {
        background-color: #e9c046;
        color: white;
      }

      &:hover {
        background-color: #e9c046;
        color: white;
      }
    }

    p{
      text-align: left;
    }

    .form-switch {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 1em;
      padding-left: 0;

      .form-switch__label {
        font-weight: 600;
      }
    }
  }
}

</style>
