# -*- 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 a class that manages a view, which parses content provided
#  to it and returns a string containing the parsed content.

from Library.Base.Events import ALoggingEventfulObject
from Library.IO.Logging.Logs import AMessageWithTimeStamp

## Events generated by a view
vwEvents = {
    ## Before the view is rendered
    "render": None
}

## A view error
class AViewError(AMessageWithTimeStamp):
    pass
    
## A simple view
class AView(ALoggingEventfulObject):
    ## Constructor
    def __init__(Self, **params):
        ALoggingEventfulObject.__init__(Self, **params)
        
    ## Initializer
    def init(Self):
        result = ALoggingEventfulObject.init(Self)
        
        if result:
            # Establish public attributes
            # The variables assigned to the view
            Self.variables = {}
                        
            # Update the events generated by the view
            Self._events.update(vwEvents)
            
        return result
        
    ## Render the view and return a string that contains the rendered content.
    #
    #  This routine generates the \p render event.
    def render(Self, content):
        result = ""
        
        Event = Self.generate("render", content=content)
        if Event.handled():
            result = Event.result()
            
        del Event
        return result
