package com.yaohun.storyline.quest;

import com.io.yaohun.fenjie.event.ResolveItemEvent;
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.DemonLevelReward.Event.LevelRewardReceiveEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class LevelRewardListener implements Listener {

    @EventHandler
    public void onLevelReward(LevelRewardReceiveEvent e) {
        Player player = e.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.LEVEL_REWARD_KEY) {
            String rewardKey = e.getRewardKey();
            if(rewardKey.contains(quest.getTargetName())){
                playerData.addQuestAmount(quest);
            }
        } else if(quest.getQuestType() == QuestType.LEVEL_REWARD_VALUE) {
            int rewardLevel = e.getLevels();
            if(String.valueOf(rewardLevel).equalsIgnoreCase(quest.getTargetName())){
                playerData.addQuestAmount(quest);
            }
        }
    }
}
