package com.yaohun.storyline.quest;

import com.yaohun.storyline.Storyline;
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 com.yaohun.storyline.util.QuestType;
import me.Demon.DemonLevels.event.ShareExpEvent;
import me.Demon.DemonLevels.event.UpLevelEvant;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.Set;

public class LevelExpListener implements Listener {

    @EventHandler
    public void onTeamExp(UpLevelEvant evant) {
        Player player = evant.getPlayer();
        // 获取玩家名称
        String playerName = player.getName();
        // 通过你的插件系统获取该玩家的任务数据管理器
        PlayerManager playerManager = Storyline.getPlayerManager();
        PlayerData playerData = playerManager.getPlayerData(playerName);
        if(playerData == null) return;
        // 玩家若已完成所有任务则直接返回
        if(playerData.isAllCompleted()){
            return;
        }
        // 获取玩家当前任务
        Quest quest = QuestManager.getQuest(playerData.getCurrentQuest());
        if(quest == null){return;}
        if(quest.getQuestType() == QuestType.PLAYER_LEVEL_UP) {
            if(evant.getLevel() >= quest.getTargetAmount()) {
                playerData.forcCompleteQuest(quest);
            }
        }
    }

    @EventHandler
    public void onTeamExp(ShareExpEvent event) {
        Player player = event.getPlayer();
        // 获取玩家名称
        String playerName = player.getName();

        // 通过你的插件系统获取该玩家的任务数据管理器
        PlayerManager playerManager = Storyline.getPlayerManager();
        PlayerData playerData = playerManager.getPlayerData(playerName);

        if(playerData == null) return;

        // 玩家若已完成所有任务则直接返回
        if(playerData.isAllCompleted()){
            return;
        }

        // 获取玩家当前任务
        Quest quest = QuestManager.getQuest(playerData.getCurrentQuest());
        if(quest == null){return;}
        if (quest.getQuestType() == QuestType.PLAYER_TEAM_EXP) {
            // 增加该任务的完成进度
            playerData.addQuestAmount(quest);
        }
    }
}
