import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
import { Router } from '@angular/router';
import { CharacterService } from '../services/character.service';
import type { Character } from '../services/character.service';


@Component({
  selector: 'app-character',
  templateUrl: './character.page.html',
  styleUrls: ['./character.page.scss'],
  standalone: true,
  imports: [CommonModule, IonicModule]
})
export class CharacterPage implements OnInit {
  characters: Character[] = [];

  constructor(
    private router: Router,
    private characterService: CharacterService
  ) { }

  ngOnInit() {
    this.loadCharacters();
  }

  loadCharacters() {
    this.characterService.characters$.subscribe(chars => {
      this.characters = chars;
    });
  }

  createNewCharacter() {
    this.router.navigate(['/character-creator']);
  }

  navigateToDetail(characterId: string) {
    this.router.navigate(['/character-detail', characterId]); // 确保这里的 characterId 是有效的
  }
}