/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pw.yumc.Residence.text.help;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 */
public class HelpEntry {
    protected static int linesPerPage = 7;
    protected String desc;
    protected String[] lines;
    protected String name;
    protected List<HelpEntry> subentrys;
    ResidenceMain plugin;

    public HelpEntry(final ResidenceMain plugin, final String entryname) {
        this.plugin = plugin;
        name = entryname;
        subentrys = new ArrayList<HelpEntry>();
        lines = new String[0];
    }

    public static int getLinesPerPage() {
        return linesPerPage;
    }

    public static HelpEntry parseHelp(final ResidenceMain plugin, final FileConfiguration node, final String key) {
        final String split[] = key.split("\\.");
        final String thisname = split[split.length - 1];
        final HelpEntry entry = new HelpEntry(plugin, thisname);
        final ConfigurationSection keysnode = node.getConfigurationSection(key);
        Set<String> keys = null;
        if (keysnode != null) {
            keys = keysnode.getKeys(false);
        }
        if (keys != null) {
            if (keys.contains("Info")) {
                final List<String> stringList = node.getStringList(key + ".Info");
                if (stringList != null) {
                    entry.lines = new String[stringList.size()];
                    for (int i = 0; i < stringList.size(); i++) {
                        entry.lines[i] = "- " + stringList.get(i);
                    }
                }
            }
            if (keys.contains("Description")) {
                entry.desc = node.getString(key + ".Description");
            }
            if (keys.contains("SubCommands")) {
                final Set<String> subcommandkeys = node.getConfigurationSection(key + ".SubCommands").getKeys(false);
                for (final String subkey : subcommandkeys) {
                    entry.subentrys.add(HelpEntry.parseHelp(plugin, node, key + ".SubCommands." + subkey));
                }
            }
        }
        return entry;
    }

    public static void setLinesPerPage(final int lines) {
        linesPerPage = lines;
    }

    public void addSubEntry(final HelpEntry entry) {
        if (!subentrys.contains(entry)) {
            subentrys.add(entry);
        }
    }

    public boolean containesEntry(final String name) {
        return this.getSubEntry(name) != null;
    }

    public String getDescription() {
        if (desc == null) {
            return "";
        }
        return desc;
    }

    public String getName() {
        if (name == null) {
            return "";
        }
        return name;
    }

    public HelpEntry getSubEntry(final String name) {
        final String[] split = name.split("\\.");
        HelpEntry entry = this;
        for (final String entryname : split) {
            entry = entry.findSubEntry(entryname);
            if (entry == null) {
                return null;
            }
        }
        return entry;
    }

    public int getSubEntryCount() {
        return subentrys.size();
    }

    public void printHelp(final CommandSender sender, final int page) {
        final List<String> helplines = this.getHelpData();
        final int pagecount = (int) Math.ceil((double) helplines.size() / (double) linesPerPage);
        if (page > pagecount || page < 1) {
            sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidHelp"));
            return;
        }
        sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("HelpPageHeader",
                ChatColor.YELLOW + name + ChatColor.RED + "." + ChatColor.YELLOW + page + ChatColor.RED + "." + ChatColor.YELLOW + pagecount + ChatColor.RED));
        sender.sendMessage(ChatColor.DARK_AQUA + plugin.getLanguage().getPhrase("Description") + ": " + ChatColor.GREEN + desc);
        final int start = linesPerPage * (page - 1);
        final int end = start + linesPerPage;
        boolean alternatecolor = false;
        for (int i = start; i < end; i++) {
            if (helplines.size() > i) {
                if (alternatecolor) {
                    sender.sendMessage(ChatColor.YELLOW + helplines.get(i));
                    alternatecolor = false;
                } else {
                    sender.sendMessage(ChatColor.GOLD + helplines.get(i));
                    alternatecolor = true;
                }
            }
        }
        if (page < pagecount) {
            sender.sendMessage(ChatColor.GRAY + "---<" + plugin.getLanguage().getPhrase("NextPage") + ">---");
        } else {
            sender.sendMessage(ChatColor.GRAY + "-----------------------");
        }
    }

    public void printHelp(final CommandSender sender, final int page, final String path) {
        final HelpEntry subEntry = this.getSubEntry(path);
        if (subEntry != null) {
            subEntry.printHelp(sender, page);
        } else {
            sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidHelp"));
        }
    }

    public void removeSubEntry(final HelpEntry entry) {
        if (subentrys.contains(entry)) {
            subentrys.remove(entry);
        }
    }

    public void setDescription(final String description) {
        desc = description;
    }

    public void setName(final String inname) {
        name = inname;
    }

    private HelpEntry findSubEntry(final String name) {
        for (final HelpEntry entry : subentrys) {
            if (entry.getName().equalsIgnoreCase(name)) {
                return entry;
            }
        }
        return null;
    }

    private List<String> getHelpData() {
        final List<String> helplines = new ArrayList<String>();
        helplines.addAll(Arrays.asList(lines));
        if (subentrys.size() > 0) {
            helplines.add(ChatColor.LIGHT_PURPLE + "---" + plugin.getLanguage().getPhrase("SubCommands") + "---");
        }
        for (final HelpEntry entry : subentrys) {
            helplines.add(ChatColor.GREEN + entry.getName() + ChatColor.YELLOW + " - " + entry.getDescription());
        }
        return helplines;
    }

}
