package com.yaohun.storyline.core;

import com.yaohun.storyline.Storyline;
import com.yaohun.storyline.util.QuestType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Quest {

    private int questID; // 任务唯一id
    private String displayName; // 任务名
    private String description; // 描述信息
    private QuestType questType; // 任务类型
    private String targetName; // 目标名
    private int targetAmount; // 目标次数
    private List<String> rewardList; // 奖励内容
    private List<String> commands; // 奖励命令
    private int nextQuestID;
    private Location startingPoint;
    private List<String> introduceList;

    public Quest(int questID, String displayName, String description, QuestType questType, String targetName, int targetAmount, List<String> rewardList, List<String> commands, int nextQuestID, Location startingPoint, List<String> introduceList) {
        this.questID = questID;
        this.displayName = displayName;
        this.description = description;
        this.questType = questType;
        this.targetName = targetName;
        this.targetAmount = targetAmount;
        this.rewardList = rewardList;
        this.commands = commands;
        this.nextQuestID = nextQuestID;
        this.startingPoint = startingPoint;
        this.introduceList = introduceList;
    }

    public int getQuestID() {
        return questID;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getDescription() {
        return description;
    }

    public QuestType getQuestType() {
        return questType;
    }

    public String getTargetName() {
        return targetName;
    }

    public int getTargetAmount() {
        return targetAmount;
    }

    public List<String> getRewardList() {
        return rewardList;
    }

    public List<String> getCommands() {
        return commands;
    }

    public int getNextQuestID() {
        return nextQuestID;
    }

    public Location getStartingPoint() {
        return startingPoint;
    }

    public void setStartingPoint(Location startingPoint) {
        this.startingPoint = startingPoint;
        File file = new File(Storyline.inst().getDataFolder(), "Quest.yml");
        FileConfiguration config = YamlConfiguration.loadConfiguration(file);
        double x = startingPoint.getX();
        double y = startingPoint.getY();
        double z = startingPoint.getZ();
        float yaw = startingPoint.getYaw();
        float pitch = startingPoint.getPitch();
        String locString = String.format("%.1f",x)+","+String.format("%.1f",y)+","+String.format("%.1f",z)+","+String.format("%.1f",yaw)+","+String.format("%.1f",pitch);
        config.set(questID+".location",startingPoint.getWorld().getName()+","+locString);
        try {
            config.save(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<String> getIntroduceList() {
        return introduceList;
    }

    public void carryOutCommands(Player player){
        if(player == null){
            return;
        }
        for (String s : this.commands) {
            if(s.contains("msg:")){
                player.sendMessage(s.replace("msg:","").replace("&","§"));
            }else {
                String cmd = s.replace("console:","").replace("&","§");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", player.getName()));
            }
        }
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP,1,1);
    }
}
