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 org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

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

public class FenJieListener implements Listener {

    @EventHandler
    public void onHhFenJie(ResolveItemEvent 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.FENJIE) {
            String FjName = e.getSchemeKey();
            if (FjName.contains(quest.getTargetName())) {
                for (int i = 0; i < e.getStack().getAmount();i++) {
                    playerData.addQuestAmount(quest);
                }
            }
        }
    }
}
