import * as vscode from 'vscode';

export class LauncherViewProvider implements vscode.WebviewViewProvider {
  constructor(private readonly context: vscode.ExtensionContext) {}

  resolveWebviewView(view: vscode.WebviewView) {
    view.webview.options = { enableScripts: true };
    const version = this.context.extension.packageJSON.version as string;
    view.webview.html = getHtml(version);
    view.webview.onDidReceiveMessage((msg) => {
      if (msg?.type === 'open' && typeof msg.command === 'string') {
        vscode.commands.executeCommand(msg.command);
      }
    });
  }
}

function getHtml(version: string): string {
  return `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
  body { font-family: var(--vscode-font-family); color: var(--vscode-foreground); padding: 12px 8px; }
  h3 { margin: 0 0 12px 0; font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; opacity: 0.7; }
  .tool-btn {
    display: block; width: 100%; text-align: left;
    padding: 8px 10px; margin-bottom: 6px;
    background: var(--vscode-button-secondaryBackground);
    color: var(--vscode-button-secondaryForeground);
    border: 1px solid var(--vscode-panel-border);
    border-radius: 3px; cursor: pointer; font-size: 13px;
  }
  .tool-btn:hover {
    background: var(--vscode-button-secondaryHoverBackground);
  }
  .tool-desc { display: block; font-size: 11px; opacity: 0.7; margin-top: 2px; }
</style>
</head>
<body>
  <h3>HPL Toolbox ${version}</h3>
  <button class="tool-btn" data-cmd="hplToolbox.openPlecUpload">
    PLEC Upload
    <span class="tool-desc">Upload HTML to internal PLEC server</span>
  </button>
  <button class="tool-btn" data-cmd="hplToolbox.openApplovinUpload">
    AppLovin Demo Upload
    <span class="tool-desc">Upload to p.applov.in (QR preview)</span>
  </button>
  <button class="tool-btn" data-cmd="hplToolbox.openBase64Scanner">
    Base64 Scanner
    <span class="tool-desc">Find non-base64 assets in HTML</span>
  </button>
  <button class="tool-btn" data-cmd="hplToolbox.openDailyUpdate">
    Daily Update
    <span class="tool-desc">Compose &amp; copy a daily status</span>
  </button>
  <button class="tool-btn" data-cmd="hplToolbox.openSendToMobile">
    Send To Mobile
    <span class="tool-desc">Share HTML to a phone via LAN + QR</span>
  </button>
  <button class="tool-btn" data-cmd="hplToolbox.openPlayworksConverter">
    Playworks Converter
    <span class="tool-desc">Convert Playworks HTML to per-network variants</span>
  </button>
<script>
  const vscode = acquireVsCodeApi();
  document.querySelectorAll('.tool-btn').forEach((btn) => {
    btn.addEventListener('click', () => {
      vscode.postMessage({ type: 'open', command: btn.dataset.cmd });
    });
  });
</script>
</body>
</html>`;
}
