package fr.epicanard.globalmarketchest.managers;

import java.util.Arrays;

import org.bukkit.inventory.ItemStack;

import fr.epicanard.globalmarketchest.GlobalMarketChest;
import fr.epicanard.globalmarketchest.database.querybuilder.ConditionType;
import fr.epicanard.globalmarketchest.database.querybuilder.builders.SelectBuilder;
import fr.epicanard.globalmarketchest.exceptions.EmptyCategoryException;
import fr.epicanard.globalmarketchest.utils.DatabaseUtils;
import fr.epicanard.globalmarketchest.utils.ItemStackUtils;

public enum GroupLevels {
  LEVEL3(3, null),
  LEVEL2(2, GroupLevels.LEVEL3),
  LEVEL1(1, GroupLevels.LEVEL2);

  private GroupLevels nextLevel;
  private Integer numLayers;

  GroupLevels(Integer layers, GroupLevels next) {
    this.nextLevel = next;
    this.numLayers = layers;
  }

  /**
   * Get the next level
   *
   * @param category Category name
   * @return GroupLevels found or null
   */
  public GroupLevels getNextLevel(String category) {
    Integer numberLevels = GlobalMarketChest.plugin.getCatHandler().getGroupLevels(category);

    if (this.nextLevel != null && this.numLayers < numberLevels)
      return this.nextLevel;
    return null;
  }

  /**
   * Configure the request depending on level and category groupLevels
   *
   * @param builder SelectBuilder to configure (add field, condition, etc ...)
   * @param category The auctions category
   * @param match ItemStack to match for research
   */
  public void configBuilder(SelectBuilder builder, String category, ItemStack match) throws EmptyCategoryException {
    Integer groupLevels = GlobalMarketChest.plugin.getCatHandler().getGroupLevels(category);

    switch (this) {
      case LEVEL1:
        this.level1(builder, groupLevels, match, category);
        break;
      case LEVEL2:
        this.level2(builder, groupLevels, match, category);
        break;
      case LEVEL3:
        this.level3(builder, groupLevels, match, category);
        break;
    }
  }

  /**
   * Configure the request for level 1 (First interface)
   * Configured depending on groupLevels of the category
   * GroupLevels affected : 3, 2 and 1
   *
   * @param builder SelectBuilder to configure (add field, condition, etc ...)
   * @param groupLevels GroupLevels of the category
   * @param category The auctions category
   * @param match ItemStack to match for research
   * @throws EmptyCategoryException
   */
  private void level1(SelectBuilder builder, Integer groupLevels, ItemStack match, String category) throws EmptyCategoryException {
    String[] items = GlobalMarketChest.plugin.getCatHandler().getItems(category);
    if (items.length == 0)
      throw new EmptyCategoryException(category);
    builder.addCondition("itemStack", Arrays.asList(items), (category.equals("!")) ? ConditionType.NOTIN : ConditionType.IN);
    switch (groupLevels) {
      case 3:
        builder.addField("COUNT(itemStack) AS count");
        builder.setExtension("GROUP BY itemStack");
        break;
      case 2:
        builder.addField("COUNT(itemMeta) AS count");
        builder.setExtension("GROUP BY itemMeta");
        break;
    }
  }

  /**
   * Configure the request for level 2 (Second interface)
   * Configured depending on groupLevels of the category
   * GroupLevels affected : 3 and 2
   *
   * @param builder SelectBuilder to configure (add field, condition, etc ...)
   * @param groupLevels GroupLevels of the category
   * @param category The auctions category
   * @param match ItemStack to match for research
   */
  private void level2(SelectBuilder builder, Integer groupLevels, ItemStack match, String category) {
    switch (groupLevels) {
      case 3:
        builder.addCondition("itemStack", ItemStackUtils.getMinecraftKey(match));
        builder.addField("COUNT(itemMeta) AS count");
        builder.setExtension("GROUP BY itemMeta");
        break;
      case 2:
        builder.addCondition("itemMeta", DatabaseUtils.serialize(match));
        break;
    }
  }

  /**
   * Configure the request for level 1 (Third interface)
   * Configured depending on groupLevels of the category
   * GroupLevels affected : 3
   *
   * @param builder SelectBuilder to configure (add field, condition, etc ...)
   * @param groupLevels GroupLevels of the category
   * @param category The auctions category
   * @param match ItemStack to match for research
   */
  private void level3(SelectBuilder builder, Integer groupLevels, ItemStack match, String category) {
    switch (groupLevels) {
      case 3:
        builder.addCondition("itemMeta", DatabaseUtils.serialize(match));
        break;
    }
  }

}
@@ -3103,32 +3103,124 @@
 %0A        break;%0A
+      case 1:%0A        builder.setExtension(%22ORDER BY price ASC, start ASC%22);%0A        break;%0A
     %7D%0A  %7D%0A%0A  /**
@@ -4031,32 +4031,95 @@
 ialize(match));%0A
+        builder.setExtension(%22ORDER BY price ASC, start ASC%22);%0A
         break;%0A 
@@ -4722,32 +4722,95 @@
 ialize(match));%0A
+        builder.setExtension(%22ORDER BY price ASC, start ASC%22);%0A
         break;%0A 

Fix #50 - Readd deleted price ordering with grouplevels