class ChordSelector implements IChordSelector {

    @Override
    public HashMap<ProgressionType, List<HiveChord>> selectChordsFrom4BarLoop(List<ProgressionUnit> progressionUnits) {

        HashMap<ProgressionType, List<HiveChord>> typeToHiveChords = new HashMap<>();

        for (ProgressionUnit progressionUnit : progressionUnits) {
            if (typeToHiveChords.get(progressionUnit.getType()) == null) {

                HiveChord[] fourBarChordList = new HiveChord[4];

                HarmonicLoop loop = progressionUnit.getProgressionUnitBars().get(0).getHarmonicLoop();

                List<HiveChord> chordsInBar = progressionUnit.getProgressionUnitBars().stream().map(pBar -> pBar.getChord()).filter(chord -> chord != null).collect(Collectors.toList());

                List<HiveChord> compatibleChords = new ArrayList<>();

                for (HiveChord chord : chordsInBar) {
                    for (HiveChord loopChord : loop.getChords()) {
                        if (chord.isEqualOrTriadMatch(loopChord)) {
                            compatibleChords.add(chord);
                        }
                    }
                }

                for (Integer i = 0; i < progressionUnit.getProgressionUnitBars().size(); i++) {
                    HiveChord currentChord = progressionUnit.getProgressionUnitBars().get(i).getChord();

                    if (i.equals(0) || compatibleChords.isEmpty()) {
                        fourBarChordList[i] = currentChord;
                    } else {
                        fourBarChordList[i] = compatibleChords.get(new Random().nextInt(compatibleChords.size()));
                    }
                }

                typeToHiveChords.put(progressionUnit.getType(), Arrays.asList(fourBarChordList));
            }
        }

        return typeToHiveChords;
    }

}