package com.yutian.minerwar.util;

import com.yaohun.RandomBox.api.RBoxAPI;
import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.data.GiftData;
import com.yutian.minerwar.liveevent.GiftEventHandler;
import com.yutian.minerwar.manager.GameManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class GiftUtil {
    public static void simulateEventEffect(Player player,String userName, String eventName, int amount){
        if(specialGiftEffectTriggers(player,eventName,userName,amount)){
            return;
        }
        GiftEventHandler.SendHandLer(player, userName, eventName);
    }
    public static void simulatedGiftEffect(Player player, String userName, String giftName, int amount){
        GameManager manager = MinerWar.getGameManager();
        if(!manager.isGiftDataExit(giftName)){
            return;
        }
        GiftData giftData = manager.getGiftData(giftName);
        giftData.OutCompleEvent(amount);
        String eventName = giftData.getEvent();
        if(GiftUtil.specialGiftEffectTriggers(player,eventName,userName,amount)){
            giftData.OutPlaySoundsEvent();
            return;
        }
        String title = "§c" + eventName;
        if(amount >= 2) {
            title = "§c" + eventName + " x" + amount;
        }
        String subtitle = "§9" + giftName;
        player.sendTitle(title, subtitle,0, 30, 10);
        if (amount <= 1) {
            giftData.OutPlaySoundsEvent();
            GiftEventHandler.SendHandLer(player, userName, eventName);
        } else {
            long dadey = 5L;
            for (int i = 0; i < amount; i++) {
                Bukkit.getScheduler().runTaskLater(MinerWar.inst(), giftData::OutPlaySoundsEvent, (long) i * dadey);
            }
            for (int i = 0; i < amount; i++) {
                GiftEventHandler.SendHandLer(player, userName, eventName);
            }
        }
    }

    public static boolean specialGiftEffectTriggers(Player player,String eventName,String show_userName,int amount) {
        if (eventName.contains("盲盒")) {
            if (eventName.contains("#")) {
                String s1 = "love";
                String s2 = "000304";
                String[] eventSp = eventName.split("#");
                String boxKey = eventSp[1];
                int boxAmount = 1;
                if (boxKey.contains("X")) {
                    boxAmount = Integer.parseInt(boxKey.split("X")[1]);
                    boxKey = s1 + "-" + s2 + "-" + boxKey.split("X")[0];
                } else if (boxKey.contains("x")) {
                    boxAmount = Integer.parseInt(boxKey.split("x")[1]);
                    boxKey = s1 + "-" + s2 + "-" + boxKey.split("x")[0];
                } else {
                    boxKey = s1 + "-" + s2 + "-" + boxKey;
                }
                amount = (boxAmount * amount);
                RBoxAPI.addUserData(player, show_userName, boxKey, amount);
            } else {
                System.out.println("[错误 - 盲盒] 随机盲盒在礼物设置中配置错误.");
            }
            return true;
        }
        return false;
    }
}
