<!DOCTYPE html>
<html th:lang="${#locale.language}" th:dir="#{language.direction}" th:data-language="${#locale.toString()}"
  xmlns:th="https://www.thymeleaf.org">

<head>
  <th:block th:insert="~{fragments/common :: head(title=#{sign.title}, header=#{sign.header})}"></th:block>
  <link rel="stylesheet" th:href="@{'/css/sign.css'}">

  <th:block th:each="font : ${fonts}">
    <style th:inline="text">
      @font-face {
        font-family: "[[${font.name}]]";
        src: url('fonts/[[${font.name}]].[[${font.extension}]]') format('[[${font.type}]]');
      }

      #font-select option[value="[[${font.name}]]"] {
        font-family: "[[${font.name}]]",
        cursive;
      }
    </style>
  </th:block>
  <script th:src="@{'/js/thirdParty/signature_pad.umd.min.js'}"></script>
  <script th:src="@{'/js/thirdParty/interact.min.js'}"></script>
</head>

<body>
  <div id="page-container">
    <div id="content-wrap">
      <th:block th:insert="~{fragments/navbar.html :: navbar}"></th:block>
      <br><br>
      <div class="container">
        <div class="row justify-content-center">
          <div class="col-md-7 bg-card">
            <div class="tool-header">
              <span class="material-symbols-rounded tool-header-icon sign">signature</span>
              <span class="tool-header-text" th:text="#{sign.header}"></span>
            </div>

            <!-- pdf selector -->
            <div
              th:replace="~{fragments/common :: fileSelector(name='pdf-upload', multipleInputsForSingleRequest=false, disableMultipleFiles=true, accept='application/pdf')}">
            </div>
            <script type="module" th:src="@{'/pdfjs-legacy/pdf.mjs'}"></script>
            <div class="tab-group show-on-file-selected">
              <div class="tab-container" th:title="#{sign.upload}">
                <div
                  th:replace="~{fragments/common :: fileSelector(name='image-upload', disableMultipleFiles=false, multipleInputsForSingleRequest=true, accept='image/*', inputText=#{imgPrompt})}">
                </div>
              </div>

              <div class="tab-container drawing-pad-container" th:title="#{sign.draw}">
                <canvas id="drawing-pad-canvas"></canvas>
                <br>
                <button id="clear-signature" class="btn btn-outline-danger mt-2" onclick="signaturePad.clear()"
                  th:text="#{sign.clear}"></button>
                <button id="save-signature" class="btn btn-outline-success mt-2" onclick="addDraggableFromPad()"
                  th:text="#{sign.add}"></button>
              </div>

              <div class="tab-container" th:title="#{sign.saved}">
                <div class="saved-signatures-section" th:if="${not #lists.isEmpty(signatures)}">

                  <!-- Preview Modal -->
                  <div class="modal fade" id="signaturePreview" tabindex="-1">
                    <div class="modal-dialog modal-dialog-centered">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title"><span id="previewFileName"></span></h5>
                          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body text-center">
                          <img id="previewImage" src="" alt="Signature Preview" style="max-width: 100%;">
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"
                            th:text="#{close}"></button>
                          <button type="button" class="btn btn-primary" onclick="addSignatureFromPreview()"
                            th:text="#{addToDoc}">Add to Document</button>
                        </div>
                      </div>
                    </div>
                  </div>
                  <!-- Personal Signatures -->
                  <div th:if="${not #lists.isEmpty(signatures.?[category == 'Personal'])}">
                    <h5 th:text="#{sign.personalSigs}"></h5>
                    <div class="selected-files flex-wrap" style="position: relative; display:flex">
                      <div th:each="sig : ${signatures}" th:if="${sig.category == 'Personal'}"
                        class="small-file-container-saved d-flex flex-column justify-content-center align-items-center">
                        <div class="drag-icon"><svg xmlns="http://www.w3.org/2000/svg" height="24px"
                            viewBox="0 -960 960 960" width="24px" fill="#e8eaed">
                            <path
                              d="M360-160q-33 0-56.5-23.5T280-240q0-33 23.5-56.5T360-320q33 0 56.5 23.5T440-240q0 33-23.5 56.5T360-160Zm240 0q-33 0-56.5-23.5T520-240q0-33 23.5-56.5T600-320q33 0 56.5 23.5T680-240q0 33-23.5 56.5T600-160ZM360-400q-33 0-56.5-23.5T280-480q0-33 23.5-56.5T360-560q33 0 56.5 23.5T440-480q0 33-23.5 56.5T360-400Zm240 0q-33 0-56.5-23.5T520-480q0-33 23.5-56.5T600-560q33 0 56.5 23.5T680-480q0 33-23.5 56.5T600-400ZM360-640q-33 0-56.5-23.5T280-720q0-33 23.5-56.5T360-800q33 0 56.5 23.5T440-720q0 33-23.5 56.5T360-640Zm240 0q-33 0-56.5-23.5T520-720q0-33 23.5-56.5T600-800q33 0 56.5 23.5T680-720q0 33-23.5 56.5T600-640Z">
                            </path>
                          </svg></div>
                        <img th:src="@{'/api/v1/general/sign/' + ${sig.fileName}}" th:alt="${sig.fileName}"
                          th:data-filename="${sig.fileName}" style="width: 50px; height: 50px; object-fit: cover;" />
                        <div class="file-info d-flex flex-column align-items-center justify-content-center">
                          <div class="signature-name" th:text="${sig.fileName}"></div>
                        </div>

                      </div>
                    </div>
                  </div>

                  <!-- Shared Signatures -->
                  <div th:if="${not #lists.isEmpty(signatures.?[category == 'Shared'])}">
                    <h5 th:text="#{sign.sharedSigs}"></h5>
                    <div class="selected-files flex-wrap" style="position: relative; display: flex;">
                      <div th:each="sig : ${signatures}" th:if="${sig.category == 'Shared'}"
                        class="small-file-container-saved d-flex flex-column justify-content-center align-items-center">
                        <div class="drag-icon"><svg xmlns="http://www.w3.org/2000/svg" height="24px"
                            viewBox="0 -960 960 960" width="24px" fill="#e8eaed">
                            <path
                              d="M360-160q-33 0-56.5-23.5T280-240q0-33 23.5-56.5T360-320q33 0 56.5 23.5T440-240q0 33-23.5 56.5T360-160Zm240 0q-33 0-56.5-23.5T520-240q0-33 23.5-56.5T600-320q33 0 56.5 23.5T680-240q0 33-23.5 56.5T600-160ZM360-400q-33 0-56.5-23.5T280-480q0-33 23.5-56.5T360-560q33 0 56.5 23.5T440-480q0 33-23.5 56.5T360-400Zm240 0q-33 0-56.5-23.5T520-480q0-33 23.5-56.5T600-560q33 0 56.5 23.5T680-480q0 33-23.5 56.5T600-400ZM360-640q-33 0-56.5-23.5T280-720q0-33 23.5-56.5T360-800q33 0 56.5 23.5T440-720q0 33-23.5 56.5T360-640Zm240 0q-33 0-56.5-23.5T520-720q0-33 23.5-56.5T600-800q33 0 56.5 23.5T680-720q0 33-23.5 56.5T600-640Z">
                            </path>
                          </svg></div>
                        <img th:src="@{'/api/v1/general/sign/' + ${sig.fileName}}" th:alt="${sig.fileName}"
                          th:data-filename="${sig.fileName}" style="width: 50px; height: 50px; object-fit: cover;" />
                        <div class="file-info d-flex flex-column align-items-center justify-content-center">
                          <div class="signature-name" th:text="${sig.fileName}"></div>
                        </div>
                      </div>
                    </div>
                  </div>

                </div>
                <div th:if="${#lists.isEmpty(signatures)}" class="text-center p-3">
                  <p th:text="#{sign.noSavedSigs}">No saved signatures found</p>
                </div>
              </div>

              <div class="tab-container" th:title="#{sign.text}">
                <label class="form-check-label" for="sigText" th:text="#{text}"></label>
                <textarea class="form-control" id="sigText" name="sigText" rows="3"></textarea>
                <label th:text="#{font}"></label>
                <select class="form-control" name="font" id="font-select">
                  <option th:each="font : ${fonts}" th:value="${font.name}" th:text="${font.name}"
                    th:class="${font.name.toLowerCase()+'-font'}"></option>
                </select>
                <div class="margin-auto-parent">
                  <button id="save-text-signature" class="btn btn-outline-success mt-2 margin-center"
                    onclick="addDraggableFromText()" th:text="#{sign.add}"></button>
                </div>
              </div>
            </div>
            <script th:src="@{'/js/sign/signature-canvas.js'}"></script>
            <!-- draggables box -->
            <div class="draggable-buttons-box ignore-rtl">
              <button class="btn btn-outline-secondary"
                onclick="DraggableUtils.deleteDraggableCanvas(DraggableUtils.getLastInteracted())"
                style="border-color: #C02223; color: #C02223; background: rgba(255, 0, 0, 0.1)">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash"
                  viewBox="0 0 16 16">
                  <path
                    d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5Zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5Zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6Z" />
                  <path
                    d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1ZM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118ZM2.5 3h11V2h-11v1Z" />
                </svg>
                <span class="btn-tooltip" th:text="#{sign.delete}"></span>
              </button>
              <button class="btn btn-outline-secondary"
                onclick="DraggableUtils.addAllPagesDraggableCanvas(DraggableUtils.getLastInteracted())">
                <span class="material-symbols-rounded">
                  content_copy
                </span>
                <span class="btn-tooltip" th:text="#{sign.addToAll}"></span>
              </button>
              <div id="rotation-controls" class="align-items-center" style="display: none;">
                <div class="input-with-icon">
                  <span class="material-symbols-rounded icon" style="margin-right: 3px;">
                    screen_rotation
                  </span>
                  <input type="number" id="rotation-input" class="form-control form-control-sm me-2" value="0" step="10"
                    style="width: 6rem" />
                </div>
              </div>
              <button id="ratioToggleBtn" class="btn btn-outline-secondary"
                onclick="DraggableUtils.toggleMaintainRatio()">
                <span class="material-symbols-rounded">
                  Aspect_Ratio
                </span>
                <span class="btn-tooltip" th:text="#{sign.maintainRatio}"></span>
              </button>
              <button class="btn btn-outline-secondary" onclick="goToFirstOrLastPage(false)" style="margin-left:auto">
                <span class="material-symbols-rounded">
                  keyboard_double_arrow_left
                </span>
                <span class="btn-tooltip" th:text="#{sign.first}"></span>
              </button>
              <button class="btn btn-outline-secondary" id="incrementPage"
                onclick="document.documentElement.getAttribute('dir')==='rtl' ? DraggableUtils.incrementPage() : DraggableUtils.decrementPage()">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                  class="bi bi-chevron-left" viewBox="0 0 16 16">
                  <path fill-rule="evenodd"
                    d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z" />
                </svg>
                <span class="btn-tooltip" th:text="#{sign.previous}"></span>
              </button>
              <button class="btn btn-outline-secondary" id="decrementPage"
                onclick="document.documentElement.getAttribute('dir')==='rtl' ? DraggableUtils.decrementPage() : DraggableUtils.incrementPage()">
                <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                  class="bi bi-chevron-right" viewBox="0 0 16 16">
                  <path fill-rule="evenodd"
                    d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z" />
                </svg>
                <span class="btn-tooltip" th:text="#{sign.next}"></span>
              </button>
              <button class="btn btn-outline-secondary" onclick="goToFirstOrLastPage(true)">
                <span class="material-symbols-rounded">
                  keyboard_double_arrow_right
                </span>
                <span class="btn-tooltip" th:text="#{sign.last}"></span>
              </button>
              <button id="download-pdf" class="btn btn-outline-secondary"
                style="border-color: green; color:#b2e3a8; background: rgba(24, 122, 5, 1)">
                <span class="material-symbols-rounded">
                  download
                </span>
                <span class="btn-tooltip" th:text="#{downloadPdf}"></span>
              </button>
            </div>
            <div id="box-drag-container" class="show-on-file-selected">

              <canvas id="pdf-canvas"></canvas>
              <script th:src="@{'/js/thirdParty/pdf-lib.min.js'}"></script>
              <script src="https://cdn.jsdelivr.net/npm/fabric@latest/dist/index.min.js"></script>
              <script th:src="@{'/js/draggable-utils.js'}"></script>
              <script type="module" th:src="@{'/js/pages/sign.js'}"></script>
            </div>

          </div>
        </div>
      </div>
    </div>
    <th:block th:insert="~{fragments/footer.html :: footer}"></th:block>
  </div>
  </script>

</body>

</html>