package com.yaohun.storyline;

import com.yaohun.storyline.config.Config;
import com.yaohun.storyline.core.Quest;
import com.yaohun.storyline.data.PlayerData;
import com.yaohun.storyline.manage.PlayerManager;
import com.yaohun.storyline.manage.QuestManager;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.List;

/**
 * @Author: Baka
 * @Date: 2019/12/23 13:20
 */
public class PapiExpansion extends PlaceholderExpansion {
    private Plugin plugin;

    public PapiExpansion(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean persist() {
        return true;
    }

    @Override
    public boolean canRegister() {
        return true;
    }

    @Override
    public String getAuthor() {
        return plugin.getDescription().getAuthors().toString();
    }

    @Override
    public String getIdentifier() {
        return "quests";
    }

    @Override
    public String getVersion() {
        return plugin.getDescription().getVersion();
    }

    @Override
    public String onPlaceholderRequest(Player player, String identifier) {
        String playerName = player.getName();
        PlayerManager playerManager = Storyline.getPlayerManager();
        PlayerData playerData = playerManager.getPlayerData(playerName);
        if (identifier.equalsIgnoreCase("stats")) {
            if(playerData == null){
                return "1500";
            }
            if(playerData.isAllCompleted()){
                return "1500";
            } else {
                return "0";
            }
        } else if (identifier.equalsIgnoreCase("size")) {
            if(playerData == null){
                return "0";
            }
            return String.valueOf(playerData.getAllQuest().size());
        } else if (identifier.equalsIgnoreCase("current")) {
            if(playerData == null){
                return "";
            }
            int questID = playerData.getCurrentQuest();
            Quest quest = QuestManager.getQuest(questID);
            if(quest == null){
                return "";
            }
            return String.valueOf(playerData.getCurrentQuest());
        } else if (identifier.equalsIgnoreCase("world")) {
            if(playerData == null){
                return "";
            }
            int questID = playerData.getCurrentQuest();
            Quest quest = QuestManager.getQuest(questID);
            if(quest == null){
                return "";
            }
            return Config.getWorldAlias(quest.getStartingPoint().getWorld().getName());
        } else if (identifier.equalsIgnoreCase("name")) {
            if(playerData == null){
                return "";
            }
            int questID = playerData.getCurrentQuest();
            Quest quest = QuestManager.getQuest(questID);
            if(quest == null){
                return "";
            }
            return quest.getDisplayName();
        } else if (identifier.equalsIgnoreCase("key")) {
            if(playerData == null){
                return "";
            }
            int questID = playerData.getCurrentQuest();
            Quest quest = QuestManager.getQuest(questID);
            if(quest == null){
                return "";
            }
            int now = playerData.getQuestAmount(quest);
            return quest.getDescription().replace("{a}", String.valueOf(now)).replace("{need}",String.valueOf(quest.getTargetAmount()));
        } else if (identifier.equalsIgnoreCase("type")) {
            if(playerData == null){
                return "";
            }
            int questID = playerData.getCurrentQuest();
            Quest quest = QuestManager.getQuest(questID);
            if(quest == null){
                return "";
            }
            return Config.getQuestTypeAlias(quest.getQuestType().name());
        }
        return "";
    }
}