/**
 * 上下文压缩组件
 * 提供上下文使用情况显示和压缩功能
 */

/**
 * 获取上下文压缩组件的 HTML 内容
 */
export function getContextCompressContent(): string {
  return `
    <!-- 上下文显示 -->
    <div class="context-display">
      <div class="context-info" onclick="toggleContextPanel()">
        <div class="database-icon">
          <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" class="db-svg">
            <!-- 数据库容器主体 - 底层灰色 -->
            <path d="M870.4 57.6C780.8 19.2 652.8 0 512 0 371.2 0 243.2 19.2 153.6 57.6 51.2 102.4 0 153.6 0 211.2l0 595.2c0 57.6 51.2 115.2 153.6 153.6C243.2 1004.8 371.2 1024 512 1024c140.8 0 268.8-19.2 358.4-57.6 96-38.4 153.6-96 153.6-153.6L1024 211.2C1024 153.6 972.8 102.4 870.4 57.6L870.4 57.6zM812.8 320C729.6 352 614.4 364.8 512 364.8 403.2 364.8 294.4 352 211.2 320 115.2 294.4 70.4 256 70.4 211.2c0-38.4 51.2-76.8 140.8-108.8C294.4 76.8 403.2 64 512 64c102.4 0 217.6 19.2 300.8 44.8 89.6 32 140.8 70.4 140.8 108.8C953.6 256 908.8 294.4 812.8 320L812.8 320zM819.2 505.6C736 531.2 620.8 550.4 512 550.4c-108.8 0-217.6-19.2-307.2-44.8C115.2 473.6 64 435.2 64 396.8L64 326.4C128 352 172.8 384 243.2 396.8 326.4 416 416 428.8 512 428.8c96 0 185.6-12.8 268.8-32C851.2 384 896 352 960 326.4l0 76.8C960 435.2 908.8 473.6 819.2 505.6L819.2 505.6zM819.2 710.4c-83.2 25.6-198.4 44.8-307.2 44.8-108.8 0-217.6-19.2-307.2-44.8C115.2 684.8 64 646.4 64 601.6L64 505.6c64 32 108.8 57.6 179.2 76.8C326.4 601.6 416 614.4 512 614.4c96 0 185.6-12.8 268.8-32C851.2 563.2 896 537.6 960 505.6l0 96C960 646.4 908.8 684.8 819.2 710.4L819.2 710.4zM512 960c-108.8 0-217.6-19.2-307.2-44.8C115.2 889.6 64 851.2 64 812.8l0-96c64 32 108.8 57.6 179.2 76.8 76.8 19.2 172.8 32 262.4 32 96 0 185.6-12.8 268.8-32 76.8-19.2 121.6-44.8 185.6-76.8l0 96c0 38.4-51.2 76.8-140.8 108.8C736 947.2 614.4 960 512 960L512 960z" fill="#94a3b8" class="db-body"/>

            <!-- 填充进度效果 - 从下往上填充蓝色 -->
            <defs>
              <mask id="fill-mask">
                <rect x="0" y="0" width="1024" height="1024" id="fillRect" fill="white"/>
              </mask>
            </defs>

            <g mask="url(#fill-mask)">
              <path d="M870.4 57.6C780.8 19.2 652.8 0 512 0 371.2 0 243.2 19.2 153.6 57.6 51.2 102.4 0 153.6 0 211.2l0 595.2c0 57.6 51.2 115.2 153.6 153.6C243.2 1004.8 371.2 1024 512 1024c140.8 0 268.8-19.2 358.4-57.6 96-38.4 153.6-96 153.6-153.6L1024 211.2C1024 153.6 972.8 102.4 870.4 57.6L870.4 57.6zM812.8 320C729.6 352 614.4 364.8 512 364.8 403.2 364.8 294.4 352 211.2 320 115.2 294.4 70.4 256 70.4 211.2c0-38.4 51.2-76.8 140.8-108.8C294.4 76.8 403.2 64 512 64c102.4 0 217.6 19.2 300.8 44.8 89.6 32 140.8 70.4 140.8 108.8C953.6 256 908.8 294.4 812.8 320L812.8 320zM819.2 505.6C736 531.2 620.8 550.4 512 550.4c-108.8 0-217.6-19.2-307.2-44.8C115.2 473.6 64 435.2 64 396.8L64 326.4C128 352 172.8 384 243.2 396.8 326.4 416 416 428.8 512 428.8c96 0 185.6-12.8 268.8-32C851.2 384 896 352 960 326.4l0 76.8C960 435.2 908.8 473.6 819.2 505.6L819.2 505.6zM819.2 710.4c-83.2 25.6-198.4 44.8-307.2 44.8-108.8 0-217.6-19.2-307.2-44.8C115.2 684.8 64 646.4 64 601.6L64 505.6c64 32 108.8 57.6 179.2 76.8C326.4 601.6 416 614.4 512 614.4c96 0 185.6-12.8 268.8-32C851.2 563.2 896 537.6 960 505.6l0 96C960 646.4 908.8 684.8 819.2 710.4L819.2 710.4zM512 960c-108.8 0-217.6-19.2-307.2-44.8C115.2 889.6 64 851.2 64 812.8l0-96c64 32 108.8 57.6 179.2 76.8 76.8 19.2 172.8 32 262.4 32 96 0 185.6-12.8 268.8-32 76.8-19.2 121.6-44.8 185.6-76.8l0 96c0 38.4-51.2 76.8-140.8 108.8C736 947.2 614.4 960 512 960L512 960z" fill="#409eff" class="db-fill"/>
            </g>
          </svg>
        </div>
        <span class="context-percentage" id="contextPercentage">0%</span>
      </div>

      <!-- 上下文信息弹窗 -->
      <div id="contextPanel" class="context-panel">
        <div class="context-panel-content">
          <div class="context-info-text" id="contextInfoText">
            0k / 200k 已用上下文
          </div>
          <button class="compress-button" onclick="compressConversation()">
            压缩会话
          </button>
        </div>
      </div>
    </div>
  `;
}

/**
 * 获取上下文压缩组件的样式
 */
export function getContextCompressStyles(): string {
  return `
    /* 上下文显示样式 */
    .context-display {
      display: flex;
      flex-direction: column;
      align-items: center;
      position: relative;
    }
    .context-info {
      display: flex;
      align-items: center;
      gap: 6px;
      height: 40px;
      background: transparent;
      border: none;
      border-radius: 4px;
      font-size: 14px;
      font-weight: 500;
      color: var(--vscode-foreground);
      transition: opacity 0.3s ease;
      box-shadow: none;
      position: relative;
      overflow: hidden;
      cursor: pointer;
    }
    .context-info:hover {
      opacity: 0.8;
    }
    .database-icon {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 12px;
      height: 12px;
      position: relative;
    }
    .db-svg {
      width: 100%;
      height: 100%;
    }
    .db-body {
      fill: #ffffff;
    }
    .db-fill {
      fill: #409eff;
      transition: all 0.3s ease;
    }
    .context-percentage {
      font-size: 14px;
      font-weight: 500;
      color: var(--vscode-foreground);
      text-align: right;
    }
    /* 上下文信息弹窗样式 */
    .context-panel {
      position: absolute;
      bottom: 100%;
      left: 50%;
      transform: translateX(-50%);
      margin-bottom: 8px;
      z-index: 1000;
      animation: fadeInUp 0.2s ease-out;
      display: none;
    }
    .context-panel.active {
      display: block;
    }
    .context-panel::after {
      content: "";
      position: absolute;
      bottom: -6px;
      left: 50%;
      transform: translateX(-50%);
      width: 0;
      height: 0;
      border-left: 6px solid transparent;
      border-right: 6px solid transparent;
      border-top: 6px solid #ffffff;
    }
    .context-panel-content {
      background: #ffffff;
      border: 1px solid rgba(0, 0, 0, 0.1);
      border-radius: 8px;
      padding: 12px;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
      backdrop-filter: blur(10px);
      min-width: 160px;
    }
    .context-info-text {
      font-size: 12px;
      color: #374151;
      text-align: center;
      margin-bottom: 8px;
      white-space: nowrap;
    }
    .compress-button {
      width: 100%;
      background: linear-gradient(145deg, #3b82f6 0%, #1d4ed8 100%);
      border: 1px solid rgba(59, 130, 246, 0.3);
      border-radius: 6px;
      color: white;
      font-size: 12px;
      font-weight: 500;
      padding: 6px 12px;
      cursor: pointer;
      transition: all 0.2s ease;
    }
    .compress-button:hover {
      background: linear-gradient(145deg, #2563eb 0%, #1e40af 100%);
      transform: translateY(-1px);
      box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
    }
    .compress-button:active {
      transform: translateY(0);
    }
    @keyframes fadeInUp {
      from {
        opacity: 0;
        transform: translateX(-50%) translateY(10px);
      }
      to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
      }
    }
  `;
}

/**
 * 获取上下文压缩组件的脚本
 */
export function getContextCompressScript(): string {
  return `
    // 上下文面板相关函数
    function toggleContextPanel() {
      const contextPanel = document.getElementById('contextPanel');
      if (contextPanel) {
        if (contextPanel.classList.contains('active')) {
          contextPanel.classList.remove('active');
        } else {
          contextPanel.classList.add('active');
        }
      }
    }

    function compressConversation() {
      // 发送压缩会话请求
      vscode.postMessage({ command: 'compressConversation' });
      addMessage('正在压缩会话...', 'bot');

      // 关闭面板
      const contextPanel = document.getElementById('contextPanel');
      if (contextPanel) {
        contextPanel.classList.remove('active');
      }
    }

    function updateContextDisplay(currentTokens, maxTokens) {
      const percentage = Math.min(Math.round((currentTokens / maxTokens) * 100), 100);

      // 更新百分比显示
      const contextPercentage = document.getElementById('contextPercentage');
      if (contextPercentage) {
        contextPercentage.textContent = percentage + '%';
      }

      // 更新详细信息
      const contextInfoText = document.getElementById('contextInfoText');
      if (contextInfoText) {
        const currentK = Math.round((currentTokens / 1000) * 10) / 10;
        const maxK = Math.round(maxTokens / 1000);
        contextInfoText.textContent = \`\${currentK}k / \${maxK}k 已用上下文\`;
      }

      // 更新SVG填充效果（从下往上填充）
      const fillRect = document.getElementById('fillRect');
      if (fillRect) {
        const fillHeight = (1024 * percentage) / 100;
        const fillY = 1024 - fillHeight;
        fillRect.setAttribute('y', fillY.toString());
        fillRect.setAttribute('height', fillHeight.toString());
      }
    }

    // 点击外部关闭上下文面板
    document.addEventListener('click', (event) => {
      const contextDisplay = document.querySelector('.context-display');
      const contextPanel = document.getElementById('contextPanel');

      if (contextPanel && contextPanel.classList.contains('active') && contextDisplay) {
        if (!contextDisplay.contains(event.target)) {
          contextPanel.classList.remove('active');
        }
      }
    });
  `;
}
