import { ACTOR_TYPES, ITEM_TYPES } from "../constants.js"
import { Misc } from "../misc.js"
import { RdDSheetUtility } from "../rdd-sheet-utility.js"
import { RdDUtility } from "../rdd-utility.js"

/**
 * TODO:
 * options.editable ?
 * 
 */
const _SPACEHOLDER = { placeholder: true }

const _VENDRE = {
  code: 'item-vendre', label: 'Vendre ou donner', icon: 'fa-solid fa-comments-dollar',
  filter: it => Misc.toInt(it.system.quantite) > 0 || it.parent?.type == ACTOR_TYPES.commerce,
  action: (item, actor) => item.proposerVente()
}
const _ACHETER = {
  code: 'item-acheter', label: 'Acheter', icon: 'fa-regular fa-coins',
  filter: it => it.parent?.type == ACTOR_TYPES.commerce,
  allowLimited: true,
  action: (item, actor) => actor.vente(item)
}
const _MONTRER = {
  code: 'item-montrer', label: 'Montrer', icon: 'fa-solid fa-comment',
  allowLimited: true,
  action: (item, actor) => item.postItemToChatCompact()
}
const _SPLIT = {
  code: 'item-split', label: 'Séparer le goupe', icon: 'fa-solid fa-unlink',
  actorFilter: actor => actor.isOwner,
  filter: it => Misc.toInt(it.system.quantite) > 1 && it.parent?.type != ACTOR_TYPES.commerce,
  action: (item, actor) => RdDSheetUtility.splitItem(item, actor)
}

const _EDIT = {
  code: 'item-edit', label: it => it.type == ITEM_TYPES.conteneur ? 'Ouvrir' : 'Editer',
  icon: 'fa-solid fa-edit',
  action: (item, actor) => item.sheet.render(true)
}

const _DELETE = {
  code: 'item-delete', label: 'Supprimer', icon: 'fa-solid fa-trash',
  optionsFilter: options => options.isOwner,
  action: (item, actor) => RdDUtility.confirmActorItemDelete(item, actor)
}
const _EQUIPER = {
  code: 'item-equip', label: 'Equiper', icon: it => it.system.equipe ? 'fa-solid fa-hand-rock' : 'fa-regular fa-hand-paper',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => !it.estContenu && it.isEquipable(),
  action: (item, actor) => actor.equiperObjet(item)
}

const _CUISINER = {
  code: 'item-cuisiner', label: 'Cuisiner',
  img: 'systems/foundryvtt-reve-de-dragon/assets/actions/cuisine.svg',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => it.getUtilisation() == 'cuisine' && it.system.sust > 0,
  action: (item, actor) => actor.preparerNourriture(item)
}
const _MANGER_CRU = {
  code: 'item-manger-cru', label: 'Manger cru', icon: 'fa-solid fa-drumstick-bite',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => it.getUtilisation() == 'cuisine' && it.system.sust > 0,
  action: (item, actor) => actor.mangerNourriture(item)
}
const _MANGER = {
  code: 'item-manger', label: 'Manger', icon: 'fa-solid fa-utensils',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => !(it.system.boisson),
  action: (item, actor) => actor.mangerNourriture(item)
}
const _BOIRE = {
  code: 'item-boire', label: 'Boire', icon: 'fa-solid fa-glass-water',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => it.system.boisson,
  action: (item, actor) => actor.mangerNourriture(item)
}
const _DECOCTION = {
  code: 'item-decoction', label: 'Décoction', icon: 'fa-solid fa-flask-vial',
  actorFilter: actor => actor.isPersonnage(),
  action: (item, actor) => actor.fabriquerDecoctionHerbe(item)
}

const _OUVRIR = {
  code: 'item-edit', label: 'Ouvrir',
  img: 'systems/foundryvtt-reve-de-dragon/assets/actions/ouvrir.svg',
  action: (item, actor) => item.sheet.render(true)
}
const _LIRE = {
  code: 'item-lire', label: 'Lire', icon: 'fa-solid fa-book-open',
  actorFilter: actor => actor.isPersonnage(),
  action: (item, actor) => actor.actionLire(item)
}
const _MARQUER_LU = {
  code: 'item-marquer-lu', label: 'Marquer comme lu',
  icon: it => it.system.lu ? 'fa-solid fa-book-open' : 'fa-regular fa-book',
  actorFilter: actor => actor.isPersonnage(),
  action: async (item, actor) => { await actor.creerCopieLue(item) }
}

const _REFOULER = {
  code: 'item-refouler', label: 'Refouler', icon: 'fa-solid fa-burst',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => it.system.refoulement > 0,
  action: (item, actor) => actor.actionRefoulement(item)
}

const _SORT_RESERVE = {
  code: 'item-sortreserve-add', label: 'Ajouter en réserve', icon: 'fa-solid fa-sparkles',
  actorFilter: actor => actor.isPersonnage(),
  filter: it => game.user.isGM && !it.system.isrituel,
  action: (item, actor) => actor.addSortReserve(item)
}

const _VIDER = {
  code: 'item-vider', label: 'Vider le conteneur', icon: 'fa-solid fa-box-open',
  filter: it => it.isConteneur() && (it.system.contenu?.length > 0),
  actorFilter: actor => actor.isOwner,
  action: (item, actor) => item.update({ 'system.contenu': [] })
}

export const COMMON_ACTIONS = [_EQUIPER]
export const DEFAULT_ACTIONS = [_ACHETER, _SPLIT, _SPACEHOLDER, _VENDRE, _MONTRER, _EDIT, _DELETE]

export const ITEM_ACTIONS = {
  faune: [_CUISINER, _MANGER_CRU],
  ingredient: [_CUISINER, _MANGER_CRU],
  conteneur: [_VIDER, _OUVRIR],
  herbe: [_DECOCTION, _CUISINER, _MANGER_CRU],
  livre: [_LIRE, _MARQUER_LU],
  nourritureboisson: [_MANGER, _BOIRE],
  ombre: [_REFOULER],
  plante: [_CUISINER, _MANGER_CRU],
  queue: [_REFOULER],
  sort: [_SORT_RESERVE]
}

export class ItemAction {

  static applies(action, item, options) {
    return action && item
      && item.isActionAllowed(action.code)
      && (!action.actorFilter || (item.actor && action.actorFilter(item.actor)))
      && (!action.filter || action.filter(item))
      && (action.allowLimited || options.editable)
      && (!action.optionsFilter || action.optionsFilter(options))
  }

  static label(action, item) {
    if (action.placeholder) {
      return ""
    }
    return ItemAction.getParam(action.label, item)
  }

  static img(action, item) {
    if (action.placeholder) {
      return ""
    }

    const img = ItemAction.getParam(action.img, item)
    if (img) {
      return `<img src="${img}" />`
    }
    const icon = ItemAction.getParam(action.icon, item)
    if (icon) {
      return `<i class="${icon}"></i>`
    }
    return action.label
  }

  static getParam(p, item) {
    return typeof (p) == 'function' ? p(item) : p
  }

  static async onActionItem(event, actor, options) {
    const code = $(event.currentTarget).data('code')
    const item = RdDSheetUtility.getItem(event, actor)
    const action = item?.itemActions().find(it => it.code == code)
    if (action && ItemAction.applies(action, item, options)) {
      await action.action(item, actor)
    }
  }
}
