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

from __future__ import unicode_literals

from lektor.pluginsystem import Plugin
from markupsafe import Markup

from .translation import translate_lazy as _

TEMPLATE = (
    '<a rel="license" target="_blank" href="http://creativecommons.org/'
    'licenses/{type}/{version}/deed.{locale}">'
    '<img alt="Creative Commons {type}" style="border-width:0" '
    'src="https://i.creativecommons.org/l/{type}/{version}/{size}.png" />'
    '</a><br />{message} '
    '<a rel="license" target="_blank" href="http://creativecommons.org/'
    'licenses/{type}/{version}/deed.{locale}">{license}</a>.'
)

LICENSES = {
    'by': {
        'type': 'by',
        'version': '4.0',
        'license': _(
            'Creative Commons Attribution 4.0 International License'
        ),
    },
    'by-nc': {
        'type': 'by-nc',
        'version': '4.0',
        'license': _(
            'Creative Commons Attribution-NonCommercial '
            '4.0 International License'
        ),
    },
    'by-sa': {
        'type': 'by-sa',
        'version': '4.0',
        'license': _(
            'Creative Commons Attribution-ShareAlike 4.0 '
            'International License'
        ),
    },
    'by-nc-sa': {
        'type': 'by-nc-sa',
        'version': '4.0',
        'license': _(
            'Creative Commons Attribution-NonCommercial-ShareAlike '
            '4.0 International License'
        ),
    },
    'by-nd': {
        'type': 'by-nd',
        'version': '4.0',
        'license': _(
            'Creative Commons Attribution-NoDerivatives 4.0 '
            'International License'
        ),
    },
    'by-nc-nd': {
        'type': 'by-nc-nd',
        'version': '4.0',
        'license': _(
            'Creative Commons Attribution-NonCommercial-NoDerivatives '
            '4.0 International License'
        ),
    },
}

LICENSE_SIZES = {
    'normal': '88x31',
    'compact': '80x15',
}


class CreativeCommonsPlugin(Plugin):

    name = 'Creative Commons'
    description = 'Add Creative Commons license to your pages.'

    def __init__(self, env, id):
        self.locale = env.load_config().site_locale or 'en'
        _.translator.configure(self.locale)

        super(CreativeCommonsPlugin, self).__init__(env, id)

    def render_cc_license(self, type, size='normal'):
        license = LICENSES[type].copy()
        license['size'] = LICENSE_SIZES[size]
        license['locale'] = self.locale
        license['message'] = _('This work is licensed under a')

        return Markup(TEMPLATE.format(**license))

    def on_setup_env(self, **extra):
        self.env.jinja_env.globals.update(
            render_cc_license=self.render_cc_license
        )

@@ -187,13 +187,32 @@
 LATE
+S
  =
+ %7B%0A    'full':
  (%0A
+    
     
@@ -276,32 +276,36 @@
 mmons.org/'%0A    
+   
+ 
 'licenses/%7Btype%7D
@@ -332,16 +332,20 @@
 ale%7D%22%3E'%0A
+    
     '%3Cim
@@ -405,16 +405,20 @@
 %22 '%0A    
+  
+  
 'src=%22ht
@@ -480,16 +480,20 @@
 ng%22 /%3E'%0A
+    
     '%3C/a
@@ -511,24 +511,28 @@
 sage%7D '%0A    
+  
+  
 '%3Ca rel=%22lic
@@ -584,24 +584,28 @@
 mmons.org/'%0A
+    
     'license
@@ -654,17 +654,435 @@
 %7D%3C/a%3E.'%0A
-)
+    ),%0A    'image-only': (%0A        '%3Ca rel=%22license%22 target=%22_blank%22 href=%22http://creativecommons.org/'%0A        'licenses/%7Btype%7D/%7Bversion%7D/deed.%7Blocale%7D%22%3E'%0A        '%3Cimg alt=%22Creative Commons %7Btype%7D%22 style=%22border-width:0%22 '%0A        'src=%22https://i.creativecommons.org/l/%7Btype%7D/%7Bversion%7D/%7Bsize%7D.png%22 /%3E'%0A        '%3C/a%3E'%0A    ),%0A    'caller': (%0A        '%7B%7B caller(type, version, locale, size, message, license) %7D%7D'%0A    )%0A%7D
 %0A%0ALICENS
@@ -2767,16 +2767,46 @@
 'normal'
+, template='full', caller=None
 ):%0A     
@@ -2990,16 +2990,107 @@
 der a')%0A
+        %0A        if callable(caller):%0A            return Markup(caller(**license))%0A        
 %0A       
@@ -3112,16 +3112,27 @@
 TEMPLATE
+S%5Btemplate%5D
 .format(

Add additional template types.