<script>
  import { api } from '../lib/api.js';
  
  export let status;

  $: powerForward = status?.power_forward || 0;
  $: powerReflected = status?.power_reflected || 0;
  $: swr = status?.swr || 1.0;
  $: voltage = status?.voltage || 0;
  $: vdd = status?.vdd || 0;
  $: current = status?.current || 0;
  $: peakCurrent = status?.peak_current || 0;
  $: temperature = status?.temperature || 0;
  $: harmonicLoadTemp = status?.harmonic_load_temp || 0;
  $: fanMode = status?.fan_mode || 'CONTEST';
  $: state = status?.state || 'IDLE';
  $: bandA = status?.band_a || '0';
  $: bandB = status?.band_b || '0';
  $: connected = status?.connected || false;
  
  $: displayState = state.replace('TRANSMIT_A', 'TRANSMIT').replace('TRANSMIT_B', 'TRANSMIT');
  
  // Color functions
  $: tempColor = temperature < 40 ? '#4caf50' : temperature < 60 ? '#ffc107' : temperature < 75 ? '#ff9800' : '#f44336';
  $: swrColor = swr < 1.5 ? '#4caf50' : swr < 2.0 ? '#ffc107' : swr < 3.0 ? '#ff9800' : '#f44336';
  $: powerPercent = Math.min((powerForward / 2000) * 100, 100);

  async function setFanMode(mode) {
    try {
      await api.power.setFanMode(mode);
    } catch (err) {
      console.error('Failed to set fan mode:', err);
      alert('Failed to set fan mode');
    }
  }

  async function toggleOperate() {
    try {
      const operateValue = state === 'IDLE' ? 0 : 1;
      await api.power.setOperate(operateValue);
    } catch (err) {
      console.error('Failed to toggle operate:', err);
      alert('Failed to toggle operate mode');
    }
  }
</script>

<div class="card">
  <div class="card-header">
    <h2>Power Genius XL</h2>
    <div class="header-right">
      <button 
        class="state-badge" 
        class:idle={state === 'IDLE'} 
        class:transmit={state.includes('TRANSMIT')}
        on:click={toggleOperate}
      >
        {displayState}
      </button>
      <span class="status-dot" class:disconnected={!connected}></span>
    </div>
  </div>

  <div class="metrics">
    <!-- Power Display - Big and Bold -->
    <div class="power-display">
      <div class="power-main">
        <div class="power-value">{powerForward.toFixed(0)}<span class="unit">W</span></div>
        <div class="power-label">Forward Power</div>
      </div>
      <div class="power-bar">
        <div class="power-bar-fill" style="width: {powerPercent}%">
          <div class="power-bar-glow"></div>
        </div>
        <div class="power-scale">
          <span>0</span>
          <span>1000</span>
          <span>2000</span>
        </div>
      </div>
    </div>

    <!-- SWR Circle Indicator -->
    <div class="swr-container">
      <div class="swr-circle" style="--swr-color: {swrColor}">
        <div class="swr-value">{swr.toFixed(2)}</div>
        <div class="swr-label">SWR</div>
      </div>
      <div class="swr-status">
        {#if swr < 1.5}
          <span class="status-text good">Excellent</span>
        {:else if swr < 2.0}
          <span class="status-text ok">Good</span>
        {:else if swr < 3.0}
          <span class="status-text warning">Caution</span>
        {:else}
          <span class="status-text danger">High!</span>
        {/if}
      </div>
    </div>

    <!-- Temperature Gauges -->
    <div class="temp-group">
      <div class="temp-item">
        <div class="temp-value" style="color: {tempColor}">{temperature.toFixed(1)}°</div>
        <div class="temp-label">PA Temp</div>
        <div class="temp-mini-bar">
          <div class="temp-mini-fill" style="width: {(temperature / 80) * 100}%; background: {tempColor}"></div>
        </div>
      </div>
      <div class="temp-item">
        <div class="temp-value" style="color: {tempColor}">{harmonicLoadTemp.toFixed(1)}°</div>
        <div class="temp-label">HL Temp</div>
        <div class="temp-mini-bar">
          <div class="temp-mini-fill" style="width: {(harmonicLoadTemp / 80) * 100}%; background: {tempColor}"></div>
        </div>
      </div>
    </div>

    <!-- Electrical Parameters -->
    <div class="params-grid">
      <div class="param-box">
        <div class="param-label">VAC</div>
        <div class="param-value">{voltage.toFixed(0)}</div>
      </div>
      <div class="param-box">
        <div class="param-label">VDD</div>
        <div class="param-value">{vdd.toFixed(1)}</div>
      </div>
      <div class="param-box">
        <div class="param-label">ID Peak</div>
        <div class="param-value">{peakCurrent.toFixed(1)}</div>
      </div>
    </div>

    <!-- Band Display -->
    <div class="band-display">
      <div class="band-item">
        <span class="band-label">Band A</span>
        <span class="band-value">{bandA}</span>
      </div>
      <div class="band-item">
        <span class="band-label">Band B</span>
        <span class="band-value">{bandB}</span>
      </div>
    </div>

    <!-- Fan Control -->
    <div class="fan-control">
      <label class="control-label">Fan Mode</label>
      <select value={fanMode} on:change={(e) => setFanMode(e.target.value)}>
        <option value="STANDARD">Standard</option>
        <option value="CONTEST">Contest</option>
        <option value="BROADCAST">Broadcast</option>
      </select>
    </div>
  </div>
</div>

<style>
  .card {
    background: linear-gradient(135deg, #1a2332 0%, #0f1923 100%);
    border: 1px solid #2d3748;
    border-radius: 8px;
    padding: 0;
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: rgba(79, 195, 247, 0.05);
    border-bottom: 1px solid #2d3748;
  }

  h2 {
    font-size: 14px;
    font-weight: 600;
    color: var(--accent-cyan);
    margin: 0;
    letter-spacing: 0.5px;
  }

  .header-right {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .state-badge {
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
  }

  .state-badge.idle {
    background: rgba(76, 175, 80, 0.2);
    color: #4caf50;
  }

  .state-badge.transmit {
    background: rgba(255, 152, 0, 0.2);
    color: #ff9800;
    animation: pulse 1s infinite;
  }

  @keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
  }

  .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #4caf50;
    box-shadow: 0 0 8px #4caf50;
  }

  .status-dot.disconnected {
    background: #f44336;
    box-shadow: 0 0 8px #f44336;
  }

  .metrics {
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  /* Power Display */
  .power-display {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .power-main {
    text-align: center;
  }

  .power-value {
    font-size: 48px;
    font-weight: 200;
    color: var(--accent-cyan);
    line-height: 1;
    text-shadow: 0 0 20px rgba(79, 195, 247, 0.5);
  }

  .power-value .unit {
    font-size: 24px;
    color: var(--text-secondary);
    margin-left: 4px;
  }

  .power-label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-top: 4px;
  }

  .power-bar {
    position: relative;
    height: 8px;
    background: var(--bg-tertiary);
    border-radius: 4px;
    overflow: hidden;
  }

  .power-bar-fill {
    position: relative;
    height: 100%;
    background: linear-gradient(90deg, #4caf50, #ffc107, #ff9800, #f44336);
    border-radius: 4px;
    transition: width 0.3s ease;
  }

  .power-bar-glow {
    position: absolute;
    top: 0;
    right: 0;
    width: 20px;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5));
    animation: shimmer 2s infinite;
  }

  @keyframes shimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
  }

  .power-scale {
    display: flex;
    justify-content: space-between;
    font-size: 9px;
    color: var(--text-muted);
    margin-top: 4px;
  }

  /* SWR Circle */
  .swr-container {
    display: flex;
    align-items: center;
    gap: 16px;
  }

  .swr-circle {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(79, 195, 247, 0.1), transparent);
    border: 3px solid var(--swr-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 20px var(--swr-color);
  }

  .swr-value {
    font-size: 24px;
    font-weight: 300;
    color: var(--swr-color);
  }

  .swr-label {
    font-size: 10px;
    color: var(--text-muted);
    text-transform: uppercase;
  }

  .swr-status {
    flex: 1;
  }

  .status-text {
    font-size: 14px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  .status-text.good { color: #4caf50; }
  .status-text.ok { color: #ffc107; }
  .status-text.warning { color: #ff9800; }
  .status-text.danger { color: #f44336; }

  /* Temperature */
  .temp-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .temp-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 12px;
    background: var(--bg-tertiary);
    border-radius: 6px;
  }

  .temp-value {
    font-size: 32px;
    font-weight: 300;
    line-height: 1;
  }

  .temp-label {
    font-size: 10px;
    color: var(--text-muted);
    text-transform: uppercase;
  }

  .temp-mini-bar {
    height: 4px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
    overflow: hidden;
    margin-top: 4px;
  }

  .temp-mini-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.3s ease;
  }

  /* Parameters Grid */
  .params-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .param-box {
    padding: 8px;
    background: var(--bg-tertiary);
    border-radius: 4px;
    text-align: center;
  }

  .param-label {
    font-size: 9px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  .param-value {
    font-size: 18px;
    font-weight: 300;
    color: var(--text-primary);
    margin-top: 2px;
  }

  /* Band Display */
  .band-display {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 8px;
    background: rgba(79, 195, 247, 0.05);
    border-radius: 6px;
  }

  .band-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .band-label {
    font-size: 11px;
    color: var(--text-muted);
  }

  .band-value {
    font-size: 14px;
    font-weight: 600;
    color: var(--accent-cyan);
  }

  /* Fan Control */
  .fan-control {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .control-label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  select {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 8px;
    font-size: 12px;
    cursor: pointer;
    outline: none;
    transition: all 0.2s;
  }

  select:hover {
    border-color: var(--accent-cyan);
  }

  select:focus {
    border-color: var(--accent-cyan);
    box-shadow: 0 0 0 2px rgba(79, 195, 247, 0.2);
  }
</style>