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

Mmodul schematu drzewa xml w postaci klas 
"""
import lxml.etree
from lxml.etree import Element, ElementTree, XMLSchema
#from xml.etree.ElementTree as et
from Mapa_PU.swdl.model.tags import Tags
import zipfile
from zipfile import ZipFile

class BaseElement():
    """
    Klasa bazowa dla elementów
    """
    def __init__(self,tag,text=None,nsmap=None):
        ##dodanie przestrzeni nazw
        self.element=Element(tag,nsmap=nsmap)
        self.element.text=text
        
    
    def tag(self):
        ##Wartosc tagu elementu
        return self.element.tag
    
    def append(self,el):
        ##dodanie dziecka drzewa
        if(el==None):
            return
        if hasattr(el, 'element'):
            self.element.append(el.element)
        else:
            self.element.append(el)
        
            
    #def set_text(self,text):
    #    ##Wstawia rartoś w wezel
    #    self.swdl.text=text
        
    def text(self):
        ##wartośc elementu
        return self.element.text

class RootElement(BaseElement):
    """
    Korzen drzewa
    """
    
    def __init__(self):
        self.nsmap={None:'https://www.bdl.lasy.gov.pl/portal/swdl',
                    'docbook':'http://docbook.org/ns/docbook',
                    'xsi':'http://www.w3.org/2001/XMLSchema-instance',
                    'xlink':'http://www.w3.org/1999/xlink',
                    'gml':'http://www.opengis.net/gml'
                    }
        self.SWDL_NAME='obiekt.swdl'
        BaseElement.__init__(self,Tags.ROOT.value,nsmap=self.nsmap)
             
    def write(self,path):
        ##Zapis elementu do pliku xml
        try:
            tree = ElementTree(self.element)
            #tree.write(path, pretty_print=True, xml_declaration=True,   encoding="utf-8")
            data=lxml.etree.tostring(tree,pretty_print=True, xml_declaration=True,encoding="utf-8")
            self.write_to_zip(path, data)
        except Exception as e:
            raise e
        
    def set(self,name,value):
        ##Dodanie parametru
        self.element.set(name,value)
        
    def write_to_zip(self,path:str,data:str):
        """Zapis drzewa do pliku zip"""
        try:
            with ZipFile(path,'w',compression=zipfile.ZIP_DEFLATED) as myzip:
                myzip.writestr(self.SWDL_NAME,data)
        except Exception as e:
            raise e

class TreeElementOptional(BaseElement):
    """Opcjonalny element drzewa jeżeli tetx=None zwracany jest None
    """
    def __init__(self,tag,text=None):
        BaseElement.__init__(self,tag)
        self.element.text=text
        
    def set(self,name,value):
        ##Dodanie parametru
        self.element.set(name,value)
        
    @classmethod
    def create(cls,tag,text):
        if(text==None):
            return None
        return cls(tag,str(text))

class TreeElement(BaseElement):
    """
    Element drzewa 
    """
    def __init__(self,tag,text=None):
        BaseElement.__init__(self,tag)
        if text is not None:
            self.element.text=str(text)
        
    def set(self,name,value):
        ##Dodanie parametru
        self.element.set(name,value)
        
    
    