from .engine import SearchResults
from .engines import search_engines_dict
from . import output as out
from . import config as cfg


class MultipleSearchEngines(object):
    '''Uses multiple search engines.'''
    def __init__(self, engines, proxy=cfg.PROXY, timeout=cfg.TIMEOUT):
        self._engines = [
            se(proxy, timeout) 
            for se in search_engines_dict.values() 
            if se.__name__.lower() in engines
        ]
        self._filter = None

        self.ignore_duplicate_urls = False
        self.ignore_duplicate_domains = False
        self.results = SearchResults()

    def set_search_operator(self, operator):
        '''Filters search results based on the operator.'''
        self._filter = operator
    
    def search(self, query, pages=cfg.SEARCH_ENGINE_RESULTS_PAGES): 
        '''Searches multiples engines and collects the results.'''
        for engine in self._engines:
            engine.ignore_duplicate_urls = self.ignore_duplicate_urls
            engine.ignore_duplicate_domains = self.ignore_duplicate_domains
            if self._filter:
                engine.set_search_operator(self._filter)

            for item in engine.search(query, pages):
                if self.ignore_duplicate_urls and item['link'] in self.results.links():
                    continue
                if self.ignore_duplicate_domains and item['host'] in self.results.hosts():
                    continue
                self.results._results.append(item)
        return self.results
    
    def output(self, output=out.PRINT, path=None):
        '''Prints search results and/or creates report files.'''
        output = (output or '').lower()
        query = self._engines[0]._query if self._engines else u''
        if not path:
            path = cfg.OUTPUT_DIR + u'_'.join(query.split())
        out.console('')

        if out.PRINT in output:
            out.print_results(self._engines)
        if out.HTML in output:
            out.write_file(out.create_html_data(self._engines), path + u'.html') 
        if out.CSV in output:
            out.write_file(out.create_csv_data(self._engines), path + u'.csv') 
        if out.JSON in output:
            out.write_file(out.create_json_data(self._engines), path + u'.json')


class AllSearchEngines(MultipleSearchEngines):
    '''Uses all search engines.'''
    def __init__(self, proxy=cfg.PROXY, timeout=cfg.TIMEOUT):
        super(AllSearchEngines, self).__init__(
            list(search_engines_dict), proxy, timeout
        )

