/**
 * Glass Slider — surface-transparency slider that drives `transparency` on the
 * theme engine; the engine writes `--bg-glass`, `--bg-glass-2`, `--bg-glass-3`.
 *
 * Mirrors Theme.jsx applyTheme L354-358:
 *   const t = clamp(transparency, 0, 0.30);
 *   root.style.setProperty('--bg-glass',   `rgba(${tint}, ${t * 0.5})`);
 *   root.style.setProperty('--bg-glass-2', `rgba(${tint}, ${t})`);
 *   root.style.setProperty('--bg-glass-3', `rgba(${tint}, ${t * 1.75})`);
 */

import { getTheme, setThemePartial } from '../../services/theme-engine'

const MIN = 0
const MAX = 0.2
const STEP = 0.01

interface GlassSliderOptions {
  /** When true, render an inline "Solid · slider · Frosted" layout (Theme Studio body). */
  expanded?: boolean
}

export class GlassSlider {
  public readonly el: HTMLElement
  private readonly input: HTMLInputElement
  private readonly value: HTMLSpanElement

  constructor(options: GlassSliderOptions = {}) {
    const expanded = options.expanded === true
    const initial = clamp(getTheme().transparency, MIN, MAX)

    this.el = document.createElement('div')
    this.el.className = expanded
      ? 'preview-glass-slider preview-glass-slider--expanded'
      : 'preview-glass-slider'

    if (!expanded) {
      const label = document.createElement('span')
      label.className = 'preview-glass-slider__label'
      label.textContent = 'Glass'
      this.el.appendChild(label)
    }

    const row = document.createElement('div')
    row.className = 'preview-glass-slider__row'

    if (expanded) {
      const left = document.createElement('span')
      left.className = 'preview-glass-slider__edge'
      left.textContent = 'Solid'
      row.appendChild(left)
    }

    this.input = document.createElement('input')
    this.input.type = 'range'
    this.input.min = String(MIN)
    this.input.max = String(MAX)
    this.input.step = String(STEP)
    this.input.value = String(initial)
    this.input.className = 'preview-glass-slider__input'
    this.input.setAttribute('aria-label', 'Surface transparency')
    row.appendChild(this.input)

    if (expanded) {
      const right = document.createElement('span')
      right.className = 'preview-glass-slider__edge'
      right.textContent = 'Frosted'
      row.appendChild(right)
    }

    this.value = document.createElement('span')
    this.value.className = 'preview-glass-slider__value'
    this.value.textContent = formatLevel(initial)
    row.appendChild(this.value)

    this.el.appendChild(row)
    this.applySliderFill(initial)

    this.input.addEventListener('input', () => {
      const next = clamp(Number(this.input.value), MIN, MAX)
      setThemePartial({ transparency: next })
    })

    document.addEventListener('cm:theme-change', () => {
      const t = clamp(getTheme().transparency, MIN, MAX)
      if (Number(this.input.value) !== t) this.input.value = String(t)
      this.value.textContent = formatLevel(t)
      this.applySliderFill(t)
    })
  }

  private applySliderFill(level: number): void {
    const pct = ((level - MIN) / (MAX - MIN)) * 100
    this.input.style.setProperty('--slider-pct', `${pct}%`)
  }
}

function clamp(value: number, min: number, max: number): number {
  if (!Number.isFinite(value)) return min
  return Math.max(min, Math.min(max, value))
}

function formatLevel(level: number): string {
  return `${Math.round(level * 100)}%`
}
