package com.yaohun.wheatwar.liveevent;

import com.io.yutian.mclive.event.LiveChatEvents;
import com.io.yutian.mclive.event.LiveLikeEvents;
import com.yaohun.wheatwar.WheatWar;
import com.yaohun.wheatwar.data.LikeData;
import com.yaohun.wheatwar.manage.GameManager;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector;
import tools.CDTimeAPI;
import tools.RandomUtil;

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

public class LikeListener implements Listener {

    private GameManager gameManager;
    public LikeListener(GameManager manager){
        gameManager = manager;
    }
    @EventHandler//点赞
    public void onDianZan(LiveLikeEvents e) {
        Player zhubo = e.getPlayer();
        String userName = ""+ RandomUtil.getRandomInt(999,10000);
        if(e.getUser().nickName() != null) {
            userName = e.getUser().nickName();
        }
        long add_amount = e.getCount();
        HashMap<String, LikeData> likeDataMap = gameManager.getLikeDataMap();
        for (String key : likeDataMap.keySet()){
            LikeData likeData = likeDataMap.get(key);
            likeData.addNowAmount((int)add_amount);
            dropLikeEffect(zhubo, (int) add_amount);
            if(likeData.meetTheQuantityRequirement()){
                String eventName = likeData.getEffectKey();
                String title = "§c";
                String subtitle = "§c("+eventName+") §f- §9"+likeData.getNeedAmount()+"个戳一戳";
                // String subtitle = "§r";
                likeData.OutPlaySoundsEvent();
                zhubo.sendTitle(title, subtitle,0, 30, 10);
                GiftEventHandler.SendHandLer(zhubo,userName,eventName);
                likeData.setNowAmount(0);
            }
        }
    }

    public void dropLikeEffect(Player zhubo,int amount) {
        List<ItemStack> stackList = new ArrayList<>();
        stackList.add(getLikeStack(251));
        stackList.add(getLikeStack(252));
        stackList.add(getLikeStack(253));
        stackList.add(getLikeStack(254));
        stackList.add(getLikeStack(255));
        stackList.add(getLikeStack(256));
        stackList.add(getLikeStack(257));
        for (int i = 0; i < amount; i++) {
            Bukkit.getScheduler().runTaskLater(WheatWar.inst(), () -> {
                int rand = RandomUtil.getRandomInt(0, stackList.size() - 1);
                ItemStack stack = stackList.get(rand);
                Item item = zhubo.getWorld().dropItem(zhubo.getLocation().add(0,1,0), stack);
                item.setVelocity(item.getLocation().getDirection().multiply(0.25D));
                item.setVelocity(new Vector(item.getVelocity().getX(), 0.4D, item.getVelocity().getZ()));
                Bukkit.getScheduler().runTaskLater(WheatWar.inst(), item::remove, (long) 60);
            }, (long) i * 5);
        }
    }

    public ItemStack getLikeStack(int modelId){
        ItemStack item = new ItemStack(Material.SUGAR);
        ItemMeta meta = item.getItemMeta();
        List<String> lore = new ArrayList<>();
        lore.add("#"+RandomUtil.getRandomInt(999,100000));
        meta.setLore(lore);
        meta.setCustomModelData(modelId);
        item.setItemMeta(meta);
        return item;
    }
}
