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

from Mapa_PU.swdl.model.swdl.forest_map_no_sf import ForestMapNoSF,CountyLayer,BasicObjectLayer,LineLayer,LabelsLayer
from Mapa_PU.swdl.model.swdl.basic_object_geom import BasicObjectGeom
from Mapa_PU.swdl.model.swdl.line_geom import LineGeom
from Mapa_PU.swdl.model.swdl.geom import GeomInterface, Geom
from Mapa_PU.swdl.model.tags import Tags

class ForestMapNoSFHelperXml():
    """Klasa konwertujaca drzewo xml na obiekty ForestMapNoSF"""
    
    def __init__(self,xml_converter):
        self.__xml=xml_converter
    
    def value(self,element,name):
        """Wartośc elementu
        :param element; element drzewa
        :param name; nazwa wartości
        """ 
        try:
            return self.__xml.get_value(self.__xml.get_element(element,name))
        except Exception as e:
            raise e
    
    def create_forest_map_no_sf(self,tree:str)->ForestMapNoSF:
        """Tworzy obiekt ForestMapNoSF z drzewa xml"""
        try:
            forest_map_no_sf=ForestMapNoSF()
            forest_map_no_sf.county_layer=self.create_county_layer(tree)
            forest_map_no_sf.basic_objects_layer=self.create_basic_object_layer(tree)
            forest_map_no_sf.subarea_labels_layer=self.create_subarea_labels_layer(tree)
            forest_map_no_sf.county_labels_layer=self.create_county_labels_layer(tree)
            forest_map_no_sf.line_layer=self.create_line_layer(tree)
            return forest_map_no_sf
        except Exception as e:
            raise e
     
    def create_county_layer(self,tree:str)->CountyLayer:
        """Tworzy obiekt CountyLayer z drzewa xml"""
        try:
            county_layer=CountyLayer()
            geometries=[Geom(self.value(so,Tags.ADRESS_FOREST.value),self.__xml.to_string(self.__xml.get_element(so,Tags.GEOMETRY.value)[0])) 
                             for so in self.__xml.get_elements(tree,Tags.COUNTY_GEOM.value)]
            [county_layer.add(geom) for geom in geometries]
            return county_layer
        except Exception as e:
            raise e
        
    def create_basic_object_layer(self,tree:str)->BasicObjectLayer:
        """Tworzy obiekt BasicObjectLayer (obiektów podstawowych) z drzewa xml"""
        try:
            layer=BasicObjectLayer()
            geometries=[BasicObjectGeom(self.value(so,Tags.ADRESS_FOREST.value),self.value(so, Tags.PARCEL_NR.value),self.value(so, Tags.AREA_USE_DIC_CONT_NUM.value),self.__xml.to_string(self.__xml.get_element(so,Tags.GEOMETRY.value)[0])) 
                             for so in self.__xml.get_elements(tree,Tags.BASIC_OBJECT_GEOM.value)]
            [layer.add(geom) for geom in geometries]
            return layer
        except Exception as e:
            raise e  
        
    def create_subarea_labels_layer(self,tree:str)->LabelsLayer:
        """Tworzy obiekt LabelsLayer (opisy pododdziałów) z drzewa xml"""
        try:
            layer=LabelsLayer()
            geometries=[Geom(self.value(so,Tags.ADRESS_FOREST.value),self.__xml.to_string(self.__xml.get_element(so,Tags.GEOMETRY.value)[0])) 
                             for so in self.__xml.get_elements(tree,Tags.SUBAREA_LABEL_GEOM.value)]
            [layer.add(geom) for geom in geometries]
            return layer
        except Exception as e:
            raise e  
        
    def create_county_labels_layer(self,tree:str)->LabelsLayer:
        """Tworzy obiekt LabelsLayer (opisy oddziałów) z drzewa xml"""
        try:
            layer=LabelsLayer()
            geometries=[Geom(self.value(so,Tags.ADRESS_FOREST.value),self.__xml.to_string(self.__xml.get_element(so,Tags.GEOMETRY.value)[0])) 
                             for so in self.__xml.get_elements(tree,Tags.COUNT_LABEL_GEOM.value)]
            [layer.add(geom) for geom in geometries]
            return layer
        except Exception as e:
            raise e  
        
    def create_line_layer(self,tree:str)->LabelsLayer:
        """Tworzy obiekt LineLayer (obiekty liniowe) z drzewa xml"""
        try:
            layer=LabelsLayer()
            geometries=[LineGeom(self.value(so,Tags.WIDTH.value),self.value(so,Tags.AREA_TYPE_CD_LINE.value),self.__xml.to_string(self.__xml.get_element(so,Tags.GEOMETRY.value)[0])) 
                             for so in self.__xml.get_elements(tree,Tags.LINE_GEOM.value)]
            [layer.add(geom) for geom in geometries]
            return layer
        except Exception as e:
            raise e  