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

@Injectable({
  providedIn: 'root'
})
export class DataService {
  private cardsSource = new BehaviorSubject<any[]>([]);
  currentCards = this.cardsSource.asObservable();

  constructor() { }

  // 更新所有卡片
  updateCards(cards: any[]) {
    this.cardsSource.next(cards);
  }

  // 添加新卡片
  addCard(newCard: any) {
    const currentCards = this.cardsSource.value;
    this.cardsSource.next([...currentCards, newCard]);
  }

  // 删除指定卡片
  removeCard(cardId: string) {
    const updatedCards = this.cardsSource.value.filter(card => card.id !== cardId);
    this.cardsSource.next(updatedCards);
  }

  // 更新指定卡片
  updateCard(updatedCard: any) {
    const cards = this.cardsSource.value.map(card =>
      card.id === updatedCard.id ? { ...card, ...updatedCard } : card
    );
    this.cardsSource.next(cards);
  }

  // 获取当前卡片快照
  getCurrentCardsSnapshot(): any[] {
    return this.cardsSource.value;
  }
}
