<template>
  <div v-html="parsedContent"></div>
</template>

<script>
import { createApp, h, ref, watch } from 'vue';
import WorshipRender from './WorshipRender.vue';
import ImageRender from './ImageRender.vue';
import EventRender from './EventRender.vue';
import ContactRender from './ContactRender.vue'; // Importiere die neue Komponente
import InstitutionRender from './InstitutionRender.vue'; // Importiere die neue Komponente
import DownloadLink from './DownloadLink.vue';

export default {
  name: 'RenderContentComponent',
  props: {
    content: {
      type: String,
      required: true,
    },
  },
  setup(props) {
    const parsedContent = ref('');

    const renderContent = (content) => {
      let result = renderWorship(content);
      result = renderImage(result);
      result = renderDownload(result);
      result = renderEvent(result);
      result = renderContact(result); 
      result = renderInstitution(result);
      return result;
    };

    const renderWorship = (content) => {
      const worshipsPattern = /{{ worshipslist:(.*?) }}/g;
      let result = content;
      result = result.replace(worshipsPattern, (match, config) => {
        const placeholderId = `worship-render-placeholder-${Math.random().toString(36).substr(2, 9)}`;
        setTimeout(() => {
          const placeholder = document.getElementById(placeholderId);
          if (placeholder) {
            const app = createApp({ 
              render() {
                return h(WorshipRender, {config: JSON.parse(config) });
              },
            });
            app.mount(placeholder);
          }
        }, 0);
        return `<div id="${placeholderId}"></div>`;
      });
      return result;
    };

    const renderImage = (content) => {
      const imagePattern = /{{ image:(.*?) }}/g;
      let result = content;
      result = result.replace(imagePattern, (match, config) => {
        const placeholderId = `image-render-placeholder-${Math.random().toString(36).substr(2, 9)}`;
        setTimeout(() => {
          const placeholder = document.getElementById(placeholderId);
          if (placeholder) {
            const app = createApp({
              render() {
                return h(ImageRender, { id: config });
              },
            });
            app.mount(placeholder);
          }
        }, 0);
        return `<span id="${placeholderId}"></span>`;
      });
      return result;
    };

    const renderDownload = (content) => {
      const downloadPattern = /{{ download:(.*?) }}/g;
      let result = content;
      result = result.replace(downloadPattern, (match, config) => {
        const placeholderId = `image-render-placeholder-${Math.random().toString(36).substr(2, 9)}`;
        setTimeout(() => {
          const placeholder = document.getElementById(placeholderId);
          if (placeholder) {
            const app = createApp({
              render() {
                return h(DownloadLink, { hash: config });
              },
            });
            app.mount(placeholder);
          }
        }, 0);
        return `<span id="${placeholderId}"></span>`;
      });
      return result;
    };

    const renderEvent = (content) => {
      const eventsPattern = /{{ events:(.*?) }}/g;
      let result = content;
      result = result.replace(eventsPattern, (match, config) => {
        const placeholderId = `event-render-placeholder-${Math.random().toString(36).substr(2, 9)}`;
        setTimeout(() => {
          const placeholder = document.getElementById(placeholderId);
          if (placeholder) {
            const app = createApp({
              render() {
                console.log('RC', config)
                return h(EventRender, {config: JSON.parse(config) });
              },
            });
            app.mount(placeholder);
          }
        }, 0);
        return `<div id="${placeholderId}"></div>`;
      });
      return result;
    };

    const renderContact = (content) => {
      const contactPattern = /{{ contact:(.*?) }}/g;
      let result = content;
      result = result.replace(contactPattern, (match, config) => {
        const props = parseConfig(config);
        const placeholderId = `contact-render-placeholder-${Math.random().toString(36).substr(2, 9)}`;
        setTimeout(() => {
          const placeholder = document.getElementById(placeholderId);
          if (placeholder) {
            const app = createApp({
              render() {
                return h(ContactRender, { config: props });
              },
            });
            app.mount(placeholder);
          }
        }, 0);
        return `<div id="${placeholderId}"></div>`;
      });
      return result;
    };

    const renderInstitution = (content) => {
      const institutionPattern = /{{ institution:(.*?) }}/g;
      let result = content;
      result = result.replace(institutionPattern, (match, config) => {
        const props = parseConfig(config);
        const placeholderId = `institution-render-placeholder-${Math.random().toString(36).substr(2, 9)}`;
        setTimeout(() => {
          const placeholder = document.getElementById(placeholderId);
          if (placeholder) {
            const app = createApp({
              render() {
                return h(InstitutionRender, { config: props });
              },
            });
            app.mount(placeholder);
          }
        }, 0);
        return `<div id="${placeholderId}"></div>`;
      });
      return result;
    };

    const parseConfig = (configString) => {
      try {
        const config = JSON.parse(configString);
        return config;
      } catch (error) {
        console.error('Error parsing config:', error);
        console.log(configString);
        return {};
      }
    };

    watch(
      () => props.content,
      (newContent) => {
        parsedContent.value = renderContent(newContent);
      },
      { immediate: true }
    );

    return {
      parsedContent,
    };
  },
};
</script>

<style scoped>
/* Add styles if needed */
</style>
