package com.avairebot.orion.chat;

import com.avairebot.orion.contracts.chat.Restable;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.MessageEmbed;

import java.awt.*;
import java.time.Instant;

public class PlaceholderMessage extends Restable {

    private EmbedBuilder builder;
    private String message;

    public PlaceholderMessage(MessageChannel channel, EmbedBuilder builder, String message) {
        super(channel);

        this.builder = builder;
        this.message = message;
    }

    public PlaceholderMessage(EmbedBuilder builder, String message) {
        super(null);

        this.builder = builder;
        this.message = message;
    }

    public PlaceholderMessage set(String placeholder, String value) {
        message = message.replaceAll(":" + placeholder, value);
        return this;
    }

    public PlaceholderMessage set(String placeholder, Object value) {
        return set(placeholder, value.toString());
    }

    public PlaceholderMessage setTitle(String title, String url) {
        builder.setTitle(title, url);
        return this;
    }

    public PlaceholderMessage setTitle(String title) {
        return setTitle(title, null);
    }

    public PlaceholderMessage setFooter(String text, String iconUrl) {
        builder.setFooter(text, iconUrl);
        return this;
    }

    public PlaceholderMessage setFooter(String text) {
        return setFooter(text, null);
    }

    public PlaceholderMessage setThumbnail(String thumbnail) {
        builder.setThumbnail(thumbnail);
        return this;
    }

    public PlaceholderMessage setImage(String image) {
        builder.setImage(image);
        return this;
    }

    public PlaceholderMessage setTimestamp(Instant timestamp) {
        builder.setTimestamp(timestamp);
        return this;
    }

    public PlaceholderMessage setAuthor(String name, String url, String iconUrl) {
        builder.setAuthor(name, url, iconUrl);
        return this;
    }

    public PlaceholderMessage setAuthor(String name, String url) {
        builder.setAuthor(name, url);
        return this;
    }

    public PlaceholderMessage setAuthor(String name) {
        builder.setAuthor(name);
        return this;
    }

    public PlaceholderMessage setDescription(String description) {
        message = description;
        return this;
    }

    public PlaceholderMessage setColor(Color color) {
        builder.setColor(color);
        return this;
    }

    public PlaceholderMessage addField(String name, String value, boolean inline) {
        builder.addField(name, value, inline);
        return this;
    }

    public PlaceholderMessage addField(MessageEmbed.Field field) {
        builder.addField(field);
        return this;
    }

    public EmbedBuilder build() {
        return builder.setDescription(message);
    }

    @Override
    public MessageEmbed buildEmbed() {
        return builder.setDescription(message).build();
    }

    @Override
    public String toString() {
        return message;
    }
}

@@ -276,59 +276,229 @@
 nt;%0A
-%0Apublic class PlaceholderMessage extends Restable %7B
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0Apublic class PlaceholderMessage extends Restable %7B%0A%0A    private final Map%3CString, String%3E placeholders = new HashMap%3C%3E();
 %0A%0A  
@@ -993,43 +993,25 @@
     
-message = message.replaceAll(%22:%22 + 
+placeholders.put(
 plac
@@ -3045,23 +3045,31 @@
 ription(
-m
+formatM
 essage
+()
 );%0A    %7D
@@ -3161,23 +3161,31 @@
 ription(
-m
+formatM
 essage
+()
 ).build(
@@ -3235,24 +3235,397 @@
 oString() %7B%0A
+        return formatMessage();%0A    %7D%0A%0A    private String formatMessage() %7B%0A        if (placeholders.isEmpty()) %7B%0A            return message;%0A        %7D%0A%0A        List%3CString%3E keys = new ArrayList%3C%3E(placeholders.keySet());%0A        keys.sort((o1, o2) -%3E o2.length() - o1.length());%0A        keys.forEach(key -%3E message = message.replaceAll(%22:%22 + key, placeholders.get(key)));%0A%0A
         retu

Fix custom placeholders being sorted correctly