#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
from scholarly_citation_finder.apps.core.models import Publication
from .Strategy import Strategy

logger = logging.getLogger(__name__)


class FieldofstudyStrategy(Strategy):


    def __init__(self, ordered=False, limit=None):
        name = 'fieldofstudy'
        if ordered:
            name += '-ordered'
        if limit > 0:
            name += '-limit'
        super(FieldofstudyStrategy, self).__init__(name=name)
        self.ordered = ordered
        self.limit = limit
        
    def run(self, publication_set, callback):
        fieldofstudies = publication_set.get_fieldofstudies(self.ordered)
        if self.limit > 0:
            fieldofstudies = fieldofstudies[:self.limit]
        logger.info('found {} (limit: {}) field of studies in search set'.format(len(fieldofstudies), self.limit))
        fieldofstudies_publications = self.__find_fieldofstudies_publications(fieldofstudies, publication_set.get_min_year())
        callback(fieldofstudies_publications, 'field of studies (limit={})'.format(self.limit))
        #for fieldofstudy in fieldofstudies:
        #    fieldofstudy_publications = self.__find_fieldofstudy_publications(fieldofstudy.id, publication_set.get_min_year())
        #    fieldofstudy_publications_citing = citing_papers.filter(publication__in=fieldofstudy_publications)
        #    logger.info('field of study "{}": found {} publications (year >= {}), {} citations'.format(fieldofstudy, len(fieldofstudy_publications), publication_set.get_min_year(), len(fieldofstudy_publications_citing)))  
        #    citation_set.add(fieldofstudy_publications_citing, len(fieldofstudy_publications))

    def __find_fieldofstudies_publications(self, fieldofstudies, min_year):
        return Publication.objects.using(self.database).filter(publicationkeyword__fieldofstudy__in=fieldofstudies).filter(year__gte=min_year).distinct()

        
    #def __find_fieldofstudy_publications(self, fieldofstudy_id, min_year):
        #return list(Publication.objects.using(self.database).raw("SELECT DISTINCT publication_id AS id from core_publicationkeyword WHERE fieldofstudy_id=%s", [fieldofstudy_id]))
    #    return Publication.objects.using(self.database).filter(publicationkeyword__fieldofstudy_id=fieldofstudy_id).filter(year__gte=min_year).distinct()

@@ -227,17 +227,16 @@
 tegy):%0A%0A
-%0A
     def 
@@ -264,16 +264,32 @@
 d=False,
+ min_year=False,
  limit=N
@@ -375,16 +375,68 @@
 rdered'%0A
+        if min_year:%0A            name += '-minyear'%0A
         
@@ -571,16 +571,49 @@
 ordered%0A
+        self.min_year = min_year%0A
         
@@ -950,32 +950,110 @@
 s), self.limit))
+%0A%0A        min_year = publication_set.get_min_year() if self.min_year else None
 %0A        fieldof
@@ -1135,36 +1135,25 @@
 es, 
-publication_set.get_
+min_year=
 min_year
 ())%0A
@@ -1140,34 +1140,32 @@
 in_year=min_year
-()
 )%0A        callba
@@ -1215,16 +1215,26 @@
 tudies (
+year%3E=%7B%7D, 
 limit=%7B%7D
@@ -1243,16 +1243,26 @@
 .format(
+min_year, 
 self.lim
@@ -1937,32 +1937,37 @@
 tudies, min_year
+=None
 ):%0A        retur
@@ -1957,30 +1957,31 @@
 e):%0A        
-return
+query =
  Publication
@@ -2069,16 +2069,63 @@
 studies)
+%0A        if min_year:%0A            query = query
 .filter(
@@ -2135,32 +2135,53 @@
 r__gte=min_year)
+%0A        return query
 .distinct()%0A%0A   

make min_year optional