"""Moduł pomocniczy do tworzenia obiektu forest_map_no_sf"""
 
from Mapa_PU.swdl.model.swdl.forest_map_no_sf import ForestMapNoSF,CountyLayer,BasicObjectLayer,LabelsLayer,LineLayer  
from Mapa_PU.swdl.kontroler.geometry_helper import GeometryHelper
from Mapa_PU.swdl.model.tags import TagsLayer


class ForestMapNoSFHelper():
    """Klasa pomocnicza do tworzenia obiektów forest_map_no_sf"""
    
    def __init__(self,geometry_helper: GeometryHelper ,path: str):
        self.__geometry_helper=geometry_helper
        self.__path=path
        
    
    def create_forest_map_no_sf(self,address: str):
        """Metoda tworząca obiekt forest_map_no_sf"""
        forestMapNoSf=ForestMapNoSF()
        #Warstwa oddziałów
        forestMapNoSf.county_layer=self.create_county_layer(address)
        #Warstwa obiektów podstawowych 
        forestMapNoSf.basic_objects_layer=self.create_basic_objects_layer(address)
        #Warstwa opisów pododdziałów
        forestMapNoSf.subarea_labels_layer=self.create_subarea_labels_layer(address)
        #Warstwa opisów oddziałów
        forestMapNoSf.county_labels_layer=self.create_county_labels_layer(address)
        #Warstwa obiektów liniowych
        forestMapNoSf.line_layer=self.create_line_layer()
        return forestMapNoSf
    
    def create_county_layer(self,address: str)->CountyLayer:
        """Tworzenie obiektów listy oddziałów"""
        try:
            #Normalizacja adresu do oddziału
            if len(address)>17:
                address=address[0:17]
#             sql="select ADR_BDL from ODDZIAL where ADR_BDL like '{}%'".format(address)
            county_layer=CountyLayer()
            geometries=self.__geometry_helper.get_county_geometries(TagsLayer.COUNTY.value)
            [county_layer.add(geom) for geom in geometries if geom.address_forest.startswith(address)]
            return county_layer
        except Exception as e:
            raise e
        
    def create_basic_objects_layer(self,address: str)->BasicObjectLayer:
        """Tworzenie listy obiektów podstawowych"""
        try:
            #sql="select ADR_BDL, NR_EW, NR_KONT from O_PODST where ADR_BDL like '{}%'".format(address)
            basic_objects_layer=BasicObjectLayer()
            geometries=self.__geometry_helper.get_basic_objects_geometries(TagsLayer.BASIC_OBJECTS.value)
            [basic_objects_layer.add(geom) for geom in geometries if geom.address_forest.startswith(address)]
            return basic_objects_layer
        except Exception as e:
            raise e
        
    def create_subarea_labels_layer(self,address: str)->LabelsLayer:
        """Warstwa opisów pododdziałów"""
        try:
            #sql="select ADR_BDL from OP_POW where ADR_BDL like '{}%'".format(address)
            subarea_labels_layer=LabelsLayer()
            geometries=self.__geometry_helper.get_label_geometries(TagsLayer.SUBAREA_LABELS.value)
            [subarea_labels_layer.add(geom) for geom in geometries if geom.address_forest.startswith(address)]
            return subarea_labels_layer
        except Exception as e:
            raise e
        
    def create_county_labels_layer(self,address: str)->LabelsLayer:
        try:
            #Normalizacja adresu do oddziału
            if len(address)>17:
                address=address[0:17]
            #sql="select ADR_BDL from OP_ODDZ where ADR_BDL like '{}%'".format(address)
            county_labels_layer=LabelsLayer()
            geometries=self.__geometry_helper.get_label_geometries(TagsLayer.COUNTY_LABELS.value)
            [county_labels_layer.add(geom) for geom in geometries if geom.address_forest.startswith(address)]
            return county_labels_layer
        except Exception as e:
            raise e
        
    def create_line_layer(self)->LineLayer:
        try:
            line_layer=LineLayer()
            geometries=self.__geometry_helper.get_line_geometries(TagsLayer.LINES.value)
            [line_layer.add(geom) for geom in geometries]
            return line_layer
        except Exception as e:
            raise e