from __future__ import unicode_literals, division, absolute_import
import logging

from flexget import plugin
from flexget.event import event
from flexget.plugins.filter.seen import FilterSeen

log = logging.getLogger('seenmovies')


class FilterSeenMovies(FilterSeen):
    """
        Prevents movies being downloaded twice.
        Works only on entries which have imdb url available.

        How duplicate movie detection works:
        1) Remember all imdb urls from downloaded entries.
        2) If stored imdb url appears again, entry is rejected.
    """
    schema = {'type': 'string', 'enum': ['strict', 'loose']}

    def __init__(self):
        # remember and filter by these fields
        self.fields = ['imdb_id', 'tmdb_id']
        self.keyword = 'seen_movies'

    # We run last (-255) to make sure we don't reject duplicates before all the other plugins get a chance to reject.
    @plugin.priority(-255)
    def on_task_filter(self, task, config):
        # strict method
        if config == 'strict':
            for entry in task.entries:
                if 'imdb_id' not in entry and 'tmdb_id' not in entry:
                    log.info('Rejecting %s because of missing movie (imdb or tmdb) id' % entry['title'])
                    entry.reject('missing movie (imdb or tmdb) id, strict')
        # call super
        super(FilterSeenMovies, self).on_task_filter(task, True)
        # check that two copies of a movie have not been accepted this run
        imdb_ids = set()
        tmdb_ids = set()
        for entry in task.accepted:
            if 'imdb_id' in entry:
                if entry['imdb_id'] in imdb_ids:
                    entry.reject('already accepted once in task')
                    continue
                else:
                    imdb_ids.add(entry['imdb_id'])
            if 'tmdb_id' in entry:
                if entry['tmdb_id'] in tmdb_ids:
                    entry.reject('already accepted once in task')
                    continue
                else:
                    tmdb_ids.add(entry['tmdb_id'])

@event('plugin.register')
def register_plugin():
    plugin.register(FilterSeenMovies, 'seen_movies', api_ver=2)

@@ -571,16 +571,49 @@
 hema = %7B
+%0A        'oneOf': %5B%0A            %7B
 'type': 
@@ -650,16 +650,350 @@
 loose'%5D%7D
+,%0A            %7B%0A                'type': 'object',%0A                'properties': %7B%0A                    'scope': %7B'type': 'string', 'enum': %5B'global', 'local'%5D%7D,%0A                    'matching': %7B'type': 'string', 'enum': %5B'strict', 'loose'%5D%7D%0A                %7D,%0A                'additionalProperties': False%0A            %7D%0A        %5D%0A    %7D
 %0A%0A    de
@@ -1340,23 +1340,119 @@
     
-# strict method
+if not isinstance(config, dict):%0A            config = %7B'matching': config%7D%0A        # Reject all entries without
 %0A   
@@ -1465,16 +1465,32 @@
 f config
+.get('matching')
  == 'str
@@ -1557,57 +1557,56 @@
  if 
-'imdb_id' not in entry and 'tmdb_id' not in entry
+not any(field in entry for field in self.fields)
 :%0A  
@@ -1867,14 +1867,35 @@
 ask,
+ config.get('scope',
  True)
+)
 %0A   

Allow 'local' mode for seen_movies