class ChordSelector implements IChordSelector {

    @Override
    public HiveChord selectMiddleChord(HarmonicLoop selectedLoop, HiveChord chordOneOfFour, HiveChord chordOneOfNextBar, List<HiveChord> hiveChordInDatabase) {
        //TODO ADD LOGIC TO MAKE A SMART DECISION ...
        //TODO CONSIDER chordOneOfFour & chordOneOfNextBar ..

        List<HiveChord> compatibleChords = selectedLoop.getValidChords();

        if (compatibleChords == null || compatibleChords.size() == 0) {
            return null;
        }


        for (int i = 0; i < compatibleChords.size(); i++) {

            HiveChord compChord = compatibleChords.get(i);

            for (int j = 0; j < hiveChordInDatabase.size(); j++) {

                HiveChord dbChord = hiveChordInDatabase.get(j);

                if ((chordOneOfFour != null && chordOneOfNextBar != null) && (j > 0 && j < hiveChordInDatabase.size() - 1) && dbChord.isEqualOrTriadMatch(compChord) &&
                        hiveChordInDatabase.get(j - 1).isEqualOrTriadMatch(chordOneOfFour) &&
                        hiveChordInDatabase.get(j + 1).isEqualOrTriadMatch(chordOneOfNextBar)) {
                    return compChord;
                } else if ((chordOneOfFour != null) && (j > 0) && dbChord.isEqualOrTriadMatch(compChord) &&
                        hiveChordInDatabase.get(j - 1).isEqualOrTriadMatch(chordOneOfFour)) {
                    return compChord;
                } else if ((chordOneOfNextBar != null) && (j < hiveChordInDatabase.size() - 1) && dbChord.isEqualOrTriadMatch(compChord) &&
                        hiveChordInDatabase.get(j + 1).isEqualOrTriadMatch(chordOneOfNextBar)) {
                    return compChord;
                }
            }
        }

        int index = new Random().nextInt(compatibleChords.size());
        return compatibleChords.get(index);
    }

}