# -*- coding: UTF-8 -*-
#    This file is part of arena.
#   
#    arena is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 3 of the License or, at your
#    option, any later version.
#
#    arena is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
#    License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with arena.  If not, see <http://www.gnu.org/licenses/>. 

## @file
#  This file defines some classes that make outputting HTML tags more elegant.

from Library.Base.Classwork import AnObject
from Library.String.Utilities import IsPrintable, String

## The format used to construct a string representation of an XML tag
xtStringRepresentation = "<{tagName}{attributes} />"
## The format used to construct a string representation of an XML wrapper tag
xwtStringRepresentation = "<{tagName}{attributes}>{text}</{tagName}>"

## A basic XML tag
class AnXMLTag(AnObject, IsPrintable):
    ## Constructor
    def __init__(Self, tagName):
        AnObject.__init__(Self)
        
        Self._tagName = tagName
        
    ## Initializer
    def init(Self):
        result = AnObject.init(Self)
        
        if result:
            # Establish private attributes
            # The name of the tag
            Self._tagName = ""
            # The attributes of the tag
            Self._attributes = {}
            
        return result
        
    ## Destructor
    def __del__(Self):
        # Delete the attributes dictionary
        del Self._attributes
        
        AnObject.__del__(Self)
        
    # IsPrintable implementation -----------------------------------------------
    ## Construct a string representation of the tag
    #
    #  The string representation will contain the name of the tag as well as
    #  any attributes from its attributes dictionary that have values which
    #  are not blank.
    def toString(Self):
        attribute = ""
        attrs = ""
        
        if len(Self._attributes) > 0:
            attrs = " "
            for key, value in Self._attributes.iteritems():
                attribute = str(value)
                if len(attribute) > 0:
                    attrs += str(key) + "=\"" + attribute + "\" "
                
            # Strip whitespace from the end
            if attrs[-1] == " ":
                attrs = attrs[0:-1]   
        
        result = xtStringRepresentation.format(
            tagName=Self._tagName,
            attributes=attrs
        )
        
        return result
        
    # Properties ---------------------------------------------------------------
    ## Retrieve the tag name of the tag
    def name(Self):
        if len(Self._tagName) > 0:
            return Self._tagName
            
        return AnObject.name(Self)
        
    ## Retrieve the attributes of the tag
    def attributes(Self):
        return Self._attributes
        
    ## Retrieve the specified attribute from the tag
    def __getitem__(Self, key):
        if key in Self._attributes:
            return Self._attributes[key]
            
        return None
        
    ## Set the specified attribute in the tag
    def __setitem__(Self, key, value):
        Self._attributes[key] = value
        
    ## Delete the specified attribute from the tag
    def __delitem__(Self, key):
        if key in Self._attributes:
            del Self._attributes[key]
        
## An XML tag that wraps around some text
class AnXMLWrapperTag(AnXMLTag):        
    ## Initializer
    def init(Self):
        result = AnXMLTag.init(Self)
        
        if result:
            # Establish public attributes
            # The text of the tag
            Self.text = ""
            
        return result
        
    ## IsPrintable implementation ----------------------------------------------
    ## Construct a string representation of the tag
    #
    #  The string representation will contain the name of the tag as well as
    #  any attributes from its attributes dictionary that have values which
    #  are not blank.
    def toString(Self):
        attribute = ""
        attrs = ""
        
        if len(Self._attributes) > 0:
            attrs = " "
            for key, value in Self._attributes.iteritems():
                attribute = str(value)
                if len(attribute) > 0:
                    attrs += str(key) + "=\"" + attribute + "\" "
                
            # Strip whitespace from the end
            if attrs[-1] == " ":
                attrs = attrs[0:-1]                
        
        result = xwtStringRepresentation.format(
            tagName=Self._tagName,
            attributes=attrs,
            text=Self.text
        )
        
        return result        
