package com.yutian.minerwar.data;

import com.io.yutian.mclive.event.ZhuboAPI;
import com.yutian.minerwar.MinerWar;
import com.yutian.minerwar.liveevent.GiftEventHandler;
import com.yutian.minerwar.util.GiftUtil;
import de.tr7zw.nbtapi.NBTItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import tools.GameUtil;
import tools.RandomUtil;

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

public class RepairGiftGui implements Listener {

    public static String invTitle = "我的世界整蛊 - 礼物触发管理";

    public static String invTitle2 = "请选择触发礼物的数量";
    public static void OpenGui(Player p,ItemStack stack){
        Inventory inv = Bukkit.createInventory(null,18,invTitle2);
        inv.setItem(0,getGiftStack(stack,1,1));
        inv.setItem(1,getGiftStack(stack,2,2));
        inv.setItem(2,getGiftStack(stack,3,3));
        inv.setItem(3,getGiftStack(stack,5,5));
        inv.setItem(4,getGiftStack(stack,6,6));
        inv.setItem(5,getGiftStack(stack,7,7));
        inv.setItem(6,getGiftStack(stack,8,8));
        inv.setItem(7,getGiftStack(stack,9,9));
        inv.setItem(8,getGiftStack(stack,10,10));
        inv.setItem(9,getGiftStack(stack,6,66));
        inv.setItem(10,getGiftStack(stack,18,188));
        inv.setItem(11,getGiftStack(stack,52,520));
        inv.setItem(12,getGiftStack(stack,64,1314));
        p.openInventory(inv);
    }

    public static ItemStack getGiftStack(ItemStack stack,int itemAmount,int amount){
        ItemStack item = stack.clone();
        item.setAmount(itemAmount);
        ItemMeta meta = item.getItemMeta();
        List<String> lore = new ArrayList<>();
        lore.add(" ");
        lore.add("§b§l★ §6点击 §7执行§e"+amount+"§7次");
        meta.setLore(lore);
        item.setItemMeta(meta);
        NBTItem nbti = new NBTItem(item);
        nbti.setInteger("giftAmount",amount);
        return nbti.getItem();
    }

    @EventHandler
    public void onClick(InventoryClickEvent e){
        int rawSlot = e.getRawSlot();
        Player zhubo = (Player) e.getWhoClicked();
        Inventory inv = e.getInventory();
        if(e.getView().getTitle().equalsIgnoreCase(invTitle2)){
            e.setCancelled(true);
            if(rawSlot < 0 || rawSlot >= 18) { return;}
            zhubo.closeInventory();
            ItemStack stack = e.getCurrentItem();
            if (stack != null && stack.getType() != Material.AIR) {
                NBTItem nbti = new NBTItem(stack);
                if (nbti.hasKey("giftName")) {
                    String giftName = nbti.getString("giftName");
                    String userName = "抖音" + RandomUtil.getRandomInt(1, 100);
                    int amount = nbti.getInteger("giftAmount");
                    GiftData giftData = MinerWar.getGameManager().getGiftData(giftName);
                    giftData.OutCompleEvent(amount);
                    String hide_userName = GameUtil.hideName(userName);
                    String eventName = giftData.getEvent();
                    if (GiftUtil.specialGiftEffectTriggers(zhubo, eventName, hide_userName, amount)) {
                        giftData.OutPlaySoundsEvent();
                        return;
                    }
                    if (amount <= 1) {
                        String title = "§c" + eventName;
                        String subtitle = "§9" + hide_userName;
                        zhubo.sendTitle(title, subtitle, 0, 30, 10);
                        giftData.OutPlaySoundsEvent();
                        GiftEventHandler.SendHandLer(zhubo, userName, eventName);
                    } else {
                        String title = "§c" + eventName + " x" + amount;
                        String subtitle = "§9" + hide_userName;
                        zhubo.sendTitle(title, subtitle, 0, 30, 10);
                        for (int i = 0; i < amount; i++) {
                            Bukkit.getScheduler().runTaskLater(MinerWar.inst(), () -> {
                                giftData.OutPlaySoundsEvent();
                                GiftEventHandler.SendHandLer(zhubo, userName, eventName);
                            }, (long) i * 5);
                        }
                    }
                }
            }
        } else if(e.getView().getTitle().equalsIgnoreCase(invTitle)){
            if(rawSlot >= 0) {
                e.setCancelled(true);
                ItemStack stack = e.getCurrentItem();
                if (stack != null && stack.getType() != Material.AIR) {
                    NBTItem nbti = new NBTItem(stack);
                    if (nbti.hasKey("giftName")) {
                        String giftName = nbti.getString("giftName");
                        String userName = "抖音"+ RandomUtil.getRandomInt(1,100);
                        int amount = 1;
                        zhubo.closeInventory();
                        if(MinerWar.getGameManager().getGiftData(giftName) == null){
                            zhubo.sendTitle("§r", "§c未设置效果",10, 30, 10);
                            return;
                        }
                        if(e.getClick() == ClickType.SHIFT_RIGHT || e.getClick() == ClickType.SHIFT_LEFT){
                            OpenGui(zhubo,stack);
                            return;
                        }
                        if (e.getClick() == ClickType.RIGHT) {
                            amount = 10;
                        }
                        GiftData giftData = MinerWar.getGameManager().getGiftData(giftName);
                        giftData.OutCompleEvent(amount);
                        String hide_userName = GameUtil.hideName(userName);
                        String eventName = giftData.getEvent();
                        if(GiftUtil.specialGiftEffectTriggers(zhubo,eventName,hide_userName,amount)){
                            giftData.OutPlaySoundsEvent();
                            return;
                        }
                        if (amount <= 1) {
                            String title = "§c" + eventName;
                            String subtitle = "§9" + hide_userName;
                            zhubo.sendTitle(title, subtitle, 0, 30, 10);
                            giftData.OutPlaySoundsEvent();
                            GiftEventHandler.SendHandLer(zhubo, userName, eventName);
                        } else {
                            String title = "§c" + eventName + " x" + amount;
                            String subtitle = "§9" + hide_userName;
                            zhubo.sendTitle(title, subtitle, 0, 30, 10);
                            for (int i = 0; i < amount; i++) {
                                Bukkit.getScheduler().runTaskLater(MinerWar.inst(), () -> {
                                    giftData.OutPlaySoundsEvent();
                                    GiftEventHandler.SendHandLer(zhubo, userName, eventName);
                                }, (long) i * 5);
                            }
                        }
                    }
                }
            }
        }
    }

    public static void OpenGui(Player p){
        Inventory inv = Bukkit.createInventory(null,27,invTitle);
        HashMap<String,ItemStack> hashMap = new HashMap<>();
        if(ZhuboAPI.getRoomLiveName().equalsIgnoreCase("KuaiShou")) {
            hashMap.put("棒棒糖",stackGift("棒棒糖", 101));
            hashMap.put("比心",stackGift("比心", 102));
            hashMap.put("粉丝团灯牌",stackGift("粉丝团灯牌", 103));
            hashMap.put("集结票",stackGift("集结票", 104));
            hashMap.put("浪漫风铃",stackGift("浪漫风铃", 105));
            hashMap.put("玫瑰",stackGift("玫瑰", 106));
            hashMap.put("玫瑰花园",stackGift("玫瑰花园", 107));
            hashMap.put("魔法箱子",stackGift("魔法箱子", 108));
            hashMap.put("摸摸头",stackGift("摸摸头", 109));
            hashMap.put("陪伴你",stackGift("陪伴你", 110));
            hashMap.put("啤酒",stackGift("啤酒", 111));
            hashMap.put("人气卡",stackGift("人气卡", 112));
            hashMap.put("人气票",stackGift("人气票", 113));
            hashMap.put("送你花环",stackGift("送你花环", 114));
            hashMap.put("童话日记",stackGift("童话日记", 115));
            hashMap.put("小白菜",stackGift("小白菜", 116));
            hashMap.put("钻戒",stackGift("钻戒", 117));
            hashMap.put("荧光棒",stackGift("荧光棒", 118));
            hashMap.put("小可爱",stackGift("小可爱", 119));
        } else {
            hashMap.put("ONE礼挑一", stackGift("ONE礼挑一", 1));
            hashMap.put("Thuglife", stackGift("Thuglife", 2));
            hashMap.put("爱的纸鹤", stackGift("爱的纸鹤", 3));
            hashMap.put("爱你哟", stackGift("爱你哟", 4));
            hashMap.put("棒棒糖", stackGift("棒棒糖", 5));
            hashMap.put("比心", stackGift("比心", 6));
            hashMap.put("比心兔兔", stackGift("比心兔兔", 7));
            hashMap.put("称心如意", stackGift("称心如意", 8));
            hashMap.put("大啤酒", stackGift("大啤酒", 9));
            hashMap.put("点亮孤单", stackGift("点亮孤单", 10));
            hashMap.put("抖音", stackGift("抖音", 11));
            hashMap.put("抖音1号", stackGift("抖音1号", 12));
            hashMap.put("多喝热水", stackGift("多喝热水", 13));
            hashMap.put("繁花秘语", stackGift("繁花秘语", 14));
            hashMap.put("粉丝团灯牌", stackGift("粉丝团灯牌", 15));
            hashMap.put("光之祝福", stackGift("光之祝福", 16));
            hashMap.put("豪华邮轮", stackGift("豪华邮轮", 17));
            hashMap.put("花开烂漫", stackGift("花开烂漫", 18));
            hashMap.put("花落长亭", stackGift("花落长亭", 19));
            hashMap.put("环球旅行车", stackGift("环球旅行车", 20));
            hashMap.put("黄桃罐头", stackGift("黄桃罐头", 21));
            hashMap.put("加油鸭", stackGift("加油鸭", 22));
            hashMap.put("嘉年华", stackGift("嘉年华", 23));
            hashMap.put("浪漫花火", stackGift("浪漫花火", 24));
            hashMap.put("礼花筒", stackGift("礼花筒", 25));
            hashMap.put("龙抬头", stackGift("龙抬头", 26));
            hashMap.put("玫瑰", stackGift("玫瑰", 27));
            hashMap.put("你最好看", stackGift("你最好看", 28));
            hashMap.put("捏捏小脸", stackGift("捏捏小脸", 29));
            hashMap.put("跑车", stackGift("跑车", 30));
            hashMap.put("保时捷", stackGift("保时捷", 30));
            hashMap.put("怦然心动", stackGift("怦然心动", 31));
            hashMap.put("亲吻", stackGift("亲吻", 32));
            hashMap.put("拳拳出击", stackGift("拳拳出击", 33));
            hashMap.put("热气球", stackGift("热气球", 34));
            hashMap.put("人气票", stackGift("人气票", 35));
            hashMap.put("日出相伴", stackGift("日出相伴", 36));
            hashMap.put("闪耀星辰", stackGift("闪耀星辰", 37));
            hashMap.put("私人飞机", stackGift("私人飞机", 38));
            hashMap.put("送你花花", stackGift("送你花花", 39));
            hashMap.put("万象烟花", stackGift("万象烟花", 40));
            hashMap.put("为你闪耀", stackGift("为你闪耀", 41));
            hashMap.put("为你举牌", stackGift("为你举牌", 42));
            hashMap.put("鲜花", stackGift("鲜花", 43));
            hashMap.put("小心心", stackGift("小心心", 44));
            hashMap.put("星星点灯", stackGift("星星点灯", 45));
            hashMap.put("一点心意", stackGift("一点心意", 46));
            hashMap.put("一束花开", stackGift("一束花开", 47));
            hashMap.put("荧光棒", stackGift("荧光棒", 48));
            hashMap.put("游戏手柄", stackGift("游戏手柄", 49));
            hashMap.put("掌上明珠", stackGift("掌上明珠", 50));
            hashMap.put("真爱玫瑰", stackGift("真爱玫瑰", 51));
            hashMap.put("真的爱你", stackGift("真的爱你", 52));
            hashMap.put("直升机", stackGift("直升机", 53));
            hashMap.put("纸短情长", stackGift("纸短情长", 54));
            hashMap.put("蝶 · 比翼鸟", stackGift("蝶 · 比翼鸟", 56));
            hashMap.put("永生花", stackGift("永生花", 57));
        }
        HashMap<String, GiftData> giftData = MinerWar.getGameManager().getGiftDataMap();
        for (String giftName : giftData.keySet()){
            if(hashMap.get(giftName) != null){
                inv.addItem(hashMap.get(giftName));
            }else{
                inv.addItem(stackGift(giftName,-1));
            }
        }
        p.openInventory(inv);
    }

    public static ItemStack stackGift(String name,int model_id) {
        ItemStack item = new ItemStack(Material.PAPER);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§d§l★ §e礼物: " + name);
        List<String> lore = new ArrayList<>();
        if (MinerWar.getGameManager().getGiftData(name) != null) {
            GiftData giftData = MinerWar.getGameManager().getGiftData(name);
            lore.add("§7触发效果: §a" + giftData.getEvent() + " " + giftData.getComple_Show());
        } else {
            lore.add("§7触发效果: §c无");
        }
        lore.add(" ");
        lore.add("§b§l★ §6左键点击 §7召唤1次");
        lore.add("§b§l★ §6右键点击 §7召唤10次");
        lore.add("§b§l★ §6SHIFT+点击 §7自定义数量");
        meta.setLore(lore);
        if (model_id >= 1){
            meta.setCustomModelData(model_id);
        }else{
            item.setType(Material.DIAMOND);
        }
        item.setItemMeta(meta);
        NBTItem nbti = new NBTItem(item);
        nbti.setString("giftName",name);
        item = nbti.getItem();
        return item;
    }
}
