import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, of } from 'rxjs';

export interface Character {
  id: string;
  name: string;
  gender: string;
  appearance: string;
  personality: string;
  avatar: string;
}

@Injectable({
  providedIn: 'root'
})
export class CharacterService {
  private characters = new BehaviorSubject<Character[]>([]);
  characters$ = this.characters.asObservable();
  createCharacterAgent(character: Character): Observable<Character> {
    const currentCharacters = this.characters.value;
    this.characters.next([...currentCharacters, character]);
    return of(character);
  }

  getCharacterById(id: string): Observable<Character | undefined> {
    console.log('查找角色 ID:', id); // 调试信息
    console.log('当前角色列表:', this.characters.value); // 调试信息
    return new Observable(observer => {
        const character = this.characters.value.find(c => c.id === id);
        observer.next(character);
        observer.complete();
    });
}
  
}