<script setup lang="ts">
import * as log from 'loglevel'
import LearningSequence from '@/components/circle/LearningSequence.vue'
import CircleOverview from '@/components/circle/CircleOverview.vue'
import CircleDiagram from '@/components/circle/CircleDiagram.vue'
import LearningContent from '@/components/circle/LearningContent.vue'

import { computed, onMounted } from 'vue'
import { useCircleStore } from '@/stores/circle'
import { useAppStore } from '@/stores/app'
import { useRoute } from 'vue-router'
import _ from 'lodash'
import { humanizeDuration } from '@/utils/humanizeDuration'

const route = useRoute()

log.debug('CircleView.vue created', route)

const props = defineProps<{
  learningPathSlug: string
  circleSlug: string
}>()

const appStore = useAppStore()
appStore.showMainNavigationBar = true

const circleStore = useCircleStore()

const duration = computed(() => {
  if (circleStore.circle) {
    const minutes = _.sumBy(circleStore.circle.learningSequences, 'minutes')
    return humanizeDuration(minutes)
  }

  return ''
})

onMounted(async () => {
  log.debug('CircleView.vue mounted', props.learningPathSlug, props.circleSlug)

  try {
    await circleStore.loadCircle(props.learningPathSlug, props.circleSlug)

    if (route.hash.startsWith('#ls-')) {
      const hashLearningSequence = circleStore.circle?.learningSequences.find((ls) => {
        return ls.slug.endsWith(route.hash.replace('#', ''))
      })
      if (hashLearningSequence) {
        document.getElementById(hashLearningSequence.slug)?.scrollIntoView({ behavior: 'smooth' })
      }
    }
  } catch (error) {
    log.error(error)
  }
})
</script>

<template>
  <div>
    <Teleport to="body">
      <CircleOverview
        :circle="circleStore.circle"
        :show="circleStore.page === 'OVERVIEW'"
        @closemodal="circleStore.page = 'INDEX'"
      />
    </Teleport>
    <Transition mode="out-in">
      <div v-if="circleStore.page === 'LEARNING_CONTENT'">
        <LearningContent :key="circleStore.currentLearningContent.translation_key" />
      </div>
      <div v-else>
        <div class="circle-container bg-gray-200">
          <div class="circle max-w-9xl">
            <div class="flex flex-col lg:flex-row">
              <div class="flex-initial lg:w-128 px-4 py-4 lg:px-8 lg:pt-4 bg-white">
                <router-link
                  :to="`/learn/${props.learningPathSlug}`"
                  class="btn-text inline-flex items-center px-3 py-4 font-normal"
                  data-cy="back-to-learning-path-button"
                >
                  <it-icon-arrow-left class="-ml-1 mr-1 h-5 w-5"></it-icon-arrow-left>
                  <span class="inline">zurück zum Lernpfad</span>
                </router-link>

                <h1 class="text-blue-dark text-4xl lg:text-6xl" data-cy="circle-title">
                  {{ circleStore.circle?.title }}
                </h1>

                <div class="mt-2">Dauer: {{ duration }}</div>

                <div class="w-full mt-8">
                  <CircleDiagram></CircleDiagram>
                </div>

                <div class="border-t-2 border-gray-500 mt-4 lg:hidden">
                  <div class="mt-4 inline-flex items-center" @click="circleStore.page = 'OVERVIEW'">
                    <it-icon-info class="mr-2" />
                    Das lernst du in diesem Circle
                  </div>
                  <div class="inline-flex items-center">
                    <it-icon-message class="mr-2" />
                    Fachexpertin kontaktieren
                  </div>
                </div>

                <div class="hidden lg:block">
                  <div class="block border border-gray-500 mt-8 p-6">
                    <h3 class="text-blue-dark">Das lernst du in diesem Circle.</h3>
                    <div class="leading-relaxed mt-4">
                      {{ circleStore.circle?.description }}
                    </div>

                    <button class="btn-primary mt-4 text-xl" @click="circleStore.page = 'OVERVIEW'">
                      Erfahre mehr dazu
                    </button>
                  </div>

                  <div class="expert border border-gray-500 mt-8 p-6">
                    <h3 class="text-blue-dark">Hast du Fragen?</h3>
                    <div class="leading-relaxed mt-4">
                      Tausche dich mit der Fachexpertin aus für den Circle Analyse aus.
                    </div>
                    <button class="btn-secondary mt-4 text-xl">Fachexpertin kontaktieren</button>
                  </div>
                </div>
              </div>

              <div class="flex-auto bg-gray-200 px-4 py-8 lg:px-24">
                <div
                  v-for="learningSequence in circleStore.circle?.learningSequences || []"
                  :key="learningSequence.translation_key"
                >
                  <LearningSequence :learning-sequence="learningSequence"></LearningSequence>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

<style lang="postcss" scoped>
.circle-container {
  /*background: linear-gradient(to right, white 0%, white 50%, theme(colors.gray.200) 50%, theme(colors.gray.200) 100%);*/
}

.circle {
  /*max-width: 1440px;*/
  /*margin: 0 auto;*/
}

.v-enter-active,
.v-leave-active {
  transition: opacity 0.3s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}
</style>
