# Path: zdict/models.py
# class Record(peewee.Model):
#     '''
#     A model for storing the query results into the SQLite db.
# 
#     :param word: the vocabulary
#     :param content: the query result of the vocabulary.
#         It's a json document has the following spec.
#         {
#             'word': word,
#             // storing the querying result.
#             'pronounce': [
#                 ('key', 'value'),
#                 ...
#             ],
#             'sound': [
#                 ('type', 'url'),
#                 ...
#                 // type: (mp3|ogg)
#             ],
#             'explain': [
#                 ('speech',
#                     (
#                         'meaning',
#                         ('sentence1', 'translation'),
#                         ...
#                     ),
#                     ...
#                 ),
#                 ...
#             ]
#         }
#     :param source: source of the content. May be Yahoo!, Google, ... Dict
#     '''
# 
#     word = peewee.TextField()
#     content = peewee.TextField()
#     source = peewee.CharField()
# 
#     class Meta:
#         database = db
#         primary_key = peewee.CompositeKey('word', 'source')
# 
# Path: zdict/utils.py
# class Color(metaclass=ColorConst):
#     _force_color = False
# 
#     @classmethod
#     def set_force_color(cls, force_color=True):
#         cls._force_color = force_color
# 
#     @classmethod
#     def format(self, s='', color='org', indent=0):
#         '''
#         :type s: str
#         :param s: message
#         :param color: predefined color name, e,g,: red, RED.
#             Using 'l' prefix for bright color, e.g.: lred, lwhite.
#             It's case-insensitive.
# 
#             If stdout isn't a tty, the color option will be ignored.
#         '''
#         colorize = self._force_color or sys.stdout.isatty()
# 
#         return '{indent}{color}{s}{org}'.format(
#             indent=' ' * indent,
#             color=getattr(self, color, '') if colorize else '',
#             s=s,
#             org=self.ORG if colorize else '',
#         )
# 
#     @classmethod
#     def print(self, *args, end='\n', **kwargs):
#         print(self.format(*args, **kwargs), end=end)


import re
import random
import importlib
import importlib.util
from zdict.models import Record
from zdict.utils import Color



def import_pyjokes_module():
    if importlib.util.find_spec('pyjokes'):
        return importlib.import_module('pyjokes')


def get_pyjoke(pyjokes, word: str):
    if not pyjokes:
        return

    try:
        # very basic string searching in jokes
        r = random.choice(
            list(filter(
                lambda j: word in map(
                    lambda x: ''.join(c for c in x if c.isalnum()),
                    j.split()
                ),
                pyjokes.get_jokes()
            ))
        )
    except IndexError:
        return
    else:
        return Record(word=word, content=r, source='pyjokes')



# Path: zdict/models.py
# class Record(peewee.Model):
#     '''
#     A model for storing the query results into the SQLite db.
# 
#     :param word: the vocabulary
#     :param content: the query result of the vocabulary.
#         It's a json document has the following spec.
#         {
#             'word': word,
#             // storing the querying result.
#             'pronounce': [
#                 ('key', 'value'),
#                 ...
#             ],
#             'sound': [
#                 ('type', 'url'),
#                 ...
#                 // type: (mp3|ogg)
#             ],
#             'explain': [
#                 ('speech',
#                     (
#                         'meaning',
#                         ('sentence1', 'translation'),
#                         ...
#                     ),
#                     ...
#                 ),
#                 ...
#             ]
#         }
#     :param source: source of the content. May be Yahoo!, Google, ... Dict
#     '''
# 
#     word = peewee.TextField()
#     content = peewee.TextField()
#     source = peewee.CharField()
# 
#     class Meta:
#         database = db
#         primary_key = peewee.CompositeKey('word', 'source')
# 
# Path: zdict/utils.py
# class Color(metaclass=ColorConst):
#     _force_color = False
# 
#     @classmethod
#     def set_force_color(cls, force_color=True):
#         cls._force_color = force_color
# 
#     @classmethod
#     def format(self, s='', color='org', indent=0):
#         '''
#         :type s: str
#         :param s: message
#         :param color: predefined color name, e,g,: red, RED.
#             Using 'l' prefix for bright color, e.g.: lred, lwhite.
#             It's case-insensitive.
# 
#             If stdout isn't a tty, the color option will be ignored.
#         '''
#         colorize = self._force_color or sys.stdout.isatty()
# 
#         return '{indent}{color}{s}{org}'.format(
#             indent=' ' * indent,
#             color=getattr(self, color, '') if colorize else '',
#             s=s,
#             org=self.ORG if colorize else '',
#         )
# 
#     @classmethod
#     def print(self, *args, end='\n', **kwargs):
#         print(self.format(*args, **kwargs), end=end)


# Path: zdict/easter_eggs.py
import re
import random
import importlib
import importlib.util
from zdict.models import Record
from zdict.utils import Color




def import_pyjokes_module():
    if importlib.util.find_spec('pyjokes'):
        return importlib.import_module('pyjokes')


def get_pyjoke(pyjokes, word: str):
    if not pyjokes:
        return

    try:
        # very basic string searching in jokes
        r = random.choice(
            list(filter(
                lambda j: word in map(
                    lambda x: ''.join(c for c in x if c.isalnum()),
                    j.split()
                ),
                pyjokes.get_jokes()
            ))
        )
    except IndexError:
        return
    else:
        return Record(word=word, content=r, source='pyjokes')



def show_pyjoke(record: Record):