package com.yutian.minerwar.listener;

import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.game.Game;
import com.yutian.minerwar.game.Region;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import tools.RandomUtil;

import java.util.HashMap;

public class KeepEntitiesOnDeath implements Listener {


    public static HashMap<Player,Integer> deathsVaule = new HashMap<>();

    public static int getDeathsVaule(Player player){
        if(deathsVaule.containsKey(player)){
            return deathsVaule.get(player);
        }
        return 0;
    }

    @EventHandler
    public void onAutoRespawn(PlayerDeathEvent e) {
        e.setDeathMessage(null);
        Player p = e.getEntity();
        if(!deathsVaule.containsKey(p)){
            deathsVaule.put(p,1);
        }else {
            deathsVaule.put(p,deathsVaule.get(p)+1);
        }
        int deathAmount = deathsVaule.get(p);
        new BukkitRunnable() {
            private int i = 0;
            public void run() {
                if (i == 1) {
                    p.spigot().respawn();
                }
                if(i == 3) {
                    p.sendTitle("§4你嘎了", "§6累积死亡: "+deathAmount+"次");
                }
                if(i == 4) {
                    Game game = MinerWar.getGame();
                    Region region = game.getGameRegion();
                    int newX = RandomUtil.getRandomInt((int) region.getMin().getX(), (int) region.getMax().getX());
                    int newZ = RandomUtil.getRandomInt((int) region.getMin().getZ(), (int) region.getMax().getZ());
                    Block theHighestBlock = region.getWorld().getHighestBlockAt(newX,newZ);
                    p.teleport(theHighestBlock.getLocation().add(0,1,0));
                    if(!p.getAllowFlight()){
                        p.setAllowFlight(true);
                    }
                    deathDropsCoarseGold(p,deathAmount);
                    game.refreshHudMessage(p);
                    cancel();
                }
                i++;
            }
        }.runTaskTimer(MinerWar.inst(),0L, 2L);
    }

    /*死亡惩罚：
    * 累积死亡6次   掉落粗金  20%
    * 累积死亡11次  掉落粗金  30%
    * 累积死亡16次  掉落粗金  40%
    * 累积死亡21次  掉落粗金  50%
    * */
    public static void deathDropsCoarseGold(Player player,int deathAmount){
        Game game = MinerWar.getGame();
        double scale = 0.1;
        if(deathAmount >= 21){
            scale = 0.5;
        }else if(deathAmount >= 16){
            scale = 0.4;
        }else if(deathAmount >= 11){
            scale = 0.3;
        }else if(deathAmount >= 6){
            scale = 0.2;
        }
        int rawGoldAmount = game.getExcavateAmount(player); // 获取玩家有多少粗金
        if(rawGoldAmount >= 1) {
            int dropRawGold = (int) (rawGoldAmount * scale);// 获取本次死亡会掉落多少粗金
            if (dropRawGold <= 1) {
                dropRawGold = 1;
            }
            player.sendMessage("§c[消息] §a丢失了§e" + dropRawGold + "块§a黄金！");
            setBackpackThickGoldTime(player, rawGoldAmount - dropRawGold);
            game.effect_RefreshExcavateAmount(player);
        }
    }

    // 获取玩家背包有多少粗金
    public static void setBackpackThickGoldTime(Player player,int residualCrudeGold){
        Inventory inv = player.getInventory();
        for (int i = 0; i < 36; i++) {
            ItemStack item = inv.getItem(i);
            if (item != null && item.getType() != Material.AIR) {
                if (item.getType().equals(Material.RAW_GOLD)) {
                    inv.setItem(i, new ItemStack(Material.AIR));
                }
            }
        }
        if (residualCrudeGold >= 1) {
            ItemStack rawGold = new ItemStack(Material.RAW_GOLD,residualCrudeGold);
            player.getInventory().addItem(rawGold);
        }
    }
}
