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

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

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-") || route.hash.startsWith("#lu-")) {
      const slugEnd = route.hash.replace("#", "");
      let wagtailPage = null;

      if (slugEnd.startsWith("ls-")) {
        wagtailPage = circleStore.circle?.learningSequences.find((ls) => {
          return ls.slug.endsWith(slugEnd);
        });
      } else if (slugEnd.startsWith("lu-")) {
        const learningUnits = circleStore.circle?.learningSequences.flatMap(
          (ls) => ls.learningUnits
        );
        wagtailPage = learningUnits.find((lu) => {
          return lu.slug.endsWith(slugEnd);
        });
      }
      if (wagtailPage) {
        document
          .getElementById(wagtailPage.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>
        <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"
                  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 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 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 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>

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