import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
  output,
  signal,
} from '@angular/core';
import { MissionStateResponse } from '@fog-explorer/api-interfaces';
import { killerAvatarUrl } from '@fog-explorer/encounter-library';
import { avatarUrl, survivorInitials } from './survivor-initials';

/**
 * State → ring colour + shape indicator.
 * Colour alone is never the sole differentiator (accessibility requirement).
 */
const STATE_META: Record<string, { color: string; label: string }> = {
  active:    { color: 'var(--fog-amber)',     label: '◆' },
  injured:   { color: 'var(--fog-amber-dim)', label: '◈' },
  downed:    { color: 'var(--fog-red)',       label: '◇' },
  sacrificed:{ color: 'var(--fog-red)',       label: '✕' },
  idle:      { color: 'var(--fog-text-dim)',  label: '○' },
};

@Component({
  selector: 'app-minimised-panel',
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: './minimised-panel.component.html',
  styleUrl: './minimised-panel.component.css',
})
export class MinimisedPanelComponent {
  missionState = input.required<NonNullable<MissionStateResponse>>();
  lanternClick = output<void>();

  protected stateMeta = computed(() => {
    const state = this.missionState().survivors[0]?.state ?? 'idle';
    return STATE_META[state] ?? STATE_META['idle'];
  });

  protected initials = computed(() => {
    const name = this.missionState().survivors[0]?.name ?? '';
    return name ? survivorInitials(name) : '?';
  });

  protected avatarSrc = computed(() => {
    const name = this.missionState().survivors[0]?.name ?? '';
    return avatarUrl(name);
  });

  protected readonly imgError = signal(false);

  protected killerName = computed(() => this.missionState().mission.killerName ?? null);

  protected killerAvatarSrc = computed(() => {
    const name = this.killerName();
    return name ? killerAvatarUrl(name) : null;
  });

  protected readonly killerImgError = signal(false);

  protected latestLogLine = computed(() => {
    const log = this.missionState().recentLog;
    return log.length ? log[0].logText : null;
  });
}
