# -*- coding: utf-8 -*-
"""Moduł pomocniczy konwersji danych globalnych
"""
from Mapa_PU.swdl.model.swdl.forest_global_data import ForestGlobalData
from Mapa_PU.swdl.model.scheme import TreeElement,TreeElementOptional
from Mapa_PU.swdl.model.tags import Tags
"""Klasa pomocnicza danych globalnych
"""
class ForestGlobalDataConverter():
    
    def create_global_data(self,forest_global_data):
        """Metoda tworząca drzewo xml dla danych globalnych
        :param forest_global_data; dane globalne
        """
        try:
            global_data=TreeElement(Tags.FOREST_GLOBAL_DATA.value)
            #województwa
            conties=TreeElement(Tags.COUNTIES.value)
            for county in forest_global_data.counties:
                conties.append(self.create_county(county))
            global_data.append(conties)
            #powiaty
            districts=TreeElement(Tags.DISTRICTS.value)
            for district in forest_global_data.districts:
                districts.append(self.create_district(district))
            global_data.append(districts)
            #gminy
            municipalities=TreeElement(Tags.MUNICIPALITIES.value)
            for municipality in forest_global_data.municipalities:
                municipalities.append(self.create_municipality(municipality))
            global_data.append(municipalities)
            #obręby
            communities=TreeElement(Tags.COMMUNITIES.value)
            for community in forest_global_data.communities:
                communities.append(self.create_community(community))
            global_data.append(communities)
            #wskazowki
            measures=TreeElement(Tags.MEASURES.value)
            for measure in forest_global_data.measures:
                measures.append(self.create_measure(measure))
            global_data.append(measures)
            
            return global_data
        except Exception as e:
            raise e
    
    def create_measure(self,measure):
        """Wskazówka gospodarcza
        :param measure; wskazówka
        """
        try:
            m=TreeElement(Tags.MEASURE.value)
            m.append(TreeElement(Tags.MEASURE_CD.value,measure.measure_cd))
            m.append(TreeElementOptional.create(Tags.MEASURE_NR.value,measure.measure_nr))
            m.append(TreeElementOptional.create(Tags.MEASURE_NAME.value,measure.measure_name))
            #m.append(TreeElementOptional.create(Tags.COST_CENTRE_CD.value,measure.cost_centre_cd))
            return m
        except Exception as e:
            raise e
    
    def create_community(self,community):
        """Obręby
        :param community; obiekt obrębu
        """
        try:
            c=TreeElement(Tags.COMMUNITY.value)
            c.append(TreeElement(Tags.COUNTY_CD.value,community.county_cd))
            c.append(TreeElement(Tags.DISTRICT_CD.value,community.district_cd))
            c.append(TreeElement(Tags.MUNICIPALITY_CD.value,community.municipality_cd))
            c.append(TreeElement(Tags.COMMUNITY_CD.value,community.community_cd))
            c.append(TreeElement(Tags.COMMUNITY_NAME.value,community.community_name))
            c.append(TreeElementOptional.create(Tags.ALLOW_CUT_LCA.value,community.allow_cut_l_c_a))
            c.append(TreeElementOptional.create(Tags.ALLOW_CUT_PCA.value,community.allow_cut_p_c_a))
            c.append(TreeElementOptional.create(Tags.ALLOW_CUT_CN.value,community.allow_cut_c_n))
            c.append(TreeElementOptional.create(Tags.ALLOW_CUT_ADOPTED.value,community.allow_cut_adopted))
            c.append(TreeElementOptional.create(Tags.ALLOW_CUT_OTHER_CUT.value,community.allow_cut_other_cut))
            c.append(TreeElementOptional.create(Tags.ALLOW_CUT_MAX_VOL.value,community.allow_cut_max_vol))
            return c
        except Exception as e:
            raise e
        
    def create_municipality(self,municipality):
        """Gmina
        :param municipality; obiekt gminy
        """
        try:
            m=TreeElement(Tags.MUNICIPALITY.value)
            m.append(TreeElement(Tags.COUNTY_CD.value,municipality.county_cd))
            m.append(TreeElement(Tags.DISTRICT_CD.value,municipality.district_cd))
            m.append(TreeElement(Tags.MUNICIPALITY_CD.value,municipality.municipality_cd))
            m.append(TreeElement(Tags.MUNICIPALITY_NAME.value,municipality.municipality_name))
            return m
        except Exception as e:
            raise e
    
    def create_district(self,district):
        """Powiat
        :param district; obiekt powiatu
        """
        try:
            d=TreeElement(Tags.DISTRICT.value)
            d.append(TreeElement(Tags.COUNTY_CD.value,district.county_cd))
            d.append(TreeElement(Tags.DISTRICT_CD.value,district.district_cd))
            d.append(TreeElement(Tags.DISTRICT_NAME.value,district.district_name))
            return d
        except Exception as e:
            raise e
    
    def create_county(self,county):
        """Województwo
        :param county; obiekt województwa
        """
        try:
            c=TreeElement(Tags.COUNTY.value)
            c.append(TreeElement(Tags.COUNTY_CD.value,county.county_cd))
            c.append(TreeElement(Tags.COUNTY_NAME.value,county.county_name))
            return c
        except Exception as e:
            raise e
            
    