import logging

from sirbot.plugins.github.hookimpl import hookimpl
from sirbot.slack.message import SlackMessage, Attachment

logger = logging.getLogger('sirbot.pythondev')


async def issues(event, facades):
    att = None

    if event['action'] == 'opened':
        att = issue_format(event, 'good')
    elif event['action'] == 'closed':
        att = issue_format(event, 'danger')

    if att:
        slack = facades.get('slack')
        channel = await slack.channels.get(name='community-projects')
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)


def issue_format(event, color):

    att = Attachment(
        fallback='issue {}'.format(event['action']),
        pretext='Issue {action} in <{url}|{name}>'.format(
            url=event['repository']['html_url'],
            name=event['repository']['name'],
            action=event['action']
        ),
        color=color,
        text=event['issue']['body'],
        title=event['issue']['title'],
        title_link=event['issue']['html_url'],
        author_icon=event['sender']['avatar_url'],
        author_name=event['sender']['login'],
        author_link=event['sender']['html_url'],
        footer=', '.join(label['name'] for label in event['issue']['labels'])
    )

    return att


async def pull_request(event, facades):
    att = None

    if event['action'] == 'opened':
        data = {'color': 'good', 'action': 'opened'}
        att = pull_request_format(event, data)
    elif event['action'] == 'closed':
        if event['pull_request']['merged']:
            data = {'color': '#6f42c1', 'action': 'merged'}
        else:
            data = {'color': 'danger', 'action': 'closed'}

        att = pull_request_format(event, data)

    if att:
        slack = facades.get('slack')
        channel = await slack.channels.get(name='community-projects')
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)


def pull_request_format(event, data):

    footer = '+ {add} / - {del_}'.format(
        add=event['pull_request']['additions'],
        del_=event['pull_request']['deletions']
    )

    att = Attachment(
        fallback='pull request {}'.format(data['action']),
        pretext='Pull request {action} in <{url}|{name}>'.format(
            url=event['repository']['html_url'],
            name=event['repository']['name'],
            action=data['action']
        ),
        color=data['color'],
        text=event['pull_request']['body'],
        title=event['pull_request']['title'],
        title_link=event['pull_request']['html_url'],
        author_icon=event['sender']['avatar_url'],
        author_name=event['sender']['login'],
        author_link=event['sender']['html_url'],
        footer=footer
    )

    return att


@hookimpl
def register_github_events():

    events = [
        {
            'event': 'issues',
            'func': issues
        },
        {
            'event': 'pull_request',
            'func': pull_request
        }

    ]

    return events

@@ -479,33 +479,33 @@
 (name='community
--
+_
 projects')%0A     
@@ -964,32 +964,249 @@
 ssue'%5D%5B'body'%5D,%0A
+        title='Issue %7Baction%7D in %3C%7Burl%7D%7C%7Bname%7D%3E: %7Btitle%7D'.format(%0A                url=event%5B'repository'%5D%5B'html_url'%5D,%0A                name=event%5B'repository'%5D%5B'name'%5D,%0A                action=event%5B'action'%5D,%0A        
         title=ev
@@ -1218,32 +1218,42 @@
 issue'%5D%5B'title'%5D
+%0A        )
 ,%0A        title_
@@ -2096,17 +2096,17 @@
 ommunity
--
+_
 projects
@@ -2488,23 +2488,21 @@
         
-pretext
+title
 ='Pull r
@@ -2526,32 +2526,41 @@
 n %3C%7Burl%7D%7C%7Bname%7D%3E
+: %7Btitle%7D
 '.format(%0A      
@@ -2673,32 +2673,83 @@
 n=data%5B'action'%5D
+,%0A            title=event%5B'pull_request'%5D%5B'title'%5D,
 %0A        ),%0A    
@@ -2821,54 +2821,8 @@
 '%5D,%0A
-        title=event%5B'pull_request'%5D%5B'title'%5D,%0A
     

Rename community projects channel & formating