package RiseOfDeath.Help;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration;
import org.bukkit.util.config.ConfigurationNode;

public class oldHelp extends JavaPlugin{

	private Configuration config;
	
	public static Logger log;
    public static Server server;
    protected PluginManager pm;
    
    private boolean AntiIdiot;
    private int MaxRecursion;
    private int MaxTitleLenght;
    private int MaxStringsPerScreen;
    
    private Vector<HelpTopic> Sections;
    private Vector<HelpUser> Users;
    
    
    private final HelpPlayerListener playerListener = new HelpPlayerListener(this);
    
	@Override
	public void onDisable() {
		// TODO Auto-generated method stub
		config.save();
		
	}

	@Override
	public void onEnable() {
		Sections = new Vector<HelpTopic>();
		Users = new Vector<HelpUser>();
		
		pm = getServer().getPluginManager();
		server=this.getServer();
		config=getConfiguration();
		log=Logger.getLogger("Minecraft");
		AntiIdiot=config.getNode("AntiIdiotProtection").getBoolean("Enable", true);
		MaxRecursion=config.getNode("AntiIdiotProtection").getInt("MaxIncludeLevel", 0);
		MaxTitleLenght=config.getNode("AntiIdiotProtection").getInt("MaxTitleLength", 0);
		MaxStringsPerScreen=config.getNode("AntiIdiotProtection").getInt("MaxStringsPerScreen", 0);
		
		log.info("Anti idiot is " + Boolean.toString(AntiIdiot));
		File file=new File(this.getDataFolder(), "content.yml");
		if(!file.isFile())
		{
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		loadTitles();
		pm.registerEvent(Event.Type.PLAYER_JOIN, this.playerListener, Event.Priority.Normal, this);
		
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) 

	{
		String strBuf;
		if(label.equalsIgnoreCase("help"))
		{
			if(args.length!=0)
			{			
				for(int i=0;i<Users.size();i++)
				{
					if(Users.get(i).getPlayer().equalsIgnoreCase(sender.getName()))
					{
						if(Users.get(i).getLastTipic()==null)
						{
							for(int j=0;j < Sections.size();j++)
							{
								try
								{
									if(Sections.get(j).getName().equalsIgnoreCase(args[0]))
									{
										if(Sections.get(j).isNoSub())
										{
											sender.sendMessage(Sections.get(j).getSubject());
											return true;
										}
										else
										{
											sender.sendMessage("Try set subsection " + Sections.get(j).getName());
											Users.get(i).setLastTipic(Sections.get(j));
											strBuf="Help topics:";
											sender.sendMessage(strBuf);
											sender.sendMessage(Integer.toString(Users.get(i).getLastTipic().getSections().size()));
											for(int k=0;k < Users.get(i).getLastTipic().getSections().size();k++)
											{
												strBuf=new String();
												if(Sections.get(j).getSections().get(i).isNoSub())
												{
													strBuf=ChatColor.GREEN + Users.get(i).getLastTipic().getSections().get(k).getName() + "";
												}
												else
												{
													strBuf=ChatColor.YELLOW + Users.get(i).getLastTipic().getSections().get(k).getName() + "";
												}
												sender.sendMessage(strBuf);
											}
											return true;
										}
									}
									else
									{
										
									}
								}
								catch(NullPointerException ex)
								{
									sender.sendMessage("Oh no");
									ex.printStackTrace();
								}
							}
						}
						else
						{
							for(int j=0;j<Users.get(i).getLastTipic().getSections().size();j++)
							{
								if(Users.get(i).getLastTipic().getSections().get(j).getName().equalsIgnoreCase(args[0]))
								{
									if(Users.get(i).getLastTipic().getSections().get(j).isNoSub())
									{
										sender.sendMessage(Users.get(i).getLastTipic().getSections().get(j).getSubject());
										return true;
									}
									else
									{
										sender.sendMessage("Try set subsection " + Users.get(i).getLastTipic().getSections().get(j).getName());
										Users.get(i).setLastTipic(Users.get(i).getLastTipic().getSections().get(j));
										strBuf="Help topics:";
										sender.sendMessage(strBuf);
										for(int k=0;k < Users.get(i).getLastTipic().getSections().get(j).getSections().size();k++)
										{
											strBuf=new String();
											if(Users.get(i).getLastTipic().getSections().get(j).getSections().get(k).isNoSub())
											{
												strBuf=ChatColor.GREEN + Users.get(i).getLastTipic().getSections().get(j).getSections().get(k).getName() + "";
											}
											else
											{
												strBuf=ChatColor.YELLOW + Users.get(i).getLastTipic().getSections().get(j).getSections().get(k).getName() + "";
											}
											sender.sendMessage(strBuf);
										}
										return true;
									}
								}
							}
							
						}
					}
					else
					{
						
					}
				}
				
			}
			else
			{
				strBuf="Help topics:";
				sender.sendMessage(strBuf);
				for(int i=0;i < Sections.size();i++)
				{
					strBuf=new String();
					if(Sections.get(i).isNoSub())
					{
						strBuf=ChatColor.GREEN + Sections.get(i).getName() + "";
					}
					else
					{
						strBuf=ChatColor.YELLOW + Sections.get(i).getName() + "";
					}
					sender.sendMessage(strBuf);
					
				}
				return true;
			}
			
		}
		return false;		
	}
	
	
	private void loadTitles()
	{
		HelpTopic buf,buf1;
		
		File fileBuf;
		fileBuf=new File(getDataFolder(),"main");
		if(fileBuf.listFiles()==null)
		{
			return;
		}
		
		for(int i=0;i<fileBuf.listFiles().length;i++)
		{
			
			if(fileBuf.listFiles()[i].isDirectory())
			{
				//log.info(Integer.toString(fileBuf.listFiles()[i].listFiles().length));
				buf=new HelpTopic();
				buf.setNoSub(false);
				log.info("Try read directory: " + fileBuf.listFiles()[i].getName());
				buf.setName(fileBuf.listFiles()[i].getName());
				log.info(Integer.toString(fileBuf.listFiles()[i].listFiles().length) + " files in " + fileBuf.listFiles()[i].getName());
				if(fileBuf.listFiles()[i].listFiles().length==0)
				{
					Sections.add(buf);
				}
				else
				{
					buf.setSubject(getSubject(fileBuf));
					for(int j=0;j< fileBuf.listFiles()[i].listFiles().length;j++)
					{
						buf1=loadTitles(fileBuf.listFiles()[i].listFiles()[j]);
						if(buf1!=null)
						{
							buf.addSection(buf1);
						}
					}
					Sections.add(buf);
				}
				
				
			}
			else
			{
				buf=new HelpTopic();
				buf.setNoSub(true);
				log.info("Try read file: " + fileBuf.listFiles()[i].getName());
				buf.setSubject(getSubject(fileBuf.listFiles()[i]));
				buf.setName(getName(fileBuf.listFiles()[i]));
				Sections.add(buf);
			}
		}
	}
	
	private HelpTopic loadTitles(File fileBuf)
	{
		HelpTopic buf,buf1;
		log.info(fileBuf.getName());
		if(fileBuf.listFiles()==null)
		{
			log.info("NULL??????");
			return null;
		}
		for(int i=0;i<fileBuf.length();i++)
		{
			if(fileBuf.listFiles()[i].isDirectory())
			{
				buf=new HelpTopic();
				buf.setNoSub(false);
				log.info("Try read directory: " + fileBuf.listFiles()[i].getName());
				buf.setSubject(getSubject(fileBuf));
				buf.setName(fileBuf.getName());
				for(int j=0;j< fileBuf.listFiles()[i].listFiles().length;j++)
				{
					buf1=loadTitles(fileBuf.listFiles()[j]);
					if(buf1==null)
					{
						log.info("Empty folder :(((");
					}
					else
					{
						buf.addSection(buf1);						
					}

				}
			}
			else
			{
				buf=new HelpTopic();
				buf.setNoSub(true);
				log.info("Try read file: " + fileBuf.listFiles()[i].getName());
				buf.setSubject(getSubject(fileBuf.listFiles()[i]));
				buf.setName(getName(fileBuf.listFiles()[i]));
				Sections.add(buf);
			}
		}
		
		return null;
	}
	
	private String getSubject(File textFile)
	{
		Configuration confBuf;
		String strBuf;
		/*if(textFile==null)
		{
			log.info("Empty folder");
			return null;
		}*/
		confBuf = new Configuration(textFile);
		/*if(confBuf.getNode("content")==null)
		{
			log.info("Wrong file format");
			return null;
		}*/
		confBuf.load();
		strBuf=confBuf.getString("Text");
		return strBuf;
	}
	
	private String getName(File textFile)
	{
		Configuration confBuf;
		String strBuf;
		confBuf = new Configuration(textFile);
		/*if(confBuf.getNode("content")==null)
		{
			log.info("Wrong file format");
			return null;
		}*/
		confBuf.load();
		strBuf=confBuf.getNode("Info").getString("Title");
		return strBuf;
	}
	
	
	public void addUser(String PlayerName)
	{
		HelpUser buf = new HelpUser();
		buf.setPlayer(PlayerName);
		buf.setLastTipic(null);
		Users.add(buf);
	}
	
	public void deleteUser(String PlayerName)
	{
		for(int i=0;i<Users.size();i++)
		{
			if(Users.get(i).getPlayer().equalsIgnoreCase(PlayerName))
			{
				Users.remove(i);
			}
		}
	}	
}
