package com.bustiblelemons.cthulhator.creation.characteristics.logic;

import com.bustiblelemons.cthulhator.model.ActionGroup;
import com.bustiblelemons.cthulhator.model.CharacterProperty;

import java.util.Comparator;

/**
 * Created by bhm on 27.09.14.
 */
public enum CharacterPropertyComparators implements Comparator<CharacterProperty> {
    ALPHABETICAL {
        @Override
        public int compare(CharacterProperty lhs, CharacterProperty rhs) {
            if (lhs != null && rhs != null) {
                String lName = lhs.getName();
                String rName = rhs.getName();
                return lName.compareTo(rName);
            } else {
                return super.compare(lhs, rhs);
            }
        }
    },
    VALUE {
        @Override
        public int compare(CharacterProperty lhs, CharacterProperty rhs) {
            if (lhs != null && rhs != null) {
                int lVal = lhs.getValue();
                int rVal = rhs.getValue();
                if (lVal > rVal) {
                    return 1;
                } else if (lVal < rVal) {
                    return -1;
                } else {
                    return 0;
                }
            } else {
                return super.compare(lhs, rhs);
            }
        }
    }, ACTION_GROUP {
        @Override
        public int compare(CharacterProperty lhs, CharacterProperty rhs) {
            if (lhs != null && rhs != null) {
                ActionGroup lGroup = lhs.getMainActionGroup();
                ActionGroup rGroup = rhs.getMainActionGroup();
                if (lGroup != null && rGroup == null) {
                    return 1;
                } else if (lGroup == null && rGroup != null) {
                    return -1;
                } else if (lGroup == null && rGroup == null) {
                    return 0;
                } else {
                    int byGroup = lGroup.compareTo(rGroup);
                    return byGroup == 0 ? ALPHABETICAL.compare(lhs, rhs) : byGroup;
                }
            } else {
                return super.compare(lhs, rhs);
            }
        }
    };

    @Override
    public int compare(CharacterProperty lhs, CharacterProperty rhs) {
        if (lhs != null && rhs == null) {
            return 1;
        } else if (lhs == null && rhs != null) {
            return -1;
        } else {
            return 0;
        }
    }

}

@@ -2110,24 +2110,861 @@
 %7D%0A        %7D%0A
+    %7D, ACTION_BY_VALUE %7B%0A        @Override%0A        public int compare(CharacterProperty lhs, CharacterProperty rhs) %7B%0A            if (lhs != null && rhs != null) %7B%0A                ActionGroup lGroup = lhs.getMainActionGroup();%0A                ActionGroup rGroup = rhs.getMainActionGroup();%0A                if (lGroup != null && rGroup == null) %7B%0A                    return 1;%0A                %7D else if (lGroup == null && rGroup != null) %7B%0A                    return -1;%0A                %7D else if (lGroup == null && rGroup == null) %7B%0A                    return 0;%0A                %7D else %7B%0A                    int byGroup = lGroup.compareTo(rGroup);%0A                    return byGroup == 0 ? VALUE.compare(lhs, rhs) : byGroup;%0A                %7D%0A            %7D else %7B%0A                return super.compare(lhs, rhs);%0A            %7D%0A        %7D%0A%0A
     %7D;%0A%0A    

add sorting by group then value