package com.ryansteckler.nlpunbounce.helpers;

/**
 * Created by rsteckler on 9/5/14.
 */

import com.ryansteckler.nlpunbounce.models.AlarmStats;
import com.ryansteckler.nlpunbounce.models.EventLookup;
import com.ryansteckler.nlpunbounce.models.WakelockStats;

import java.util.Comparator;


public class SortWakeLocks {

//    public static Comparator<Entry<String, WakelockStats>> getMapComparator(final boolean byCount)
//    {
//        return new Comparator<Entry<String, WakelockStats>>()
//        {
//            public int compare(Entry<String, WakelockStats> o1,
//                               Entry<String, WakelockStats> o2)
//            {
//                if (byCount)
//                {
//                    return ((Long)o2.getValue().getAllowedCount()).compareTo(o1.getValue().getAllowedCount());
//                }
//                else
//                {
//                    return ((Long)o2.getValue().getAllowedDuration()).compareTo(o1.getValue().getAllowedDuration());
//                }
//            }
//        };
//    }

    public static Comparator<WakelockStats> getWakelockListComparator(final boolean byCount) {
        return getWakelockListComparator(byCount, true);
    }

    public static Comparator<WakelockStats> getWakelockListComparator(final boolean byCount, final boolean categorize)
    {
        return new Comparator<WakelockStats>()
        {
            public int compare(WakelockStats o1,
                               WakelockStats o2)
            {
                if (categorize)
                {
                    int blockingCompare = ((Boolean)o2.getBlockingEnabled()).compareTo(o1.getBlockingEnabled());
                    if (blockingCompare != 0)
                        return blockingCompare;

                    int safetyCompare = ((Integer)EventLookup.isSafe(o2.getName())).compareTo(EventLookup.isSafe(o1.getName()));
                    if (safetyCompare != 0)
                        return safetyCompare;

                    //The category state is the same.  Sub-compare
                    if (byCount)
                    {
                        return ((Long)o2.getAllowedCount()).compareTo(o1.getAllowedCount());
                    }
                    else
                    {
                        return ((Long)o2.getAllowedDuration()).compareTo(o1.getAllowedDuration());
                    }
                } else {
                    if (byCount)
                    {
                        return ((Long)o2.getAllowedCount()).compareTo(o1.getAllowedCount());
                    }
                    else
                    {
                        return ((Long)o2.getAllowedDuration()).compareTo(o1.getAllowedDuration());
                    }
                }

            }
        };
    }

    public static Comparator<AlarmStats> getAlarmListComparator() {
        return getAlarmListComparator(true);
    }

    public static Comparator<AlarmStats> getAlarmListComparator(final boolean categorize)
    {
        return new Comparator<AlarmStats>()
        {
            public int compare(AlarmStats o1,
                               AlarmStats o2)
            {
                if (categorize) {
                    int categoryCompare = ((Boolean)o2.getBlockingEnabled()).compareTo(o1.getBlockingEnabled());
                    if (categoryCompare == 0) {
                        //The enabled state is the same.  Sub-compare
                        return ((Long) o2.getAllowedCount()).compareTo(o1.getAllowedCount());
                    }
                    else {
                        return categoryCompare;
                    }
                } else {
                    return ((Long) o2.getAllowedCount()).compareTo(o1.getAllowedCount());
                }
            }
        };
    }

//    public static HashMap<String, WakelockStats> sortByComparator(HashMap<String, WakelockStats> unsortMap, final boolean byCount)
//    {
//
//        List<Entry<String, WakelockStats>> list = new LinkedList<Entry<String, WakelockStats>>(unsortMap.entrySet());
//
//        // Sorting the list based on values
//        Collections.sort(list, getMapComparator(byCount));
//
//        // Maintaining insertion order with the help of LinkedList
//        HashMap<String, WakelockStats> sortedMap = new LinkedHashMap<String, WakelockStats>();
//        for (Entry<String, WakelockStats> entry : list)
//        {
//            sortedMap.put(entry.getKey(), entry.getValue());
//        }
//
//        return sortedMap;
//    }
}
@@ -3208,24 +3208,25 @@
 tegorize) %7B%0A
+%0A
             
@@ -3237,24 +3237,24 @@
     int 
-category
+blocking
 Compare 
@@ -3354,24 +3354,24 @@
 if (
-category
+blocking
 Compare 
 == 0
@@ -3370,15 +3370,13 @@
 are 
-=
+!
 = 0)
- %7B
 %0A   
@@ -3400,53 +3400,32 @@
     
-//The enabled state is the same.  Sub-c
+return blockingC
 ompare
+;%0A
 %0A   
@@ -3445,135 +3445,208 @@
     
-    return ((Long) o2.getAllowedCount()).compareTo(o1.getAllowedCount());%0A                    %7D%0A                    else %7B%0A    
+int safetyCompare = ((Integer)EventLookup.isSafe(o2.getName())).compareTo(EventLookup.isSafe(o1.getName()));%0A                    if (safetyCompare != 0)%0A                        return safetyCompare;%0A%0A
     
@@ -3661,22 +3661,21 @@
         
-return
+//The
  categor
@@ -3675,24 +3675,48 @@
 category
-C
+ state is the same.  Sub-c
 ompare
-;
 %0A       
@@ -3720,33 +3720,100 @@
                 
-%7D
+return ((Long)o2.getAllowedCount()).compareTo(o1.getAllowedCount());
 %0A               

Fix Issue #50. Alarms sorting was messed up