package net.wayward_realms.waywardessentials.command;

import net.wayward_realms.waywardessentials.WaywardEssentials;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;

import java.io.IOException;

public class IssueCommand implements CommandExecutor {

    private WaywardEssentials plugin;

    public IssueCommand(WaywardEssentials plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (args.length >= 1) {
                if (args[0].equalsIgnoreCase("create")) {
                    for (ItemStack remainder : player.getInventory().addItem(new ItemStack(Material.BOOK_AND_QUILL, 1)).values()) {
                        player.getWorld().dropItem(player.getLocation(), remainder);
                    }
                    sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN + "Please write your issue in the book, titling it with the title of the issue, then use /issue submit [plugin] with the book in hand when done.");
                } else if (args[0].equalsIgnoreCase("submit")) {
                    if (args.length >= 2) {
                        if (plugin.getServer().getPluginManager().getPlugin(args[1]) != null) {
                            if (args[1].startsWith("Wayward")) {
                                if (player.getItemInHand() != null) {
                                    if (player.getItemInHand().hasItemMeta()) {
                                        if (player.getItemInHand().getItemMeta() instanceof BookMeta) {
                                            BookMeta meta = (BookMeta) player.getItemInHand().getItemMeta();
                                            StringBuilder issueBody = new StringBuilder();
                                            issueBody.append("Submitted by ").append(sender.getName()).append('\n');
                                            for (String pageText : meta.getPages()) {
                                                issueBody.append("> ").append(pageText).append('\n');
                                            }
                                            try {
                                                GHRepository repo = plugin.getGitHub().getRepository("Wayward/" + args[1]);
                                                GHIssue issue = repo.createIssue(meta.hasTitle() ? meta.getTitle() : "Issue").body(issueBody.toString()).create();
                                                player.setItemInHand(null);
                                                sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN + "Issue submitted. Thank you!");
                                                sender.sendMessage(plugin.getPrefix() + ChatColor.GREEN + "You can view it at " + issue.getUrl().toString());
                                            } catch (IOException exception) {
                                                sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "Failed to create issue.");
                                                exception.printStackTrace();
                                            }
                                        } else {
                                            sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You must be holding a book in order to create an issue.");
                                        }
                                    } else {
                                        sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You must be holding a book in order to create an issue.");
                                    }
                                } else {
                                    sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You must be holding a book in order to create an issue.");
                                }
                            } else {
                                sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You may not report issues on third-party plugins from in-game.");
                            }
                        } else {
                            sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "That plugin is not installed.");
                        }
                    } else {
                        sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "Usage: /issue submit [plugin]");
                    }
                } else {
                    sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "Usage: /issue [submit|create]");
                }
            } else {
                sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "Usage: /issue [submit|create]");
            }
        } else {
            sender.sendMessage(plugin.getPrefix() + ChatColor.RED + "You must be a player to report issues.");
        }
        return true;
    }

}

@@ -1515,225 +1515,8 @@
 ) %7B%0A
-                    if (args.length %3E= 2) %7B%0A                        if (plugin.getServer().getPluginManager().getPlugin(args%5B1%5D) != null) %7B%0A                            if (args%5B1%5D.startsWith(%22Wayward%22)) %7B%0A            
     
@@ -1565,36 +1565,24 @@
  != null) %7B%0A
-            
             
@@ -1661,36 +1661,24 @@
             
- 
-           
 if (player.g
@@ -1761,28 +1761,16 @@
         
-      
-      
 BookMeta
@@ -1822,36 +1822,24 @@
 ItemMeta();%0A
-            
             
@@ -1933,36 +1933,24 @@
             
-            
 issueBody.ap
@@ -2038,36 +2038,24 @@
             
-     
-       
 for (String 
@@ -2080,36 +2080,24 @@
 tPages()) %7B%0A
-            
             
@@ -2206,35 +2206,18 @@
         
-            %7D%0A     
+%7D%0A
         
@@ -2244,33 +2244,14 @@
     
-       try %7B%0A            
+try %7B%0A
     
@@ -2347,35 +2347,26 @@
 ward
-/%22 + args%5B1%5D);%0A            
+Realms/Wayward%22);%0A
     
@@ -2544,36 +2544,24 @@
             
-            
 player.setIt
@@ -2608,36 +2608,24 @@
             
-      
-      
 sender.sendM
@@ -2698,36 +2698,24 @@
 ank you!%22);%0A
-            
             
@@ -2880,28 +2880,16 @@
         
-            
 %7D catch 
@@ -2946,36 +2946,24 @@
             
-     
-       
 sender.sendM
@@ -3070,28 +3070,16 @@
         
-            
 exceptio
@@ -3131,34 +3131,18 @@
         
-            %7D%0A    
+%7D%0A
         
@@ -3157,32 +3157,24 @@
             
-        
 %7D else %7B%0A   
@@ -3166,36 +3166,24 @@
    %7D else %7B%0A
-            
             
@@ -3346,33 +3346,18 @@
         
-            %7D%0A   
+%7D%0A
         
@@ -3376,38 +3376,17 @@
     
-         %7D else %7B%0A            
+%7D else %7B%0A
     
@@ -3549,32 +3549,18 @@
         
-            %7D%0A  
+%7D%0A
         
@@ -3575,39 +3575,17 @@
     
-          %7D else %7B%0A            
+%7D else %7B%0A
     
@@ -3724,584 +3724,8 @@
 %22);%0A
-                                %7D%0A                            %7D else %7B%0A                                sender.sendMessage(plugin.getPrefix() + ChatColor.RED + %22You may not report issues on third-party plugins from in-game.%22);%0A                            %7D%0A                        %7D else %7B%0A                            sender.sendMessage(plugin.getPrefix() + ChatColor.RED + %22That plugin is not installed.%22);%0A                        %7D%0A                    %7D else %7B%0A                        sender.sendMessage(plugin.getPrefix() + ChatColor.RED + %22Usage: /issue submit %5Bplugin%5D%22);%0A
     

Fix issue submission repository, fixes #5