import { RFRPUtility } from "../../rfrp-utility.js";

// Our Item Sheet extends the default
export default class RMSSSkillSheet extends ItemSheet {

  // Set the height and width
  static get defaultOptions() {
    return foundry.utils.mergeObject(super.defaultOptions, {
      width: 530,
      height: 440,
      template: "systems/fvtt-rolemaster-frp/templates/sheets/skills/rmss-skill-sheet.html",
      classes: ["rmss", "sheet", "item"]
    });
  }

  // If our sheet is called here it is.
  get template() {
    return "systems/fvtt-rolemaster-frp/templates/sheets/skills/rmss-skill-sheet.html";
  }

  // Make the data available to the sheet template
  async getData() {
    const baseData = await super.getData();

    let enrichedDescription = await TextEditor.enrichHTML(this.item.system.description, { async: true });

    // Get a list of the parent item's skill categories for the dropdown
    let ownedSkillCategories = this.prepareSkillCategoryValues();

    // Figure out if a valid Skill Category is already selected
    let selectedSkillCategory = this.prepareSelectedSkillCategory(ownedSkillCategories, this.object.system.category);

    let sheetData = {
      owner: this.item.isOwner,
      editable: this.isEditable,
      item: baseData.item,
      system: baseData.item.system,
      config: CONFIG.rmss,
      owned_skillcats: ownedSkillCategories,
      enrichedDescription: enrichedDescription,
      selected_skillcat: selectedSkillCategory,
      designations: CONFIG.rmss.skill_designations
    };

    return sheetData;
  }

  activateListeners(html) {
    super.activateListeners(html);

    // Catch the event when the user clicks one of the New Ranks Checkboxes in a Skill.
    // It will increment by one or wrap back to zero on a value of three
    html.find(".skillsheet-newrank").click(ev => {
      switch (ev.currentTarget.getAttribute("value")) {
        case "0":
          this.object.update({ system: { new_ranks: { value: 1 } } });
          break;
        case "1":
          this.object.update({ system: { new_ranks: { value: 2 } } });
          break;
        case "2":
          this.object.update({ system: { new_ranks: { value: 3 } } });
          break;
        case "3":
          this.object.update({ system: { new_ranks: { value: 0 } } });
          break;
      }
    });
  }

  // Skills are related to Skill Categories so we need something to allow the user to choose that relationship
  // If this Skill is owned then we will return a list of Skill Categories and allow them to choose
  // Otherwise we'll just return 'Skill has no owner'
  prepareSkillCategoryValues() {
    let skillCategories = RFRPUtility.getSkillCategories();
    if (this.item.isEmbedded) {
      skillCategories = this.item.parent.items.filter(it => it.type == "skill_category");
    }
    //console.log("CATEG", skillCategories);
    return (skillCategories);
  }

  // Determine which Skill Category is selected and test that it is in the current list of categories.
  // If it isn't set it to None.
  prepareSelectedSkillCategory(ownedSkillCategories, selectedSkillCategory) {
    let defaultSelectedCategory = "None";
    let skillC = ownedSkillCategories.find(it => it.name.toLowerCase() == selectedSkillCategory.toLowerCase());
    if (skillC) {
      return (selectedSkillCategory);
    } else {
      return (defaultSelectedCategory);
    }
  }

  // Populate the Skill Category Bonus field on the Skill Sheet.
  // Iterate through the owned skill categories and if one of them matches the item id of currently
  // selected skill category then set the Skill Category Bonus field to the Total Bonus field of the Skill Category
  prepareSelectedSkillCategoryBonus(selected_skillcat) {
    let skillC = this.parent?.items || RFRPUtility.getSkillCategories();
    if (skillC) {
      let item = skillC.find(it => it.type == "skill_category" && it.name.toLowerCase() == itemData.system.category.toLowerCase());
      if (item) {
        this.system.category_bonus = item.system.total_bonus;
        return
      }
    }
    ui.notifications.warn("No Skill Categories found for " + this.name + ". Please create and link a Skill Category.");
  }
}
