import { RdDBaseActorSheetV2 } from "./base-actor-sheet.mjs"
import { RdDSheetUtility } from "../../rdd-sheet-utility.js"
import { RdDUtility } from "../../rdd-utility.js"
import { DialogSelect } from "../../dialog-select.js"
import { ACTOR_TYPES } from "../../constants.js"

export class RdDActorEntiteSheetV2 extends RdDBaseActorSheetV2 {

  static TYPE = ACTOR_TYPES.entite

  static DEFAULT_OPTIONS = Object.assign({}, RdDBaseActorSheetV2.DEFAULT_OPTIONS, {
    position: { width: 640, height: 720 },
  })

  static PARTS = {
    form: { template: "systems/foundryvtt-reve-de-dragon/templates/actor-entite-sheet.hbs" },
  }

  async _prepareContext() {
    const ctx = await super._prepareContext()
    ctx.niveau = this.actor.getNiveau()
    const { niveau: _, ...caracRest } = ctx.system.carac
    ctx.system = { ...ctx.system, carac: caracRest }
    ctx.resonances = this.actor.system.sante.resonnance.actors
      .map(actorId => game.actors.get(actorId))
      .filter(actor => actor != undefined)
      .map(actor => ({ id: actor.id, name: actor.name, img: actor.img }))
    return ctx
  }

  async _onRender(context, options) {
    await super._onRender(context, options)
    if (!this.isEditable) return

    this.html.find(".creature-carac").change(async event => {
      const compName = event.currentTarget.attributes.compname.value
      await this.actor.updateCreatureCompetence(compName, "carac_value", parseInt(event.target.value))
    })
    this.html.find(".creature-dommages").change(async event => {
      const compName = event.currentTarget.attributes.compname.value
      await this.actor.updateCreatureCompetence(compName, "dommages", parseInt(event.target.value))
    })

    this.html.find(".resonance-add").click(async event =>
      await DialogSelect.select({
        label: "Choisir un acteur à accorder",
        list: game.actors.filter(it => true),
      },
        it => this._resonanceAdd(it.id))
    )

    this.html.find(".resonance-delete").click(async event => {
      const li = RdDSheetUtility.getEventElement(event)
      const actorId = li.data("actor-id")
      if (actorId) {
        const actorResonance = game.actors.get(actorId)
        RdDUtility.confirmSubActeurDelete(this, actorResonance, li, () => {
          this._resonanceDelete(actorId)
          RdDUtility.slideOnDelete(this, li)
        })
      }
    })
  }

  async _onDropActor(event, dragData) {
    const dropActor = fromUuidSync(dragData.uuid)
    await this.actor.setEntiteReveAccordee(dropActor)
    await super._onDropActor(event, dragData)
  }

  async _resonanceAdd(actorId) {
    const newResonances = [...this.actor.system.sante.resonnance.actors, actorId]
    await this.actor.update({ "system.sante.resonnance.actors": newResonances })
  }

  async _resonanceDelete(actorId) {
    console.log("Delete : ", actorId)
    const newResonances = this.actor.system.sante.resonnance.actors.filter(id => id !== actorId)
    await this.actor.update({ "system.sante.resonnance.actors": newResonances }, { render: false })
  }
}
