import re

from precise_bbcode.bbcode.tag import BBCodeTag
from precise_bbcode.tag_pool import tag_pool


color_re = re.compile(r'^([a-z]+|#[0-9abcdefABCDEF]{3,6})$')


class SubTag(BBCodeTag):
    name = 'sub'

    def render(self, value, option=None, parent=None):
        return '<sub>%s</sub>' % value


class PreTag(BBCodeTag):
    name = 'pre'
    render_embedded = False

    def render(self, value, option=None, parent=None):
        return '<pre>%s</pre>' % value


class SizeTag(BBCodeTag):
    name = 'size'
    definition_string = '[size={RANGE=4,7}]{TEXT}[/size]'
    format_string = '<span style="font-size:{RANGE=4,7}px;">{TEXT}</span>'


class FruitTag(BBCodeTag):
    name = 'fruit'
    definition_string = '[fruit]{CHOICE=tomato,orange,apple}[/fruit]'
    format_string = '<h5>{CHOICE=tomato,orange,apple}</h5>'


class PhoneLinkTag(BBCodeTag):
    name = 'phone'
    definition_string = '[phone]{PHONENUMBER}[/phone]'
    format_string = '<a href="tel:{PHONENUMBER}">{PHONENUMBER}</a>'

    def render(self, value, option=None, parent=None):
        href = 'tel:{}'.format(value)
        return '<a href="{0}">{0}</a>'.format(href, value)


class StartsWithATag(BBCodeTag):
    name = 'startswitha'
    definition_string = '[startswitha]{STARTSWITH=a}[/startswitha]'
    format_string = '<span>{STARTSWITH=a}</span>'


class RoundedBBCodeTag(BBCodeTag):
    name = 'rounded'

    class Options:
        strip = False

    def render(self, value, option=None, parent=None):
        if option and re.search(color_re, option) is not None:
            return '<div class="rounded" style="border-color:{};">{}</div>'.format(option, value)
        return '<div class="rounded">{}</div>'.format(value)


tag_pool.register_tag(SubTag)
tag_pool.register_tag(PreTag)
tag_pool.register_tag(SizeTag)
tag_pool.register_tag(FruitTag)
tag_pool.register_tag(PhoneLinkTag)
tag_pool.register_tag(StartsWithATag)
tag_pool.register_tag(RoundedBBCodeTag)

[((7, 11, 7, 60), 're.compile', 're.compile', ({(7, 22, 7, 59): '"""^([a-z]+|#[0-9abcdefABCDEF]{3,6})$"""'}, {}), "('^([a-z]+|#[0-9abcdefABCDEF]{3,6})$')", False, 'import re\n'), ((65, 0, 65, 29), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(65, 22, 65, 28): 'SubTag'}, {}), '(SubTag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((66, 0, 66, 29), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(66, 22, 66, 28): 'PreTag'}, {}), '(PreTag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((67, 0, 67, 30), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(67, 22, 67, 29): 'SizeTag'}, {}), '(SizeTag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((68, 0, 68, 31), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(68, 22, 68, 30): 'FruitTag'}, {}), '(FruitTag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((69, 0, 69, 35), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(69, 22, 69, 34): 'PhoneLinkTag'}, {}), '(PhoneLinkTag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((70, 0, 70, 37), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(70, 22, 70, 36): 'StartsWithATag'}, {}), '(StartsWithATag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((71, 0, 71, 39), 'precise_bbcode.tag_pool.tag_pool.register_tag', 'tag_pool.register_tag', ({(71, 22, 71, 38): 'RoundedBBCodeTag'}, {}), '(RoundedBBCodeTag)', False, 'from precise_bbcode.tag_pool import tag_pool\n'), ((60, 22, 60, 49), 're.search', 're.search', ({(60, 32, 60, 40): 'color_re', (60, 42, 60, 48): 'option'}, {}), '(color_re, option)', False, 'import re\n')]