package de.fau.cs.mad.kwikshop.android.util;

import java.util.Comparator;

import de.fau.cs.mad.kwikshop.common.Item;
import de.fau.cs.mad.kwikshop.android.view.DisplayHelper;
import de.fau.cs.mad.kwikshop.android.view.ItemSortType;

public class ItemComparator implements Comparator<Item> {

    private final ItemSortType comparatorType;
    private final DisplayHelper displayHelper;


    public ItemComparator(DisplayHelper displayHelper, ItemSortType comparatorType) {

        if (displayHelper == null) {
            throw new IllegalArgumentException("'displayHelper' must not be null");
        }

        this.comparatorType = comparatorType;
        this.displayHelper = displayHelper;
    }

    @Override
    public int compare(Item item1, Item item2)
    {
        int res = 0;

        switch(comparatorType) {
            case BOUGHTITEMS:
                if(item1.isBought() && !item2.isBought())
                    res = 1;
                if(item2.isBought() && !item1.isBought())
                    res = -1;
                break;

            case MANUAL:
                res = item1.getOrder() - item2.getOrder();
                break;
            case GROUP:

                int groupOrder = displayHelper.getDisplayName(item1.getGroup())
                        .compareTo(displayHelper.getDisplayName(item2.getGroup()));

                //if groups are identical, sort alphabetically
                res = groupOrder;
                if(res != 0) break;


            case ALPHABETICALLY:

                int alphabetOrder = item1.getName().compareTo(item2.getName());

                //if the items have the same name, sort by item order
                res = alphabetOrder;
                if(res != 0) break;

            default:
                res = item1.getOrder() - item2.getOrder();
        }

        return res;
    }


}

@@ -1394,27 +1394,36 @@
 al, 
-sort alphabetically
+use the saved order (manual)
 %0A   
@@ -1468,37 +1468,137 @@
          if(res 
-!
+=
 = 0)
+ %7B%0A                    res = item1.getOrder() - item2.getOrder();%0A                %7D%0A%0A               
  break;%0A%0A%0A      
@@ -1590,17 +1590,16 @@
 break;%0A%0A
-%0A
         

Use saved drag and drop order when sorting by group