"""Moduł pomocniczy tworzący xml z obiektu ForestDataDesc
"""
from Mapa_PU.swdl.model.swdl.forest_data_desc import ForestDataDesc,InventoryData
from Mapa_PU.swdl.model.scheme import TreeElement,TreeElementOptional
from Mapa_PU.swdl.model.tags import Tags
from Mapa_PU.swdl.model.swdl.supervisor_info import SupervisorInfo

"""klasa tworząca xml z obiektu ForestDataDesc
"""
class ForestDataDescConverter():
    
    def create_forest_data_desc(self,forest_data_desc):
        #utworzenie drzewa forestDataDesc
        try:
            fdd=TreeElement(Tags.FOREST_DATA_DESC.value)
            fdd.append(TreeElement(Tags.INVENTORY_OBJECT_NAME.value,forest_data_desc.inventory_data.inventory_object_name))
            fdd.append(TreeElement(Tags.INVENT_YEAR.value,str(forest_data_desc.inventory_data.invent_year)))  
            fdd.append(TreeElement(Tags.ADRESS_FOREST.value,forest_data_desc.address_forest))
            fdd.append(TreeElementOptional.create(Tags.INVENT_EXECUTOR.value,forest_data_desc.invent_executor))
            inventory_data=forest_data_desc.inventory_data
            #uzaleznienie utworzenia gałęzi od  supervisor
            if inventory_data.supervisor is not None:
              supervision=TreeElement(Tags.SUPERVISION.value)
              #supervisor
              supervisor=TreeElement(Tags.SUPERVISOR.value)
              supervisorInfo=inventory_data.supervisor
              supervisor.append(TreeElement(Tags.ORGANIZATION_NAME.value,supervisorInfo.organization_name))
              supervisor.append(TreeElement(Tags.ORGANIZATION_INFO.value,supervisorInfo.organization_info))
              supervisor.append(TreeElement(Tags.STREET.value,supervisorInfo.street))
              supervisor.append(TreeElement(Tags.CITY.value,supervisorInfo.city))
              supervisor.append(TreeElement(Tags.POSTAL_CODE.value,supervisorInfo.postal_code))
              supervisor.append(TreeElement(Tags.COUNTRY.value,supervisorInfo.country))
              supervisor.append(TreeElement(Tags.PHONE.value,supervisorInfo.phone))
              supervisor.append(TreeElement(Tags.FAX.value,supervisorInfo.fax))
              supervisor.append(TreeElement(Tags.EMAIL.value,supervisorInfo.email))
              supervision.append(supervisor)
              
              if inventory_data.delegate_supervisor is not None:
                delegateSupervisor=inventory_data.delegate_supervisor
                delegateInfo=TreeElement(Tags.DELEGATE_SUPERVISOR.value)
                delegateInfo.append(TreeElement(Tags.ORGANIZATION_NAME.value,delegateSupervisor.organization_name))
                delegateInfo.append(TreeElement(Tags.ORGANIZATION_INFO.value,delegateSupervisor.organization_info))
                delegateInfo.append(TreeElement(Tags.STREET.value,delegateSupervisor.street))
                delegateInfo.append(TreeElement(Tags.CITY.value,delegateSupervisor.city))
                delegateInfo.append(TreeElement(Tags.POSTAL_CODE.value,delegateSupervisor.postal_code))
                delegateInfo.append(TreeElement(Tags.COUNTRY.value,delegateSupervisor.country))
                delegateInfo.append(TreeElement(Tags.PHONE.value,delegateSupervisor.phone))
                delegateInfo.append(TreeElement(Tags.FAX.value,delegateSupervisor.fax))
                delegateInfo.append(TreeElement(Tags.EMAIL.value,delegateSupervisor.email))
                supervision.append(delegateInfo)
              
              fdd.append(supervision)
            stateDate=forest_data_desc.state_date
            if stateDate is None:
                stateDate='{}-01-01'.format(forest_data_desc.inventory_data.invent_year)
            
            fdd.append(TreeElement(Tags.STATE_DATE.value,str(stateDate)))
            
            return fdd
        except Exception as e:
            raise e