import { PegasusUtility } from "./pegasus-utility.js";
import { PegasusActor } from "./pegasus-actor.js";
import { PegasusActorSheet } from "./pegasus-actor-sheet.js";

export class PegasusActorCreate {

  /* -------------------------------------------- */
  async start() {
    this.actor = await Actor.create({
      name: "New Actor",
      type: "character"
    });
    this.actor.sheet.render(true);

    const racesPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.race");
    this.races = racesPack.map(i => i.toObject());
    const rolesPack = await PegasusUtility.loadCompendium("fvtt-pegasus-rpg.role");
    this.roles = rolesPack.map(i => i.toObject());

    this.showRaces()
  }

  /* -------------------------------------------- */
  createFormData(step) {
    let formData = {
      name: this.actor.name,
      img: this.actor.img,
      step: step,
      races: this.races,
      roles: this.roles
    }
    return formData;
  }

  /* -------------------------------------------- */
  processChatEvent( event ) {
    const step = $(event.currentTarget).data("step-name");
    const itemId = $(event.currentTarget).data("item-id");
    console.log("Create chat evet", event, itemId, step)
    if ( step == "select-race") {
      let race = this.races.find( item => item._id == itemId);
      this.actor.applyRace( race)
    }
  }

  /* -------------------------------------------- */
  async showRaces() {
    let formData = this.createFormData("select-race")
    let chatData = {
      user: game.user.id,
      alias : this.actor.name,
      rollMode: game.settings.get("core", "rollMode"),
      whisper: [game.user.id].concat( ChatMessage.getWhisperRecipients('GM') ),
      content: await renderTemplate('systems/fvtt-pegasus-rpg/templates/chat-create-actor.html', formData)
    };  
    //console.log("Apply damage chat", chatData );
    await ChatMessage.create( chatData );
  }

  /* -------------------------------------------- */
  async showRoles() {
    console.log("Available roles", roles);

    // TODO : display buttons
  }
}
