#-*- coding:utf-8 -*-


from django.core.management.base import BaseCommand

from dictionary.models import Lemma

class Command(BaseCommand):
    args = 'none'
    help = ""

    def handle(self, **options):
        find_hanging_connections()
        
def find_hanging_connections():
    lemmas = Lemma.objects.filter(old=False).order_by('entry_obj__name')
    for lemma in lemmas:
        frames = lemma.entry_obj.visible_frames()
        for frame in frames:
            for compl in frame.complements.all():
                for real in compl.realizations.all():
                    match = False
                    matching_schemata = lemma.frames.filter(id=real.frame.id).all()
                    for schema in matching_schemata:
                        matching_poss = schema.positions.filter(id=real.position.id,
                                                                arguments=real.argument)
                        if matching_poss.exists():
                            match = True
                            break
                    if not match:
                        compl_ref = 'frame_%d_comp_%d_' % (frame.id, compl.id)
                        print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
                        print 'lemma: %s\tcomplement: %s\trealization: %s' % (lemma.entry_obj.name,
                                                                              compl_ref,
                                                                              unicode(real))
