{% extends "components/base.html" %}
{% block title %}post edit{% endblock %}

{% block main_area %}
  <h1 class="pt-3">post edit</h1>
  <div class="container">
    <div class="row mt-3">
      <div class="col-md-6">
        <form method="POST" id="post-form">
          {% csrf_token %}
          <div class="mb-3">
            {{ form.title.label_tag }}
            {{ form.title }}
          </div>
          <div class="mb-3">
            {{ form.summary.label_tag }}
            {{ form.summary }}
          </div>
          <div class="mb-3">
            {{ form.tags.label_tag }}
            {{ form.tags }}
          </div>

          <!-- 마크다운 에디터 -->
          <h2>Contents</h2>
          <div class="col-md-12">
            {{ form.contents }}
          </div>

          <!-- 버튼 -->
          <div class="d-flex justify-content-end mt-4">
            <button type="submit" class="btn btn-primary me-2">Save</button>
            <a href="{% url 'blog:post_list' %}" class="btn btn-secondary">Cancel</a>
          </div>
        </form>
      </div>

      <div class="col-md-6">
        <div id="preview" class="border p-3 bg-light h-100 overflow-auto"></div>
      </div>
    </div>

    <!-- 마크다운 파서 및 스크립트 -->
    <script src="https://cdn.jsdelivr.net/npm/markdown-it/dist/markdown-it.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github-dark.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>

    <script>
      const md = window.markdownit({
        highlight: function (str, lang) {
          if (lang && hljs.getLanguage(lang)) {
            try {
              return hljs
                .highlight(str, {language: lang})
                .value;
            } catch (__) {}
          }
          return '';
        }
      });

      const textarea = document.getElementById("markdown-editor"); // ✅ form.contents에 id="markdown-editor" 필수
      const preview = document.getElementById("preview");

      let presignedUrlMap = new Map(); // object_name => presigned_url 저장

      async function generatePresignedUrls() {
        const markdownContent = textarea.value;
        const lines = markdownContent.split("\n");

        for (let i = 0; i < lines.length; i++) {
          const match = lines[i].match(/!\[Image\]\((.+)\)/);
          if (match) {
            const objectName = match[1];

            if (!presignedUrlMap.has(objectName)) {
              try {
                const response = await fetch("/obs_minio/get_presigned_url/", {
                  method: "POST",
                  headers: {
                    "Content-Type": "application/json"
                  },
                  body: JSON.stringify({object_name: objectName})
                });

                if (response.ok) {
                  const data = await response.json();
                  presignedUrlMap.set(objectName, data.presigned_url);
                }
              } catch (error) {
                console.error("Error fetching presigned URL:", error);
              }
            }
          }
        }
      }

      function updatePreview() {
        const markdownContent = textarea.value;
        const lines = markdownContent.split("\n");
        const previewLines = [...lines];

        for (let i = 0; i < previewLines.length; i++) {
          const match = previewLines[i].match(/!\[Image\]\((.+)\)/);
          if (match) {
            const objectName = match[1];
            if (presignedUrlMap.has(objectName)) {
              const presignedUrl = presignedUrlMap.get(objectName);
              previewLines[i] = `![Image](${presignedUrl})`;
            }
          }
        }

        preview.innerHTML = md.render(previewLines.join("\n"));

        document
          .querySelectorAll("#preview pre code")
          .forEach((block) => {
            hljs.highlightElement(block);
          });
      }

      document.addEventListener("DOMContentLoaded", async function () {
        await generatePresignedUrls();
        updatePreview();
      });

      textarea.addEventListener("input", function () {
        updatePreview(); // 입력 시에도 presigned 치환 적용
      });

      textarea.addEventListener("paste", async function (event) {
        const items = (event.clipboardData || event.originalEvent.clipboardData).items;

        for (const item of items) {
          if (item.type.startsWith("image/")) {
            const file = item.getAsFile();
            if (!file) 
              return;
            
            const formData = new FormData();
            formData.append("image", file);

            try {
              const response = await fetch("/obs_minio/upload/", {
                method: "POST",
                body: formData
              });

              if (response.ok) {
                const data = await response.json();
                const fullImageUrl = data.url;
                const objectName = fullImageUrl
                  .split("/")
                  .slice(-2)
                  .join("/");

                const markdownImage = `![Image](${objectName})\n`;

                // 현재 커서 위치에 이미지 삽입
                const cursorPos = textarea.selectionStart;
                const textBefore = textarea
                  .value
                  .substring(0, cursorPos);
                const textAfter = textarea
                  .value
                  .substring(cursorPos);
                textarea.value = textBefore + markdownImage + textAfter;

                textarea.focus();

                // 새 이미지니까 presigned 새로 받아야 함
                presignedUrlMap.delete(objectName);
                await generatePresignedUrls();
                updatePreview();
              } else {
                alert("Image upload failed. Please try again.");
              }
            } catch (error) {
              console.error("Error uploading image:", error);
              alert("An error occurred during image upload.");
            }
          }
        }
      });
    </script>

  </div>
{% endblock %}
