from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import time

# import visitor.sensitive_content
import visitor.stopwords
import config
import stanford_corenlp.wrapper
import strfilters.language_filter
import strfilters.whitespace_inserter
from pattern.singleton import Singleton


class Scene(object):
    """
    To preload large components and persist in memory.
    """
    __metaclass__ = Singleton

    def __init__(self):
        logger = logging.getLogger()

        logger.info("[Scene] First time to load scene, initializing...")
        logger.info("[Loading] loading components...")

        logger.info("[Loading] whitespace inserter...")
        sorted_words_path = config.Values.sorted_words
        self.whitespace_inserter = (
            strfilters.whitespace_inserter.WhitespaceInserter(sorted_words_path))
        logger.info("[Finished] Whitespace inserter loaded.")

        logger.info("[Loading] loading corpus resources...")

        logger.info("[Finished] Corpus resources loaded.")

        similarity_processor_stopwords_path = config.Values.stopwords_path
        self.similarity_processor_stopwords = visitor.stopwords.get_stopwords(
            similarity_processor_stopwords_path)


        logger.info("[Loading] loading Stanford Core NLP Parser...")
        self.stanford_nlp_parser = (
            stanford_corenlp.wrapper.StanfordCoreNLPWrapper(
                config.Values.stanford_corenlp_dir))
        logger.info("[Finished] Stanford Core NLP Parser loaded.")
        logger.info("[Finished] all components loaded.")

    def restart_stanford_nlp_parser(self):
        logger = logging.getLogger(__name__)
        logger.info("[Restart] Restart Stanford Core NLP Parser.")
        del self.stanford_nlp_parser
        time.sleep(10)
        self.stanford_nlp_parser = (
            stanford_corenlp.wrapper.StanfordCoreNLPWrapper(
                config.Values.stanford_corenlp_dir))
