import * from Minecraft;
import Network from util.Network;

suppressDeprecated(true);

client = MinecraftClient.getClient();
player = client.getPlayer();
flag = true;

// new searchScreen
searchScreen = new FakeScreen("快捷搜索", 3);
searchScreen.setStackForSlot(18, client.itemFromString("egg").setCustomName("点此打开XPlus整合包宣传贴"));
searchScreen.setStackForSlot(26, client.itemFromString("barrier").setCustomName("关闭当前窗口"));

// register GameEvent
new GameEvent("onPlayerJoin", fun(name) {
    if(flag){
        player.message(Text.of("使用 /xplus search <搜索内容> 在MC WIKI等网站中快速搜索").format("BLUE"));
        flag = false;
    }
}).register();
//playerJoin.register();

// share link
fun sendLink(info, link){
    player.message(Text.of("[点此分享]").withClickEvent("run_function", fun(info, link){
        info = info + "：";
        player.say(info);
        player.say(link);
        player.say(" - XPlus Quick Search");
    }).format("BLUE"));
}

// register command
client.addCommand({
    "name" : "xplus",
    "subcommands" : {
        "search" : {
            "<info>" : {
                "" : fun(info) {
                        wiki = "https://searchwiki.biligame.com/mc/index.php?search=" + info;
                        bilibili = "https://search.bilibili.com/all?keyword=" + info;
                        mcbbs = "https://www.mcbbs.net/search.php?mod=forum&searchsubmit=yes&wd=" + info;

                        wikiInfo = "点击查看MCWiki中 §2" + info +" §r的搜索结果";
                        biInfo = "点击查看Bilibili中 §2" + info +" §r的搜索结果";
                        bbsInfo = "点击查看MCBBS中 §2" + info +" §r的搜索结果";
                        
                        wikiItem = client.itemFromString("bookshelf").setCustomName(wikiInfo);
                        biItem = client.itemFromString("light_blue_glazed_terracotta").setCustomName(biInfo);
                        bbsItem = client.itemFromString("chest").setCustomName(bbsInfo);

                        searchScreen.setStackForSlot(11, wikiItem);
                        searchScreen.setStackForSlot(13, biItem);
                        searchScreen.setStackForSlot(15, bbsItem);

                        searchScreen.onClick(fun(item, slot, action) {
                            switch (slot){
                                case 11 -> {
                                    player.closeScreen();
                                    player.message(Text.of(wikiInfo).withClickEvent("open_url", wiki).format("BLUE"));
                                    // sendLink(wikiInfo, wiki);
                                }
                                case 13 -> {
                                    player.closeScreen();
                                    player.message(Text.of(biInfo).withClickEvent("open_url", bilibili).format("BLUE"));
                                    // sendLink(biInfo, bilibili);
                                }
                                case 15 -> {
                                    player.closeScreen();
                                    player.message(Text.of(bbsInfo).withClickEvent("open_url", mcbbs).format("BLUE"));
                                    // sendLink(bbsInfo, mcbbs);
                                }
                                case 18 -> {
                                    player.closeScreen();
                                    player.message(Text.of("点此打开XPlus整合包宣传贴").withClickEvent("open_url", "https://www.mcbbs.net/thread-936198-1-1.html").format("BLUE"));
                                }
                                case 26 ->{
                                    player.closeScreen();
                                }
                            }
                        });

                        player.openScreen(searchScreen);
                }
            }
        }
    },
    "arguments" : {
        "info" : { "type" : "GreedyString" }
    }
});

/*
    fun getAndSetSearchScreen(info){
        wiki = "https://searchwiki.biligame.com/mc/index.php?search=" + info;
        bilibili = "https://search.bilibili.com/all?keyword=" + info;
        mcbbs = "https://www.baidu.com/s?wd=%20site%3Amcbbs.net " + info;

        wikiItem = client.itemFromString("bookshelf").setCustomName("点击查看MCWiki中 " + info +" 的搜索结果");
        biItem = client.itemFromString("light_blue_glazed_terracotta").setCustomName("点击查看MCWiki中 " + info +" 的搜索结果");
        bbsItem = client.itemFromString("chest").setCustomName("点击查看MCWiki中 " + info +" 的搜索结果");

        searchScreen.setStackForSlot(11, wikiItem);
        searchScreen.setStackForSlot(13, biItem);
        searchScreen.setStackForSlot(15, bbsItem);

        searchScreen.onClick(fun(item, slot, action) {
            switch (slot){
                case 13 -> {
                    Network.openUrl(wiki);
                }
                case 15 -> {
                    Network.openUrl(bilibili);
                }
                case 17 -> {
                    Network.openUrl(mcbbs);
                }
                case 26 -> {
                    Network.openUrl("https://www.mcbbs.net/thread-936198-1-1.html");
                }
            }
        });
    }
*/
hold();