export class RecipeEditor {
  constructor(handlers) {
    this.handlers = handlers;
  }

  async handleAction(action, event, actionEl) {
    switch (action) {
      case "add-ingredient":
        this.handlers.addIngredient();
        return true;
      case "toggle-unloading-link":
        this.handlers.toggleUnloadingLink();
        return true;
      case "add-unloading-group":
        this.handlers.addUnloadingGroup();
        return true;
      case "show-recipe-edit-off":
        this.handlers.showRecipeEdit(false);
        return true;
      case "print-all-recipes":
        this.handlers.printAllRecipesInPeriod();
        return true;
      case "print-recipe":
        this.handlers.printRecipe();
        return true;
      case "save-and-exit":
        await this.handlers.saveRecipe(null, true);
        return true;
      case "move-group-up":
        this.handlers.moveGroupUp(actionEl);
        return true;
      case "move-group-down":
        this.handlers.moveGroupDown(actionEl);
        return true;
      case "remove-unloading-group":
        this.handlers.removeUnloadingGroup(actionEl);
        return true;
      case "move-ingredient-up":
        this.handlers.moveIngredientUp(actionEl);
        return true;
      case "move-ingredient-down":
        this.handlers.moveIngredientDown(actionEl);
        return true;
      case "remove-ingredient":
        this.handlers.removeIngredient(actionEl);
        return true;
      case "open-ingredient-sheet": {
        const row = actionEl.closest(".ingredient-row");
        this.handlers.openIngredientMobileSheet(row);
        return true;
      }
      case "close-ingredient-sheet":
        this.handlers.closeIngredientMobileSheet();
        return true;
      case "remove-ingredient-sheet":
        this.handlers.removeIngredientFromSheet();
        return true;
      case "open-unloading-group-sheet": {
        const row = actionEl.closest(".unloading-group-row");
        this.handlers.openUnloadingGroupMobileSheet(row);
        return true;
      }
      case "close-unloading-group-sheet":
        this.handlers.closeUnloadingGroupMobileSheet();
        return true;
      default:
        return false;
    }
  }

  handleChange(action, actionEl) {
    switch (action) {
      case "group-type-change":
        this.handlers.handleGroupTypeChange(actionEl);
        return true;
      case "group-value-change":
        this.handlers.validateGroupValue(actionEl);
        return true;
      case "ingredient-change":
        this.handlers.handleIngredientChange(actionEl);
        return true;
      case "dry-matter-percent-change":
        this.handlers.handleDryMatterPercentChange(actionEl);
        return true;
      case "recalculate-weights":
        this.handlers.recalculateWeights();
        return true;
      case "recalculate-total-weight":
        this.handlers.recalculateFromTotalWeight(actionEl);
        return true;
      default:
        return false;
    }
  }
}
