#!/usr/bin/env python
# future
# standard lib
# third-party
# memlingo
import memlingo.yandex as yandex
# local
# try/except


class WordDictionary:
    '''WordDictionary contains all your known words from a given language.

    It is a simple dictionary that maps <word, card>.

    Attributes
    ----------
    lang: str
        Language of the current dictionary.
        String must include the target translation, due to Yandex API in form:
            "ru-en"
            "jp-en"
    ID: int
        This unique identifier is used by the genanki library. It is needed
        because Anki decks, have a unique identifier that can be referred to,
        and is important when updating the deck.
    words: dict <str, Card>
        Contains all the words and relevant information
    '''

    class Card:
        '''Card holds the relevant information on a word.

        Cards hold the required information to build an AnkiCard.`

        Attributes
        ----------
        word: str
            The word itself. How it is written. Equal to the dictionary key.
        word_class: str
            The class of the word. Can be noun, adjective, adverb, etc.
        translations: [str]
            A list of all possible translations of the word.
        examples: [(str,str)]
            A list of tuples, that contain pairs of translated sentences.
        dirty_bit: int (0,1)
            Used to discern if it's needed to export again.
        '''

        def __init__(self, word, wclass, translations, examples, bit=0):
            self.word = word
            self.word_class = wclass
            self.translations = translations
            self.examples = examples
            self.dirty_bit = bit

    def __init__(self, language, uniqueID):
        self.lang = language
        self.ID = uniqueID
        self.words = {}

    # TODO add_card shouldn't receive an api key
    def add_card(self, api_key, word):
        '''Adds a new word to the dictionary.
        All information on a word is fetched - translations, examples, etc.
        In the future, IPA transcribing, and sound files will be available.

        Parameters
        ----------
        api_key: str
            The Yandex Api Key required to lookup a word.
        word: str
            The word that the user wants to add to the collection.

        Returns
        -------
        Nil

        Side-Effects
        ------------
        Searches for word, gathers relevant information, and then adds the card
        to the WordDictionary.
        '''

        # Word must be encoded to be stored in a valid format (e.g.: Russian,
        # Japanese)
        utf8word = word.encode('utf-8')
        if utf8word in self.words:
            # Panic
            print("That word as already been added, skipping...\n")
            return
        jsonie = yandex.lookup(api_key, self.lang, word)
        if jsonie is not None:
            word = yandex.get_word(jsonie)
            word_class = yandex.get_word_class(jsonie)
            translations = yandex.get_translations(jsonie)
            examples = yandex.get_examples(jsonie)

            new_card = self.Card(word, word_class, translations, examples)
            self.words[utf8word] = new_card
        return

    # TODO this shouldn't use api_key as well
    def update(self, api_key, words):
        '''Update the WordDictionary with given list of words.

        Parameters
        ----------
        api_key: str
            The Yandex Api Key required to lookup a word.
        words: [str]
            A list of words that the user wants to add to the collection.

        Returns
        -------
        Nil

        Side-Effects
        ------------
        Adds every word that is on Yandex, to the user collection.
        '''
        word_counter = 0
        for word in words:
            self.add_card(api_key, word)
            word_counter += 1
        print(str(word_counter) + " words added to collection\n")
        return

    def to_print(self):
        '''Prints the dictioanry in a pretty manner.

        Prints the queried word, wordclass, translations and example
        '''

        for key in self.words:
            print(key.decode('utf-8'))
            print(self.words[key].word_class)
            print(self.words[key].translations)
            print(self.words[key].examples)
            print(self.words[key].dirty_bit)
            print('\n')


if __name__ == "__main__":
    newDick = WordDictionary("ru", 12345)
