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: it => 'fa-solid fa-comments-dollar',
  filter: it => Misc.toInt(it.system.quantite) > 0,
  optionsFilter: options => options.editable,
  action: (item, actor) => item.proposerVente()
}
const _ACHAT_SERVICE = {
  code: 'item-service-acheter', label: 'Acheter', icon: it => 'fa-regular fa-coins',
  //filter: it => Misc.toInt(it.system.quantite) > 0,
  //optionsFilter: options => options.editable,
  //action: (item, actor) => item.proposerVente()
}
const _MONTRER = {
  code: 'item-montrer', label: 'Montrer', icon: it => 'fa-solid fa-comment',
  action: (item, actor) => item.postItemToChat()
}
const _SPLIT = {
  code: 'item-split', label: 'Séparer le goupe', icon: it => 'fa-solid fa-unlink',
  filter: it => Misc.toInt(it.system.quantite) > 1,
  action: (item, actor) => RdDSheetUtility.splitItem(item, actor)
}

const _EDIT = {
  code: 'item-edit', label: 'Editer', icon: it => 'fa-solid fa-edit',
  action: (item, actor) => item.sheet.render(true)
}
const _DELETE = {
  code: 'item-delete', label: 'Supprimer', icon: it => 'fa-solid fa-trash',
  optionsFilter: options => options.editable && 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',
  filter: it => !it.estContenu && it.isEquipable(),
  action: (item, actor) => actor.equiperObjet(item)
}

const _CUISINER = {
  code: 'item-cuisiner', label: 'Cuisiner', icon: it => 'fa-solid fa-utensils',
  filter: it => it.getUtilisation() == 'cuisine' && it.system.sust > 0,
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.preparerNourriture(item)
}
const _MANGER_CRU = {
  code: 'item-manger-cru', label: 'Manger cru', icon: it => 'fa-solid fa-drumstick-bite',
  filter: it => it.getUtilisation() == 'cuisine' && it.system.sust > 0,
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.mangerNourriture(item)
}
const _MANGER = {
  code: 'item-manger', label: 'Manger', icon: it => 'fa-solid fa-utensils',
  filter: it => !(it.system.boisson),
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.mangerNourriture(item)
}
const _BOIRE = {
  code: 'item-boire', label: 'Boire', icon: it => 'fa-solid fa-glass-water',
  filter: it => it.system.boisson,
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.mangerNourriture(item)
}
const _DECOCTION = {
  code: 'item-decoction', label: 'Décoction', icon: it => 'fa-solid fa-flask-vial',
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.fabriquerDecoctionHerbe(item)
}
const _OUVRIR = {
  code: 'item-edit', label: 'Ouvrir', icon: it => 'fa-solid fa-eye',
  action: (item, actor) => item.sheet.render(true)
}

const _LIRE = {
  code: 'item-lire', label: 'Lire', icon: it => 'fa-solid fa-book-open',
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.actionLire(item)
}

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

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

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

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


export class ItemAction {

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

  static icon(action, item) {
    if (action && action.icon) {
      return action.icon(item)
    }
    return undefined
  }

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