package com.yaohun.storyline.data;

import com.yaohun.storyline.core.Quest;
import com.yaohun.storyline.manage.QuestManager;
import com.yaohun.storyline.util.MessageUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

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

public class PlayerData {

    private String playerName;
    private int currentQuest;

    private HashMap<Quest,Integer> questAmount;
    // -1 = 已完成
    // -99 = 未接受
    // 0~100+ = 进度

    public PlayerData(String playerName){
        this.playerName = playerName;
        this.currentQuest = 1;
        this.questAmount = new HashMap<>();
    }

    public void resetPlayerData(){
        this.currentQuest = 1;
        this.questAmount = new HashMap<>();
        receiveQuest(QuestManager.getQuest(1));
    }

    public void stopPlayerData(){
        this.currentQuest = -10;
        this.questAmount = new HashMap<>();
    }

    public PlayerData(String playerName, int currentQuest, HashMap<Quest,Integer> questAmount){
        this.playerName = playerName;
        this.currentQuest = currentQuest;
        this.questAmount = questAmount;
    }

    public int getCurrentQuest() {
        return currentQuest;
    }

    public void setCurrentQuest(int currentQuest) {
        this.currentQuest = currentQuest;
    }

    public String getPlayerName() {
        return playerName;
    }

    public Set<Quest> getAllQuest(){
        return questAmount.keySet();
    }

    public List<Quest> getQuestList() {
        List<Quest> questList = new ArrayList<>();
        for (Quest quest : questAmount.keySet()) {
            if(isReceivedQuest(quest) && !isFinishedQuest(quest)){
                questList.add(quest);
            }
        }
        return questList;
    }

    // 获取任务当前状态
    public int getQuestAmount(Quest quest){
        if(questAmount.containsKey(quest)){
            return questAmount.get(quest);
        }
        return 0;
    }

    // 判断玩家是否已接受任务
    public boolean isReceivedQuest(Quest quest){
        return this.questAmount.containsKey(quest);
    }

    // 接受任务
    public void receiveQuest(Quest quest) {
        if (!isReceivedQuest(quest)) {
            this.questAmount.put(quest, 0);
        }
    }

    // 放弃任务
    public void removeQuest(Quest quest) {
        questAmount.remove(quest);
    }

    // 判断玩家是否已提交该任务  true = 已完成
    public boolean isFinishedQuest(Quest quest){
        if(isReceivedQuest(quest)){
            int questStats = getQuestAmount(quest);
            if(questStats == -1){
                return true;
            }
        }
        return false;
    }

    // 判断任务是否已达到可提交要求
    public boolean canFinishQuest(Quest quest){
        if(isReceivedQuest(quest)){
            int amount = getQuestAmount(quest);
            if(amount >= quest.getTargetAmount()){
                return true;
            }
        }
        return false;
    }

    // 提交任务 设置状态为完成 同时 执行完成命令
    public void finisheQuest(Quest quest){
        if(!isFinishedQuest(quest)){
            quest.carryOutCommands(Bukkit.getPlayer(playerName));
            this.questAmount.put(quest,-1);
        }
    }

    // 接受任务，强制完成任务
    public void forcCompleteQuest(Quest quest) {
        if (!isFinishedQuest(quest)) {
            this.questAmount.put(quest, -1);
            int questID = quest.getNextQuestID();
            setCurrentQuest(questID);
            receiveQuest(QuestManager.getQuest(questID));
            System.out.println("#[已自动接受] #ID = "+questID);
            Player player = Bukkit.getPlayer(playerName);
            if(player != null) {
                quest.carryOutCommands(player);
                player.sendMessage(MessageUtil.getMessage("finishedQuestMessage").replace("{quest}",quest.getDisplayName()));
            }
        }
    }

    public void setQuestAmount(Quest quest, int amount){
        this.questAmount.put(quest, amount);
    }

    public void addQuestAmount(Quest quest){
        addQuestAmount(quest, 1);
    }

    public void addQuestAmount(Quest quest,int addAmount){
        // 玩家尚未接受任务 拦截返回
        if(!isReceivedQuest(quest)){
            return;
        }
        // 玩家已提交任务  拦截返回
        if(isFinishedQuest(quest)){
            return;
        }
        // 获取当前状态
        int questAmount = getQuestAmount(quest);
        int newAmount = questAmount + addAmount;
        if(newAmount > quest.getTargetAmount()){
            int needAmount = quest.getTargetAmount() - questAmount;
            questAmount = questAmount + needAmount;
            setQuestAmount(quest,questAmount);
        } else {
            setQuestAmount(quest,newAmount);
        }
        // 当任务进度满后自动提交任务
        if(canFinishQuest(quest)){
            forcCompleteQuest(quest);
        }
    }

    // 获取当前任务进度
    public double getProgress(Quest quest){
        if(!isReceivedQuest(quest)){
            return -99;
        }
        double amount = getQuestAmount(quest);
        double d = (double) amount / quest.getTargetAmount();
        if(d >= 1.0D){
            d = 1.0D;
        } else if(d <= 0.0D){
            d = 0.0D;
        }
        return d;
    }

    public String getProgressString(Quest quest){
        double progress = getProgress(quest);
        return String.format("%.2f", progress);
    }


    // 重载任务数据
    public void reload(List<Quest> quests){
        HashMap<Quest,Integer> hashMap = new HashMap<>();
        for (Quest newQuest : quests){
            for (Quest nowQuest : getAllQuest()){
                if(nowQuest == null){
                    continue;
                }
                if(newQuest == null){
                    continue;
                }
                if(newQuest.getQuestID() == nowQuest.getQuestID()){
                    hashMap.put(newQuest,this.questAmount.get(nowQuest));
                }
            }
        }
        this.questAmount = hashMap;
    }

    public boolean isAllCompleted(){
        if(this.currentQuest <= -1){
            return true;
        }
        int maxSize = QuestManager.getQuestArrayList().size();
        int nowSize = 0;
        for (Quest quest : questAmount.keySet()) {
            if(isFinishedQuest(quest)){
                nowSize++;
            }
        }
        if(nowSize >= maxSize){
            return true;
        }
        return false;
    }
}
