import * as vscode from 'vscode';

export function handleClipboardAndOpen(msg: any): boolean {
  if (msg?.type === 'copy' && typeof msg.text === 'string') {
    vscode.env.clipboard.writeText(msg.text);
    vscode.window.showInformationMessage('Copied to clipboard.');
    return true;
  }
  if (msg?.type === 'open' && typeof msg.text === 'string') {
    vscode.env.openExternal(vscode.Uri.parse(msg.text));
    return true;
  }
  return false;
}

export function getToolWebviewStyles(): string {
  return `
  :root {
    color-scheme: dark;
    --tool-gap-xs: 4px;
    --tool-gap-sm: 8px;
    --tool-gap-md: 12px;
    --tool-gap-lg: 16px;
    --tool-radius: 4px;
    --tool-border: var(--vscode-panel-border, rgba(128, 128, 128, 0.35));
    --tool-panel: var(--vscode-editorWidget-background, var(--vscode-sideBar-background));
    --tool-panel-soft: var(--vscode-textBlockQuote-background, rgba(128, 128, 128, 0.08));
  }
  * { box-sizing: border-box; }
  body {
    margin: 0;
    padding: 0;
    background: var(--vscode-editor-background);
    color: var(--vscode-foreground);
    font-family: var(--vscode-font-family);
    font-size: var(--vscode-font-size);
  }
  .tool-page {
    max-width: 1100px;
    padding: 18px;
  }
  .tool-header {
    margin-bottom: var(--tool-gap-lg);
  }
  .tool-title {
    margin: 0;
    font-size: 18px;
    line-height: 1.25;
    font-weight: 600;
  }
  .tool-description,
  .section-description,
  .muted {
    color: var(--vscode-descriptionForeground);
  }
  .tool-description {
    margin: 5px 0 0;
    max-width: 780px;
    font-size: 12px;
    line-height: 1.45;
  }
  .tool-section {
    margin-bottom: var(--tool-gap-lg);
  }
  .tool-panel {
    margin-bottom: var(--tool-gap-lg);
    border: 1px solid var(--tool-border);
    border-radius: var(--tool-radius);
    background: var(--tool-panel-soft);
    overflow: hidden;
  }
  .is-hidden,
  .tool-panel.is-hidden {
    display: none;
  }
  .panel-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--tool-gap-md);
    padding: 10px 12px;
    border-bottom: 1px solid var(--tool-border);
    background: var(--tool-panel);
  }
  .panel-title {
    margin: 0;
    color: var(--vscode-foreground);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.45px;
    text-transform: uppercase;
  }
  .panel-body {
    padding: 12px;
  }
  .control-group + .control-group {
    margin-top: var(--tool-gap-md);
  }
  .control-label {
    margin: 0 0 6px;
    color: var(--vscode-descriptionForeground);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.4px;
    text-transform: uppercase;
  }
  .section-title {
    margin: 0 0 6px;
    color: var(--vscode-descriptionForeground);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-transform: uppercase;
  }
  .section-description {
    margin: -2px 0 8px;
    font-size: 12px;
    line-height: 1.45;
  }
  .field-row,
  .file-row,
  .action-row {
    display: flex;
    align-items: center;
    gap: var(--tool-gap-sm);
    flex-wrap: wrap;
  }
  .field-row + .field-row {
    margin-top: var(--tool-gap-sm);
  }
  input[type=text],
  input[type=date],
  select,
  textarea {
    min-width: 0;
    padding: 6px 8px;
    background: var(--vscode-input-background);
    color: var(--vscode-input-foreground);
    border: 1px solid var(--vscode-input-border, var(--tool-border));
    border-radius: var(--tool-radius);
    font: inherit;
  }
  input[type=text]:focus,
  input[type=date]:focus,
  select:focus,
  textarea:focus {
    outline: 1px solid var(--vscode-focusBorder);
    border-color: var(--vscode-focusBorder);
  }
  .field-row input[type=text] {
    flex: 1 1 260px;
  }
  button {
    min-height: 28px;
    padding: 5px 12px;
    border: 1px solid transparent;
    border-radius: var(--tool-radius);
    background: var(--vscode-button-background);
    color: var(--vscode-button-foreground);
    font: inherit;
    cursor: pointer;
  }
  button:hover {
    background: var(--vscode-button-hoverBackground);
  }
  button.secondary {
    background: var(--vscode-button-secondaryBackground);
    color: var(--vscode-button-secondaryForeground);
    border-color: var(--tool-border);
  }
  button.secondary:hover {
    background: var(--vscode-button-secondaryHoverBackground);
  }
  button.danger {
    background: transparent;
    color: var(--vscode-errorForeground);
    border-color: var(--tool-border);
  }
  button.danger:hover {
    background: var(--vscode-inputValidation-errorBackground, rgba(248, 81, 73, 0.16));
  }
  .remove-selected {
    min-width: 28px;
    width: 28px;
    padding: 3px 0;
    color: var(--vscode-errorForeground);
    font-size: 16px;
    line-height: 1;
  }
  button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
  .action-row {
    margin-top: var(--tool-gap-md);
  }
  .status-panel,
  .results-panel,
  .result-card {
    border: 1px solid var(--tool-border);
    border-radius: var(--tool-radius);
    background: var(--tool-panel-soft);
  }
  .status-panel {
    min-height: 30px;
    margin-top: var(--tool-gap-md);
    padding: 8px 10px;
    white-space: pre-wrap;
  }
  .status-panel.is-busy {
    display: flex;
    align-items: center;
    gap: 8px;
  }
  .status-panel.is-busy::before {
    content: "";
    width: 12px;
    height: 12px;
    flex: 0 0 auto;
    border: 2px solid color-mix(in srgb, var(--vscode-descriptionForeground) 45%, transparent);
    border-top-color: var(--vscode-foreground);
    border-radius: 50%;
    animation: status-spin 800ms linear infinite;
  }
  @keyframes status-spin {
    to { transform: rotate(360deg); }
  }
  .status-panel:empty {
    display: none;
  }
  .results-panel {
    margin-top: var(--tool-gap-md);
    overflow: auto;
  }
  .results-panel:empty {
    display: none;
  }
  .output-panel .results-panel {
    margin-top: 0;
  }
  .result-card {
    padding: 10px;
    word-break: break-word;
  }
  .data-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
  }
  .data-table th,
  .data-table td {
    padding: 8px 10px;
    text-align: left;
    vertical-align: top;
    border-bottom: 1px solid var(--tool-border);
  }
  .data-table th {
    color: var(--vscode-descriptionForeground);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.4px;
    text-transform: uppercase;
    background: var(--tool-panel);
  }
  .data-table tr:last-child td {
    border-bottom: none;
  }
  .mono {
    font-family: var(--vscode-editor-font-family, monospace);
    font-size: 12px;
  }
  .wrap {
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .file-name {
    color: var(--vscode-descriptionForeground);
    font-size: 12px;
    overflow-wrap: anywhere;
  }
  .ok {
    color: var(--vscode-testing-iconPassed, #3fb950);
  }
  .err {
    color: var(--vscode-errorForeground, #f85149);
  }
  .warn {
    color: var(--vscode-editorWarning-foreground, #d29922);
  }
  .badge {
    display: inline-flex;
    align-items: center;
    min-height: 18px;
    padding: 1px 6px;
    border-radius: 999px;
    border: 1px solid var(--tool-border);
    font-size: 11px;
    line-height: 1.2;
    white-space: nowrap;
  }
  .badge.ok {
    border-color: color-mix(in srgb, var(--vscode-testing-iconPassed, #3fb950) 45%, transparent);
    background: rgba(63, 185, 80, 0.12);
  }
  .badge.err {
    border-color: color-mix(in srgb, var(--vscode-errorForeground, #f85149) 45%, transparent);
    background: var(--vscode-inputValidation-errorBackground, rgba(248, 81, 73, 0.12));
  }
  .badge.warn {
    border-color: color-mix(in srgb, var(--vscode-editorWarning-foreground, #d29922) 45%, transparent);
    background: var(--vscode-inputValidation-warningBackground, rgba(210, 153, 34, 0.12));
  }
`;
}

export function singletonPanel(
  store: { panel: vscode.WebviewPanel | null },
  viewType: string,
  title: string
): { panel: vscode.WebviewPanel; isNew: boolean } {
  if (store.panel) {
    store.panel.reveal();
    return { panel: store.panel, isNew: false };
  }
  const panel = vscode.window.createWebviewPanel(
    viewType,
    title,
    vscode.ViewColumn.Active,
    { enableScripts: true, retainContextWhenHidden: true }
  );
  store.panel = panel;
  panel.onDidDispose(() => { store.panel = null; });
  return { panel, isNew: true };
}
