/**
 * 模型选择器组件
 */

/**
 * 获取模型选择器的 HTML 内容
 */
export function getModelSelectorContent(
  autoIcon: string = "",
  liteIcon: string = "",
  syIcon: string = "",
  maxIcon: string = ""
): string {
  return `
    <!-- 模型选择 -->
    <div class="tooltip">
      <div class="custom-select" id="modelSelect">
        <div class="select-trigger" onclick="toggleModelDropdown()">
          <span class="select-value" id="modelValue">Auto</span>
          <svg class="select-arrow" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
            <path d="M507.8 727.728a30.016 30.016 0 0 1-21.288-8.824L231.104 463.496a30.088 30.088 0 0 1 0-42.568 30.088 30.088 0 0 1 42.568 0l234.128 234.128 234.16-234.128a30.088 30.088 0 0 1 42.568 0 30.088 30.088 0 0 1 0 42.568L529.08 718.904a30 30 0 0 1-21.28 8.824z" fill="#8a8a8a"/>
          </svg>
        </div>
        <div class="select-dropdown" id="modelDropdown">
          <div class="select-option selected" data-value="auto" onclick="selectModel('auto', 'Auto')">
            ${
              autoIcon
                ? `<img src="${autoIcon}" class="model-icon" alt="Auto">`
                : ""
            }
            <div class="option-content">
              <span class="option-label">Auto</span>
              <span class="option-desc">智能匹配最优模型</span>
            </div>
          </div>
          <div class="select-option" data-value="lite" onclick="selectModel('lite', 'Lite')">
            ${
              liteIcon
                ? `<img src="${liteIcon}" class="model-icon" alt="Lite">`
                : ""
            }
            <div class="option-content">
              <span class="option-label">Lite</span>
              <span class="option-desc">基础模型，快速相应，适合简单任务</span>
            </div>
          </div>
          <div class="select-option" data-value="syntaxic" onclick="selectModel('syntaxic', 'Syntaxic')">
            ${
              syIcon
                ? `<img src="${syIcon}" class="model-icon" alt="Syntaxic">`
                : ""
            }
            <div class="option-content">
              <span class="option-label">Syntaxic</span>
              <span class="option-desc">均衡成本和性能，节省credits同时保持可靠输出</span>
            </div>
          </div>
          <div class="select-option" data-value="max" onclick="selectModel('max', 'Max')">
            ${
              maxIcon
                ? `<img src="${maxIcon}" class="model-icon" alt="Max">`
                : ""
            }
            <div class="option-content">
              <span class="option-label">Max</span>
              <span class="option-desc">最强性能，质量优先，适合复杂任务</span>
            </div>
          </div>
        </div>
      </div>
      <span class="tooltiptext">选择模型</span>
    </div>
  `;
}

/**
 * 获取模型选择器的样式
 */
export function getModelSelectorStyles(): string {
  return `
    /* 自定义下拉框样式 */
    .custom-select {
      position: relative;
      user-select: none;
    }
    .select-trigger {
      display: flex;
      align-items: center;
      gap: 6px;
      padding: 4px 8px;
      background: var(--vscode-input-background);
      color: var(--vscode-foreground);
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 12px;
      transition: background 0.2s ease;
    }
    .select-trigger:hover {
      background: var(--vscode-list-hoverBackground);
    }
    .select-value {
      white-space: nowrap;
    }
    .select-arrow {
      width: 12px;
      height: 12px;
      flex-shrink: 0;
      transition: transform 0.2s ease;
    }
    .custom-select.active .select-arrow {
      transform: rotate(180deg);
    }
    .select-dropdown {
      position: absolute;
      bottom: calc(100% + 2px);
      left: 0;
      min-width: 100%;
      background: var(--vscode-dropdown-background);
      border: 1px solid var(--vscode-dropdown-border);
      border-radius: 4px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      z-index: 1100;
      display: none;
      overflow: visible;
    }
    .custom-select.active .select-dropdown {
      display: block;
    }
    /* 模型选择器的选项样式 */
    #modelDropdown .select-option {
      position: relative;
      padding: 8px 12px;
      cursor: pointer;
      transition: background 0.2s ease;
      display: flex;
      flex-direction: row;
      align-items: center;
      gap: 10px;
    }
    #modelDropdown .select-option:hover {
      background: rgba(128, 128, 128, 0.3);
    }
    #modelDropdown .select-option.selected {
      background: rgba(128, 128, 128, 0.5);
      color: var(--vscode-foreground);
    }
    .model-icon {
      width: 16px;
      height: 16px;
      flex-shrink: 0;
      object-fit: contain;
    }
    .option-content {
      display: flex;
      flex-direction: column;
      gap: 2px;
      flex: 1;
    }
    .option-label {
      font-size: 13px;
      color: var(--vscode-foreground);
      font-weight: 500;
      white-space: nowrap;
    }
    .option-desc {
      font-size: 11px;
      color: var(--vscode-descriptionForeground);
      white-space: nowrap;
    }
  `;
}

/**
 * 获取模型选择器的脚本
 */
export function getModelSelectorScript(): string {
  return `
    // 模型选择相关变量
    let currentModel = 'auto';

    // 切换模型下拉框显示/隐藏
    function toggleModelDropdown() {
      const modelSelect = document.getElementById('modelSelect');
      const customSelect = document.getElementById('customSelect');
      if (modelSelect) {
        modelSelect.classList.toggle('active');
        // 关闭模式下拉框
        if (customSelect) {
          customSelect.classList.remove('active');
        }
      }
    }

    // 选择模型
    function selectModel(value, label) {
      currentModel = value;
      const modelValue = document.getElementById('modelValue');
      if (modelValue) {
        modelValue.textContent = label;
      }

      // 更新选中状态
      const options = document.querySelectorAll('#modelDropdown .select-option');
      options.forEach(option => {
        if (option.getAttribute('data-value') === value) {
          option.classList.add('selected');
        } else {
          option.classList.remove('selected');
        }
      });

      // 关闭下拉框
      const modelSelect = document.getElementById('modelSelect');
      if (modelSelect) {
        modelSelect.classList.remove('active');
      }
    }

    // 点击外部关闭模型下拉框
    document.addEventListener('click', (event) => {
      const modelSelect = document.getElementById('modelSelect');
      if (modelSelect && !modelSelect.contains(event.target)) {
        modelSelect.classList.remove('active');
      }
    });

    // 获取当前选中的模型
    function getCurrentModel() {
      return currentModel;
    }
  `;
}
