package com.yaohun.storyline.quest;

import com.yaohun.itemconvert.event.ItemConvertEvent;
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 org.bukkit.event.player.PlayerItemConsumeEvent;

public class ItemConvertListener implements Listener {


    @EventHandler
    public void onItemConvert(ItemConvertEvent 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.CONVER_FILE) {
            String file_Name = quest.getTargetName();
            String type = event.getShopKey();
            if(file_Name.equalsIgnoreCase(type)){
                playerData.addQuestAmount(quest);
            }
        } else if (quest.getQuestType() == QuestType.CONVER_ITEM) {
            if(quest.getTargetName().contains("#")) {
                String[] strings = quest.getTargetName().split("#");
                String file_Name = strings[0];
                String Item_Key = strings[1];
                if (event.getShopKey().equalsIgnoreCase(file_Name)) {
                    if (event.getConvertKey().equalsIgnoreCase(Item_Key)) {
                        playerData.addQuestAmount(quest);
                    }
                }
            }
        }
    }

}
