# -*- coding: utf-8 -*-
import general_manager as gen_man
import np_adjp_manager as np_adjp_man
import orth_type_case_manager as otc_manager


class SecondTypePredManager(gen_man.GeneralManager):
    '''
    Manager predykatow drugiego typu
    '''

    def __init__(self, np_adjp_phrase):
        gen_man.GeneralManager.__init__(self, np_adjp_phrase)

    # -----------------------------------------------
    # metody operujace na pozycjach wzgledem poczatku frazy, nie zdania
    # 0 oznacza pozycje - poczatek - wzgledem frazy, nie wzgledem zdania
    def is_pred_type2_at(self, position):
        return self.is_inside_ann_phrase(
            position) and self.is_pred_type2_at_pos(position)

    def is_pred_type2_at_pos(self, position):
        return self.np_adjp().segments()[
                   position] in self.np_adjp().predicates_type2()

    def get_left_second_type_predicate(self, position):
        if self.is_outside_ann_phrase(position):
            return None
        for pos_seg in range(position - 1, -1, -1):
            if self.is_pred_type2_at_pos(pos_seg):
                return pos_seg
        return None

    def get_left_second_type_predicate_outside_agp(self, position):
        if self.is_outside_ann_phrase(position):
            return None
        npadjpman = np_adjp_man.NpAdjpManager(self.np_adjp())
        agp_pos = npadjpman.get_agp_at(position)
        for pos_seg in range(position - 1, -1, -1):
            if self.is_pred_type2_at_pos(pos_seg):
                agp_act = npadjpman.get_agp_at(pos_seg)
                if agp_act == agp_pos:
                    continue
                return pos_seg
        return None

    def get_right_second_type_predicate(self, position):
        if self.is_outside_ann_phrase(position):
            return None
        for pos_seg in range(position + 1, len(self.np_adjp().segments())):
            if self.is_pred_type2_at_pos(pos_seg):
                return pos_seg
        return None

    def get_right_second_type_predicate_outside_agp(self, position):
        if self.is_outside_ann_phrase(position):
            return None
        npadjpman = np_adjp_man.NpAdjpManager(self.np_adjp())
        agp_pos = npadjpman.get_agp_at(position)
        for pos_seg in range(position + 1, len(self.np_adjp().segments())):
            if self.is_pred_type2_at_pos(pos_seg):
                agp_act = npadjpman.get_agp_at(pos_seg)
                if agp_act == agp_pos:
                    continue
                return pos_seg
        return None

    def get_right_second_type_predicate_outside_agp_not_same_lemma(self,
                                                                   position,
                                                                   tagset):
        '''
        Zwraca pierwsza pozycje predykatu drugiego typu po prawej, taka, ze:
            predykat znajduje sie poza aktualna fraza agp/pp
            ten predykat nie wystepuje w zadnym wyrazeniu przyimkowym z prawej strony
        '''
        if self.is_outside_ann_phrase(position):
            return None
        otcman = otc_manager.OrthTypeCaseManager(self.np_adjp())
        npadjpman = np_adjp_man.NpAdjpManager(self.np_adjp())
        agp_pos = npadjpman.get_agp_at(position)
        for pos_seg in range(position + 1, len(self.np_adjp().segments())):
            if self.is_pred_type2_at_pos(pos_seg):
                agp_act = npadjpman.get_agp_at(pos_seg)
                if agp_act == agp_pos:
                    continue
                # sprawdz czy na prawo nie znajduje sie lemat zgodny z lematem pod pos_seg:
                pos_seg_orth = otcman.get_orth_at_pos(pos_seg, tagset)
                found = False
                for pos_seg_check in range(pos_seg + 1,
                                           len(self.np_adjp().segments())):
                    pos_seg_check_orth = otcman.get_orth_at_pos(pos_seg_check,
                                                                tagset)
                    if pos_seg_check_orth == pos_seg_orth:
                        if npadjpman.get_agp_at(
                                pos_seg_check) and npadjpman.get_agp_at(
                                pos_seg_check).is_pp():
                            found = True
                            break
                if not found:
                    return pos_seg
        return None

    def is_any_right_second_type_predicate(self, position):
        return self.get_right_second_type_predicate(position) != None

    def is_any_left_second_type_predicate(self, position):
        return self.get_left_second_type_predicate(position) != None
