package io.github.thatsmusic99.headsplus.inventories.icons.content;

import io.github.thatsmusic99.headsplus.HeadsPlus;
import io.github.thatsmusic99.headsplus.api.HPPlayer;
import io.github.thatsmusic99.headsplus.config.ConfigInventories;
import io.github.thatsmusic99.headsplus.config.HeadsPlusMessagesManager;
import io.github.thatsmusic99.headsplus.inventories.icons.Content;
import io.github.thatsmusic99.headsplus.placeholders.CacheManager;
import io.github.thatsmusic99.headsplus.util.HPUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class Challenge extends Content {

    private io.github.thatsmusic99.headsplus.api.Challenge challenge;
    private String reward;

    public Challenge(io.github.thatsmusic99.headsplus.api.Challenge challenge, Player player) {
        super(challenge.isComplete(player) ? challenge.getCompleteIcon().clone() : challenge.getIcon().clone());
        this.challenge = challenge;
        this.reward = challenge.getReward().getRewardString(player);
        initNameAndLore("challenge", player);
    }

    public Challenge() {
        super();
    }

    @Override
    public boolean onClick(Player player, InventoryClickEvent event) {
        try {
            if (event.isLeftClick()) {
                if (!challenge.isComplete(player)) {
                    challenge.canComplete(player).thenAcceptAsync(result -> {
                        if (result) {
                            challenge.complete(player);
                            item.setType(challenge.getCompleteIcon().getType());
                            initNameAndLore("challenge", player);
                            event.getInventory().setItem(event.getSlot(), item);
                        } else {
                            HeadsPlusMessagesManager.get().sendMessage("commands.challenges.cant-complete-challenge", player);
                        }
                    }, HeadsPlus.sync);
                } else {
                    HeadsPlusMessagesManager.get().sendMessage("commands.challenges.already-complete-challenge", player);
                }
            } else {
                HPPlayer hpPlayer = HPPlayer.getHPPlayer(player);
                if (hpPlayer.hasChallengePinned(challenge)) {
                    hpPlayer.removeChallengePin(challenge).thenAcceptAsync(why -> {
                        initNameAndLore("challenge", player);
                        event.getInventory().setItem(event.getSlot(), item);
                    }, HeadsPlus.sync);
                } else {
                    hpPlayer.addChallengePin(challenge).thenAcceptAsync(why -> {
                        initNameAndLore("challenge", player);
                        event.getInventory().setItem(event.getSlot(), item);
                    }, HeadsPlus.sync);
                }
            }
        }catch (NullPointerException ignored) {
        }
        return false;
    }

    @Override
    public String getId() {
        return "challenge";
    }

    @Override
    public void initNameAndLore(String id, Player player) {
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(HeadsPlusMessagesManager.get().formatMsg(ConfigInventories.get().getString("icons.challenge.display-name")
                .replaceAll("\\{challenge-name}", challenge.getChallengeHeader()), player));
        List<String> lore = new ArrayList<>();
        for (String loreStr : ConfigInventories.get().getStringList("icons.challenge.lore")) {
            if (loreStr.contains("{challenge-lore}")) {
                for (String loreStr2 : challenge.getDescription()) {
                    lore.add(HeadsPlusMessagesManager.get().formatMsg(loreStr2, player));
                }
            } else {
                HPUtils.parseLorePlaceholders(lore, loreStr,
                        new HPUtils.PlaceholderInfo("{completed}",
                                HeadsPlusMessagesManager.get().getString("commands.challenges.challenge-completed", player),
                                challenge.isComplete(player)),
                        new HPUtils.PlaceholderInfo("{pinned}",
                                HeadsPlusMessagesManager.get().getString("inventory.icon.challenge.pinned", player),
                                HPPlayer.getHPPlayer(player).hasChallengePinned(challenge)),
                        new HPUtils.PlaceholderInfo("{reward}", reward, true),
                        new HPUtils.PlaceholderInfo("{challenge-reward}", reward, true));
                // TODO
                if (loreStr.contains("{completed}")) {
                    if (challenge.isComplete(player)) {
                        lore.add(HeadsPlusMessagesManager.get().getString("commands.challenges.challenge-completed", player));
                    }
                } else if (loreStr.contains("{pinned}")) {
                    if (HPPlayer.getHPPlayer(player).hasChallengePinned(challenge)) {
                        lore.add(HeadsPlusMessagesManager.get().getString("inventory.icon.challenge.pinned", player));
                    }
                } else {
                    String str = HeadsPlusMessagesManager.get().formatMsg(
                            HeadsPlusMessagesManager.get().completed(loreStr, player, challenge), player);
                    try {
                        str = str.replace("{reward}", reward)
                                .replace("{challenge-reward}", reward);
                    } catch (NullPointerException ignored) {

                    }
                    str = str.replaceAll("(\\{xp}|\\{challenge-xp})", String.valueOf(challenge.getGainedXP()))
                            .replaceAll("\\{heads}", String.valueOf(
                                    // TODO - use supplier, not this
                                    CacheManager.get().getStat(challenge.getCacheID(), challenge.getStatFuture(player.getUniqueId()))))
                            .replaceAll("\\{total}", String.valueOf(challenge.getRequiredHeadAmount()));
                    lore.add(str);
                }

            }

        }
        meta.setLore(lore);
        item.setItemMeta(meta);
    }
}

@@ -4607,18 +4607,17 @@
 d, true)
-);
+,
 %0A       
@@ -4629,990 +4629,290 @@
     
-// TODO%0A                if (loreStr.contains(%22%7Bcompleted%7D%22)) %7B%0A                    if (challenge.isComplete(player)) %7B%0A                        lore.add(HeadsPlusMessagesManager.get().getString(%22commands.challenges.challenge-completed%22, player));%0A                    %7D%0A                %7D else if (loreStr.contains(%22%7Bpinned%7D%22)) %7B%0A                    if (HPPlayer.getHPPlayer(player).hasChallengePinned(challenge)) %7B%0A                        lore.add(HeadsPlusMessagesManager.get().getString(%22inventory.icon.challenge.pinned%22, player));%0A                    %7D%0A                %7D else %7B%0A                    String str = HeadsPlusMessagesMana
+        new HPUtils.PlaceholderInfo(%22%7Bxp%7D%22, challenge.getGainedXP(), true),%0A                        new HPUtils.PlaceholderInfo(%22%7Bchallenge-xp%7D%22, challen
 ge
-r
 .get
-().formatMsg(%0A                            HeadsPlusMessagesManager.get().completed(loreStr, player, challenge), player);%0A                    try %7B%0A                        str = str.replace(%22%7Breward%7D%22, reward)%0A                                .replace(%22%7Bchallenge-reward%7D%22, reward);%0A                    %7D catch (NullPointerException ignored) %7B%0A
+GainedXP(), true),%0A                        new HPUtils.PlaceholderInfo(%22%7Btotal%7D%22, challenge.getRequiredHeadAmount(), true),
 %0A   
@@ -4932,194 +4932,51 @@
     
-%7D%0A
     
-                str = str.replaceAll(%22(%5C%5C%7Bxp%7D%7C%5C%5C%7Bchallenge-xp%7D)%22, String.valueOf(challenge.getGainedXP()))%0A                            .replaceAll(%22%5C%5C%7Bheads%7D%22, String.valueOf(%0A    
+new HPUtils.PlaceholderInfo(%22%7Bheads%7D%22,%0A
     
@@ -5036,20 +5036,16 @@
 ot this%0A
-    
         
@@ -5169,169 +5169,17 @@
 ()))
-))%0A                            .replaceAll(%22%5C%5C%7Btotal%7D%22
 , 
-S
 tr
-ing.valueOf(challenge.getRequiredHeadAmount()));%0A                    lore.add(str);%0A                %7D%0A
+ue));
 %0A   

Use HPUtil placeholder management