package com.aegir.demo.model;

import java.util.Comparator;

public class BaseFloorItemComparator implements Comparator<BaseFloorItem> {

    @Override
    public int compare(BaseFloorItem t0, BaseFloorItem t1) {
        //both buildings
        if (t0.isBuilding() && t1.isBuilding()) {
            return t0.getNumber().compareTo(t1.getNumber());
        }
        //both floors
        else if (!t0.isBuilding() && !t1.isBuilding()) {
            FloorItem f0 = (FloorItem) t0;
            FloorItem f1 = (FloorItem) t1;
            int c = f0.getBuildingNumber().compareTo(f1.getBuildingNumber());
            if (c != 0) return c;
            c = f0.getNumber().compareTo(f1.getNumber());
            if (c != 0) return c;
            String n0 = f0.getName() == null ? "" : f0.getName();
            String n1 = f1.getName() == null ? "" : f1.getName();
            c = String.CASE_INSENSITIVE_ORDER.compare(n0, n1);
            if (c != 0) return c;
            String k0 = f0.getKey() == null ? "" : f0.getKey();
            String k1 = f1.getKey() == null ? "" : f1.getKey();
            return k0.compareTo(k1);
        } else if (t0.isBuilding() && !t1.isBuilding()) {
            FloorItem f1 = (FloorItem) t1;
            return t0.getNumber().compareTo(f1.getBuildingNumber());
        } else if (!t0.isBuilding() && t1.isBuilding()) {
            FloorItem f0 = (FloorItem) t0;
            return f0.getBuildingNumber().compareTo(t1.getNumber());
        }
        return 0;
    }

}
