class KeyExtractor {

    public String extract(List<String> chords){

        List<String> potentialKeys = Arrays.asList("a","a#","b", "c","c#", "d","d#", "e", "f","f#", "g", "g#");
        Map<String, Integer> keyScore = new HashMap<>();

        for (String key : potentialKeys) {
            keyScore.put(key, 0);
        }

        for(String key: potentialKeys){

            List<HiveChord> chordsInKey = getChordsInKey(key);

            for(String chord: chords){
                HiveChord hiveChord = new HiveChord(chord);

                for (HiveChord hChordInKey : chordsInKey) {
                    if(hiveChord.getChordName().equalsIgnoreCase(hChordInKey.getChordName())){
                        keyScore.put(key, keyScore.get(key)+1);
                    }
                }
            }
        }

        System.out.println(keyScore);

        String keyWithHighestScore = "";
        int currentScore = -1;

        for(String key: potentialKeys){
            if(keyScore.get(key) > currentScore){
                keyWithHighestScore = key;
                currentScore = keyScore.get(key);
            }
        }

        List<String> highestScores = new ArrayList<>();
        for(String key: potentialKeys){
            if(keyScore.get(key) == currentScore){
                highestScores.add(key);
            }
        }

        if(highestScores.size() == 1){
            return highestScores.get(0);
        }

        //if there are multiple keys match the one where the tonic is first chord
        for (String key : highestScores) {
            if(chords.get(0).equalsIgnoreCase(key+"maj") ||
                    chords.get(0).equalsIgnoreCase(key + "maj7")){
                return key;
            }
        }

        return keyWithHighestScore;
    }

}