import { SYSTEM_RDD } from "./constants.js";

const rddStatusEffects = [
  { rdd: true, id: 'stun', label: 'EFFECT.StatusStunned', icon: 'icons/svg/stoned.svg', "duration.rounds": 1 },
  { rdd: true, id: 'bleeding', label: 'EFFECT.StatusBleeding', icon: 'icons/svg/blood.svg' },
  { rdd: true, id: 'prone', label: 'EFFECT.StatusProne', icon: 'icons/svg/falling.svg' },
  { rdd: true, id: 'grappling', tint: '#33cc33', label: 'EFFECT.StatusGrappling', icon: 'systems/foundryvtt-reve-de-dragon/icons/competence_corps_a_corps.webp' },
  { rdd: true, id: 'grappled', tint: '#ff9900', label: 'EFFECT.StatusGrappled', icon: 'systems/foundryvtt-reve-de-dragon/icons/competence_corps_a_corps.webp' },
  { rdd: true, id: 'restrain', label: 'EFFECT.StatusRestrained', icon: 'icons/svg/net.svg' },
  { rdd: true, id: 'unconscious', label: 'EFFECT.StatusUnconscious', icon: 'icons/svg/unconscious.svg' },
  { rdd: true, id: 'blind', label: 'EFFECT.StatusBlind', icon: 'icons/svg/blind.svg' },
  { rdd: true, id: 'comma', label: 'EFFECT.StatusComma', icon: 'icons/svg/skull.svg' },
  { rdd: true, id: 'dead', label: 'EFFECT.StatusDead', icon: 'icons/svg/skull.svg' },
  { rdd: true, id: 'demi-reve', label: 'EFFECT.StatusDemiReve', icon: 'systems/foundryvtt-reve-de-dragon/icons/heures/hd12.svg' }
];
const demiReveStatusEffect = rddStatusEffects.find(it => it.label == 'EFFECT.StatusDemiReve');

const statusDemiSurprise = new Set(['EFFECT.StatusStunned', 'EFFECT.StatusProne', 'EFFECT.StatusRestrain']);
const statusSurpriseTotale = new Set(['EFFECT.StatusUnconscious', 'EFFECT.StatusBlind', 'EFFECT.StatusComma']);

export class StatusEffects {
  static onReady() {
    const rddStatusIds = rddStatusEffects.map(it => it.id);
    const defaultStatusEffectIds = CONFIG.statusEffects.map(it => it.id);
    game.settings.register(SYSTEM_RDD, "use-status-effects", {
      name: "use-status-effects",
      scope: "world",
      config: false,
      default: defaultStatusEffectIds.join(),
      type: String
    });

    game.settings.registerMenu(SYSTEM_RDD, "select-status-effect", {
      name: "Choisir les effets disponibles",
      label: "Choix des effets",
      hint: "Ouvre la fenêtre de sélection des effets/status appliqués aux acteurs",
      icon: "fas fa-bars",
      type: StatusEffectsSettings,
      restricted: true
    });

    CONFIG.RDD.allEffects = rddStatusEffects.concat(CONFIG.statusEffects.filter(it => !rddStatusIds.includes(it.id)));

    StatusEffects._setUseStatusEffects(StatusEffects._getUseStatusEffects());
    console.log('statusEffects', CONFIG.statusEffects);
  }

  static valeurSurprise(effect, isCombat) {
    // const id = StatusEffects.statusId(effect);
    if (statusSurpriseTotale.has(effect.label)) {
      return 2;
    }
    return statusDemiSurprise.has(effect.label) || (isCombat && effect.label == demiReveStatusEffect.label) ? 1 : 0;
  }

  static setMandatoryRdd() {
    CONFIG.statusEffects.filter(it => statusDemiSurprise.has(it.id) || statusSurpriseTotale.has(it.id))
      .forEach(it => it.rdd = true);
  }

  static _getUseStatusEffects() {
    const setting = game.settings.get(SYSTEM_RDD, "use-status-effects");
    return setting ? new Set(setting.split(',')) : new Set();
  }

  static _setUseStatusEffects(useStatusEffects) {
    if (game.user.isGM) {
      game.settings.set(SYSTEM_RDD, "use-status-effects", StatusEffects._toSetting(useStatusEffects));
    }

    for (let effect of CONFIG.RDD.allEffects) {
      effect.active = effect.rdd || useStatusEffects.has(effect.id);
    }
    CONFIG.statusEffects = CONFIG.RDD.allEffects.filter(it => it.active);
  }

  static _toSetting(useStatusEffects) {
    return Array.from(useStatusEffects).join();
  }

  static status(label) {
    return rddStatusEffects.find(it => it.label == label) ?? { label: label };
  }
  static demiReve() {
    return demiReveStatusEffect;
  }
}

class StatusEffectsSettings extends FormApplication {
  constructor(...args) {
    super(...args);
  }

  static get defaultOptions() {
    const options = super.defaultOptions;
    mergeObject(options, {
      id: "status-effects-settings",
      template: "systems/foundryvtt-reve-de-dragon/templates/status-effects-settings.html",
      height: "800",
      width: 350,
      minimizable: false,
      closeOnSubmit: true,
      title: "Choix des status/effets"
    });
    return options;
  }

  getData() {
    let formData = super.getData();
    formData.effects = CONFIG.RDD.allEffects;
    return formData;
  }

  activateListeners(html) {
    html.find(".select-effect").click((event) => {
      let id = event.currentTarget.attributes.name?.value;
      if (id) {
        let selected = StatusEffects._getUseStatusEffects();
        let isChecked = event.currentTarget.checked;
        if (isChecked) {
          selected.add(id);
        }
        else {
          selected.delete(id);
        }
        StatusEffects._setUseStatusEffects(selected);
      }
    });
  }

  async _updateObject(event, formData) {
    this.close();
  }
}

