/* -------------------------------------------- */
import { HtmlUtility } from "./html-utility.js";
import { Misc } from "./misc.js";
import { RdDCombatManager } from "./rdd-combat.js";
import { Targets } from "./targets.js";

/* -------------------------------------------- */
export class RdDTokenHud {

  static init() {
    // Integration du TokenHUD
    Hooks.on('renderTokenHUD', (app, html, token) => { RdDTokenHud.addTokenHudExtensions(app, html, token._id) });
  }

  /* -------------------------------------------- */
  static async removeExtensionHud(app, html, tokenId) {
    html.find('.control-icon.rdd-combat').remove();
    html.find('.control-icon.rdd-initiative').remove();
  }

  /* -------------------------------------------- */
  static async addExtensionHud(app, html, tokenId, isCombat) {

    let token = canvas.tokens.get(tokenId);
    let actor = token.actor;
    app.hasExtension = true;
    // soins
    await RdDTokenHud.addExtensionHudSoins(html, actor);

    if (isCombat) {
      const combatant = game.combat.combatants.find(c => c.tokenId == tokenId)
      const actor = RdDCombatManager.getActorCombatant(combatant, { warning: false })
      if (actor) {
        let actions = RdDCombatManager.listActionsActorCombatant(actor)
        // initiative
        await RdDTokenHud.addExtensionHudInit(html, combatant, actions)
        // combat
        await RdDTokenHud.addExtensionHudCombat(html, combatant, token, actions)
      }
    }


  }

  static async addExtensionHudInit(html, combatant, actions) {
    const hudData = {
      combatant, actions,
      commandes: [
        { name: "Autre action", command: 'autre' },
        { name: 'Initiative +1', command: 'inc', value: 0.01 },
        { name: 'Initiative -1', command: 'dec', value: -0.01 }]
    };
    const controlIconCombat = html.find('.control-icon[data-action=combat]');
    await RdDTokenHud._configureSubMenu(controlIconCombat,
      'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.html',
      hudData,
      (event) => {
        let initCommand = event.currentTarget.attributes['data-command']?.value;
        let combatantId = event.currentTarget.attributes['data-combatant-id']?.value;
        if (initCommand) {
          RdDTokenHud._initiativeCommand(initCommand, combatantId);
        } else {
          let index = event.currentTarget.attributes['data-action-index'].value;
          let action = hudData.actions[index];
          RdDCombatManager.rollInitiativeAction(combatantId, action);
        }
      });
  }

  static async addExtensionHudCombat(html, combatant, token, actions) {
    const hudData = { combatant, token, actions, commandes: [] };
    const controlIconTarget = html.find('.control-icon[data-action=target]');
    await RdDTokenHud._configureSubMenu(controlIconTarget, 'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.html', hudData,
      (event) => {
        const actionIndex = event.currentTarget.attributes['data-action-index']?.value;
        const action = hudData.actions[actionIndex];
        const possession = action.action == 'possession' ? combatant.actor.getPossession(action.system.possessionid) : undefined;
        if (possession) {
          combatant.actor.conjurerPossession(possession);
        }
        else {
          combatant.actor.rollArme(action, 'competence', token)
        }
      });
  }

  static async addExtensionHudSoins(html, sourceActor) {
    const target = Targets.getTarget({ warn: false });
    if (target?.actor) {
      const hudSoins = { blessures: target.actor.blessuresASoigner() ?? [] };
      if (hudSoins.blessures.length > 0) {
        const controlIconTarget = html.find('.control-icon[data-action=combat]');
        await RdDTokenHud._configureSubMenu(controlIconTarget,
          'systems/foundryvtt-reve-de-dragon/templates/hud-actor-soins.hbs',
          hudSoins,
          (event) => {
            const blessureId = event.currentTarget.attributes['data-blessure-id']?.value;
            sourceActor.rollSoins(target.actor, blessureId)
          });
      }
    }
  }

  static _initiativeCommand(initCommand, combatantId) {
    switch (initCommand) {
      case 'inc': return RdDCombatManager.incDecInit(combatantId, 0.01);
      case 'dec': return RdDCombatManager.incDecInit(combatantId, -0.01);
      case 'autre': return RdDCombatManager.rollInitiativeAction(combatantId,
        { name: "Autre action", action: 'autre', system: { initOnly: true, competence: "Autre action" } });
    }
  }

  /* -------------------------------------------- */
  static async addTokenHudExtensions(app, html, tokenId) {
    const controlIconCombat = html.find('.control-icon[data-action=combat]');
    if (controlIconCombat.length > 0) {
      controlIconCombat.click(event => {
        if (event.currentTarget.className.includes('active')) {
          RdDTokenHud.removeExtensionHud(app, html, tokenId);
        } else {
          setTimeout(() => RdDTokenHud.addExtensionHud(app, html, tokenId), 200);
        }
      });

      const isCombat = controlIconCombat[0].className.includes('active');
      RdDTokenHud.addExtensionHud(app, html, tokenId, isCombat);
    }
  }

  /* -------------------------------------------- */
  static async _configureSubMenu(insertionPoint, template, hudData, onMenuItem) {
    const hud = $(await renderTemplate(template, hudData));
    const list = hud.find('div.rdd-hud-list');

    RdDTokenHud._toggleHudListActive(hud, list);

    hud.find('img.rdd-hud-togglebutton').click(event => RdDTokenHud._toggleHudListActive(hud, list));
    list.find('.rdd-hud-menu').click(onMenuItem);

    insertionPoint.after(hud);
  }

  static _toggleHudListActive(hud, list) {
    hud.toggleClass('active');
    HtmlUtility.showControlWhen(list, hud.hasClass('active'));
  }
}