<script lang="ts">
  /**
   * Grid settings UI component
   * Configures grid size, visibility, and snap-to-grid
   */
  import { grid } from '$lib/canvas/grid';
  import { createEventDispatcher } from 'svelte';

  const dispatch = createEventDispatcher();

  function handleSizeChange(event: Event) {
    const value = parseInt((event.target as HTMLInputElement).value, 10);
    grid.setSize(value);
    dispatch('settings-change', { size: value });
  }

  function handleVisibleToggle() {
    grid.toggleVisible();
    dispatch('settings-change', { visible: !$grid.visible });
  }

  function handleSnapToggle() {
    grid.toggleSnap();
    dispatch('settings-change', { snap: !$grid.snapEnabled });
  }
</script>

<div class="grid-settings">
  <div class="settings-header">
    <h4>Grid Settings</h4>
  </div>

  <div class="settings-content">
    <!-- Grid Visibility -->
    <div class="setting-row">
      <label for="grid-visible">
        <input
          id="grid-visible"
          type="checkbox"
          checked={$grid.visible}
          on:change={handleVisibleToggle}
        />
        <span>Show Grid</span>
      </label>
    </div>

    <!-- Snap to Grid -->
    <div class="setting-row">
      <label for="grid-snap">
        <input
          id="grid-snap"
          type="checkbox"
          checked={$grid.snapEnabled}
          on:change={handleSnapToggle}
        />
        <span>Snap to Grid</span>
      </label>
    </div>

    <!-- Grid Size -->
    <div class="setting-row">
      <label for="grid-size">
        Grid Size
        <span class="value">{$grid.size}px</span>
      </label>
      <input
        id="grid-size"
        type="range"
        min="5"
        max="200"
        step="5"
        value={$grid.size}
        on:input={handleSizeChange}
      />
    </div>
  </div>
</div>

<style>
  .grid-settings {
    background-color: var(--color-bg, #ffffff);
    border: 1px solid var(--color-border, #e5e7eb);
    border-radius: 0.5rem;
    padding: 1rem;
    min-width: 250px;
  }

  .settings-header {
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--color-border, #e5e7eb);
  }

  .settings-header h4 {
    margin: 0;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--color-text, #111827);
  }

  .settings-content {
    display: flex;
    flex-direction: column;
    gap: 0.875rem;
  }

  .setting-row {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--color-text-secondary, #6b7280);
    cursor: pointer;
  }

  label span {
    flex: 1;
  }

  .value {
    font-weight: 600;
    color: var(--color-text, #374151);
  }

  input[type='checkbox'] {
    width: 18px;
    height: 18px;
    cursor: pointer;
  }

  input[type='range'] {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: var(--color-bg-secondary, #e5e7eb);
    outline: none;
    appearance: none;
    -webkit-appearance: none;
  }

  input[type='range']::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--color-primary, #3b82f6);
    cursor: pointer;
  }

  input[type='range']::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--color-primary, #3b82f6);
    cursor: pointer;
    border: none;
  }
</style>
