<!doctype html>
<!-- Copyright (c) Microsoft Corporation.
 Licensed under the MIT License. -->

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <meta name="google" content="notranslate" />
    <title>PDF Viewer - TypeAgent</title>

    <!-- FontAwesome Icons -->
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
    />

    <style>
      body,
      html {
        margin: 0;
        padding: 0;
        height: 100vh;
        overflow: hidden;
        font-family:
          -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        background-color: #323639; /* Match PDF viewer background to prevent flash */
      }

      .header {
        background: #2c3e50;
        color: white;
        padding: 12px 20px;
        display: none; /* Start hidden to prevent flash during PDF load */
        align-items: center;
        justify-content: space-between;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        z-index: 1000;
        position: relative;
      }

      .header h1 {
        margin: 0;
        font-size: 18px;
        font-weight: 500;
      }

      .header-actions {
        display: flex;
        gap: 10px;
        align-items: center;
      }

      .btn {
        background: #3498db;
        color: white;
        border: none;
        padding: 8px 16px;
        border-radius: 4px;
        cursor: pointer;
        font-size: 14px;
        display: flex;
        align-items: center;
        gap: 6px;
        transition: background-color 0.2s;
      }

      .btn:hover {
        background: #2980b9;
      }

      .btn-secondary {
        background: #95a5a6;
      }

      .btn-secondary:hover {
        background: #7f8c8d;
      }

      .loading-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 100vh; /* Start with full height, will be adjusted when header shows */
        background: #323639; /* Match PDF viewer background */
        color: white; /* Adjust text color for dark background */
        opacity: 0; /* Start transparent for delayed loading */
        transition: opacity 0.3s ease-in-out; /* Smooth fade-in transition */
      }

      .loading-spinner {
        width: 40px;
        height: 40px;
        border: 4px solid rgba(255, 255, 255, 0.1); /* Subtle border for dark background */
        border-top: 4px solid #8ab4f8; /* Light blue accent */
        border-radius: 50%;
        animation: spin 1s linear infinite;
        margin-bottom: 20px;
      }

      @keyframes spin {
        0% {
          transform: rotate(0deg);
        }
        100% {
          transform: rotate(360deg);
        }
      }

      .loading-text {
        color: #e8eaed; /* Light text for dark background */
        font-size: 16px;
        margin-bottom: 10px;
      }

      .loading-subtitle {
        color: #9aa0a6; /* Dimmer text for subtitle */
        font-size: 14px;
      }

      .error-container {
        display: none;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 100vh; /* Start with full height, will be adjusted when header shows */
        background: white;
        padding: 40px;
        text-align: center;
      }

      .error-icon {
        font-size: 48px;
        color: #e74c3c;
        margin-bottom: 20px;
      }

      .error-title {
        color: #2c3e50;
        font-size: 24px;
        margin-bottom: 16px;
        font-weight: 500;
      }

      .error-message {
        color: #7f8c8d;
        font-size: 16px;
        margin-bottom: 30px;
        line-height: 1.5;
        max-width: 500px;
      }

      .error-actions {
        display: flex;
        gap: 15px;
        flex-wrap: wrap;
        justify-content: center;
      }

      .pdf-url {
        background: #ecf0f1;
        padding: 10px;
        border-radius: 4px;
        font-family: monospace;
        font-size: 12px;
        color: #2c3e50;
        margin: 20px 0;
        word-break: break-all;
        max-width: 600px;
      }

      #pdfFrame {
        width: 100%;
        height: calc(100vh - 60px);
        border: none;
        background: #323639; /* Match PDF viewer background */
        display: none;
      }

      .url-info {
        font-size: 12px;
        color: #bdc3c7;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        max-width: 300px;
      }

      @media (max-width: 768px) {
        .header {
          padding: 10px 15px;
          flex-direction: column;
          gap: 10px;
        }

        .header h1 {
          font-size: 16px;
        }

        .header-actions {
          flex-wrap: wrap;
          justify-content: center;
        }

        .btn {
          padding: 6px 12px;
          font-size: 12px;
        }

        .url-info {
          max-width: 200px;
        }
      }
    </style>
  </head>
  <body>
    <div class="header">
      <div>
        <h1><i class="fas fa-file-pdf"></i> PDF Viewer</h1>
        <div class="url-info" id="urlInfo">Loading...</div>
      </div>
      <div class="header-actions">
        <button
          class="btn btn-secondary"
          id="backBtn"
          onclick="window.history.back()"
        >
          <i class="fas fa-arrow-left"></i> Back
        </button>
        <button class="btn" id="openOriginalBtn" style="display: none">
          <i class="fas fa-external-link-alt"></i> Open Original
        </button>
        <button class="btn" id="downloadBtn" style="display: none">
          <i class="fas fa-download"></i> Download
        </button>
      </div>
    </div>

    <div id="loadingContainer" class="loading-container">
      <div class="loading-spinner"></div>
      <div class="loading-text">Loading PDF Viewer...</div>
      <div class="loading-subtitle">Connecting to TypeAgent PDF reader</div>
    </div>

    <div id="errorContainer" class="error-container">
      <div class="error-icon">
        <i class="fas fa-exclamation-triangle"></i>
      </div>
      <h2 class="error-title">Unable to Load PDF</h2>
      <p class="error-message" id="errorMessage">
        The PDF document could not be loaded in the TypeAgent viewer.
      </p>
      <div class="pdf-url" id="pdfUrlDisplay"></div>
      <div class="error-actions">
        <button class="btn" id="retryBtn">
          <i class="fas fa-redo"></i> Retry
        </button>
        <button class="btn btn-secondary" id="openInNewTabBtn">
          <i class="fas fa-external-link-alt"></i> Open in New Tab
        </button>
        <button class="btn btn-secondary" onclick="window.history.back()">
          <i class="fas fa-arrow-left"></i> Go Back
        </button>
      </div>
    </div>

    <iframe id="pdfFrame" src="about:blank"></iframe>

    <script src="pdfView.js"></script>
  </body>
</html>
