package com.charego.lingo.client.bootstrap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.charego.lingo.common.WordReader;

import javax.annotation.PostConstruct;

@Component
public class WordRepository {

    private final Map<Integer, Set<String>> wordMap = new HashMap<>();
    private final Map<Integer, List<String>> commonWordMap = new HashMap<>();

    @PostConstruct
    private void postConstruct() throws IOException {
        final Set<String> words5 = WordReader.readFileToSet("/SOWPODS2007/words5.txt");
        final Set<String> words6 = WordReader.readFileToSet("/SOWPODS2007/words6.txt");
        final List<String> words5Common = WordReader.readFileToList("/SOWPODS2007/words5-common.txt");
        final List<String> words6Common = WordReader.readFileToList("/SOWPODS2007/words6-common.txt");

        wordMap.put(5, words5);
        wordMap.put(6, words6);
        commonWordMap.put(5, words5Common);
        commonWordMap.put(6, words6Common);
    }

    public Set<String> getWords(int length) {
        return Collections.unmodifiableSet(wordMap.get(length));
    }

    public List<String> getCommonWords(int length) {
        return new ArrayList<>(commonWordMap.get(length));
    }

}
