package net.knarcraft.blacksmith.manager;

public enum Message {

    VALUE_CHANGED("&7{setting} set to &6{newValue}"),
    VALUE_FOR_ITEM_CHANGED("&7{setting} for {itemType} {item} set to &6{newValue}"),
    CURRENT_VALUE("&7Current value of {setting}:&r {currentValue}"),
    CURRENT_VALUE_FOR_ITEM("&7Current value of {setting} for {itemType} {item}:&r {currentValue}");

    private final String messageString;

    /**
     * Instantiates a new message
     *
     * @param messageString <p>The string value of this message</p>
     */
    Message(String messageString) {
        this.messageString = messageString;
    }

    /**
     * Gets the message to display when a value has been changed
     *
     * @param setting  <p>The setting whose value has been changed</p>
     * @param newValue <p>The new value of the setting</p>
     * @return <p>The string to display to a user</p>
     */
    public static String getValueChangedMessage(String setting, String newValue) {
        return replacePlaceholders(VALUE_CHANGED.messageString, new String[]{"{setting}", "{newValue}"},
                new String[]{setting, newValue});
    }

    /**
     * Gets the message to display when a value has been changed for an item
     *
     * @param setting  <p>The setting whose value has been changed</p>
     * @param itemType <p>The type of item changed ("material" or "enchantment")</p>
     * @param item     <p>The item the setting was changed for (a material or an enchantment name)</p>
     * @param newValue <p>The new value of the setting</p>
     * @return <p>The string to display to a user</p>
     */
    public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) {
        return replacePlaceholders(VALUE_FOR_ITEM_CHANGED.messageString, new String[]{"{setting}", "{itemType}",
                "{item}", "{newValue}"}, new String[]{setting, itemType.getItemTypeName(), item, newValue});
    }

    /**
     * Gets the message to display when displaying a setting's current value
     *
     * @param setting      <p>The setting whose value is shown</p>
     * @param currentValue <p>The current value of the setting</p>
     * @return <p>The string to display to a user</p>
     */
    public static String getCurrentValueMessage(String setting, String currentValue) {
        return replacePlaceholders(CURRENT_VALUE.messageString, new String[]{"{setting}", "{currentValue}"},
                new String[]{setting, currentValue});
    }

    /**
     * Gets the message to display when displaying a setting's current value for an item
     *
     * @param setting      <p>The setting whose value is shown</p>
     * @param itemType     <p>The type of item shown ("material" or "enchantment")</p>
     * @param item         <p>The item the setting is shown for (a material or an enchantment name)</p>
     * @param currentValue <p>The current value of the setting</p>
     * @return <p>The string to display to a user</p>
     */
    public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) {
        return replacePlaceholders(CURRENT_VALUE_FOR_ITEM.messageString, new String[]{"{setting}", "{itemType}",
                "{item}", "{currentValue}"}, new String[]{setting, itemType.getItemTypeName(), item, currentValue});
    }

    /**
     * Replaces a placeholder in a string
     *
     * @param input       <p>The input string to replace in</p>
     * @param placeholder <p>The placeholder to replace</p>
     * @param replacement <p>The replacement value</p>
     * @return <p>The input string with the placeholder replaced</p>
     */
    private static String replacePlaceholder(String input, String placeholder, String replacement) {
        return input.replace(placeholder, replacement);
    }

    /**
     * Replaces placeholders in a string
     *
     * @param input        <p>The input string to replace in</p>
     * @param placeholders <p>The placeholders to replace</p>
     * @param replacements <p>The replacement values</p>
     * @return <p>The input string with placeholders replaced</p>
     */
    private static String replacePlaceholders(String input, String[] placeholders, String[] replacements) {
        for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) {
            input = replacePlaceholder(input, placeholders[i], replacements[i]);
        }
        return input;
    }

}
