import collections

from poobrains import Response, Markup, app
import poobrains.helpers
import poobrains.auth

class SVG(poobrains.auth.Protected):
    
    handle = None # needed so that app.expose registers a route with extra param, this is kinda hacky…
    
    class Meta:

        modes = collections.OrderedDict([
            ('teaser', 'read'),
            ('full', 'read'),
            ('raw', 'read'),
            ('inline', 'read')
        ])
    
    style = None

    def __init__(self, handle=None, mode=None, **kwargs):

        super(SVG, self).__init__(**kwargs)

        self.handle = handle
        self.style = Markup(app.scss_compiler.compile_string("@import 'svg';"))
    
    
    def templates(self, mode=None):

        r = super(SVG, self).templates(mode=mode)
        return ["svg/%s" % template for template in r]

    
    def instance_url(self, mode='full', quiet=False, **url_params):
    
        url_params['handle'] = self.handle

        return super(SVG, self).instance_url(mode=mode, quiet=quiet, **url_params) 


    @poobrains.helpers.themed
    def view(self, mode=None, handle=None):

        if mode == 'raw':
            
            response = Response(self.render('raw'))
            response.headers['Content-Type'] = u'image/svg+xml'
            response.headers['Content-Disposition'] = u'filename="%s.svg"' % self.__class__.__name__
            
            # Disable "public" mode caching downstream (nginx, varnish) in order to hopefully not leak restricted content
            response.cache_control.public = False
            response.cache_control.private = True
            response.cache_control.max_age = app.config['CACHE_LONG']

            return response
        
        else:
            return poobrains.helpers.ThemedPassthrough(super(SVG, self).view(mode=mode, handle=handle))
