# -*- coding: utf-8 -*-
"""
    matterllo.hook.card
    ~~~~~~~~~~~~~~~~~~~

    The application to parse card action.

    :copyright: (c) 2016 by Lujeni.
    :license: BSD, see LICENSE for more details.
"""

class Hook(object):

    def createCard(self, action):
        data = action['data']
        context = {
            'board_link': data['board']['shortLink'],
            'list_name': data['list']['name'],
            'card_name': data['card']['name'],
            'card_link': data['card']['shortLink'],
        }
        payload = u':incoming_envelope: New card "[{card_name}](https://trello.com/c/{card_link})" added to list "[{list_name}](https://trello.com/b/{board_link})"'

        return payload.format(**context)

    def updateCard(self, action):
        data = action['data']
        if data.get('listAfter') and data.get('listBefore'):
            return self.moveCardToList(action=action)

        if data['card'].get('old', False):
            return self.renameCard(action)
        if data['card'].get('closed', False):
            return self.archiveCard(action=action)
        return self.unarchiveCard(action=action)

    def archiveCard(self, action):
        data = action['data']
        context = {
            'board_link': data['board']['shortLink'],
            'card_name': data['card']['name'],
        }
        payload = u':incoming_envelope: Card archived: "[{card_name}](https://trello.com/b/{board_link})"'

        return payload.format(**context)

    def unarchiveCard(self, action):
        data = action['data']
        context = {
            'board_link': data['board']['shortLink'],
            'card_name': data['card']['name'],
        }
        payload = u':incoming_envelope: Card unarchived: "[{card_name}](https://trello.com/b/{board_link})"'

        return payload.format(**context)

    def renameCard(self, action):
        data = action['data']
        context = {
            'board_link': data['board']['shortLink'],
            'card_name': data['card']['name'],
            'card_old_name': data['old']['name'],
        }
        payload = u':incoming_envelope: Card renamed from "{card_old_name}" to "[{card_name}](https://trello.com/b/{board_link})"'

        return payload.format(**context)

    def commentCard(self, action):
        data = action['data']
        context = {
            'board_link': data['board']['shortLink'],
            'card_name': data['card']['name'],
            'card_link': data['card']['shortLink'],
            'member_creator': action['memberCreator']['fullName'],
            'comment': data['text'],
        }
        payload = u''':incoming_envelope: New comment on card "[{card_name}](https://trello.com/b/{card_link})" by `{member_creator}`
> {comment}'''

        return payload.format(**context)

    def moveCardFromBoard(self, action):
        data = action['data']
        context = {
            'board_target_name': data['boardTarget']['name'],
            'board_name': data['board']['name'],
            'board_link': data['board']['shortLink'],
            'card_name': data['card']['name'],
        }
        payload = u':incoming_envelope: Card moved: "[{card_name}](https://trello.com/b/{board_link})" moved from "[{board_name}](https://trello.com/b/{board_link}) to board "{board_target_name}"'

        return payload.format(**context)

    def moveCardToBoard(self, action):
        data = action['data']
        context = {
            'board_source_name': data['boardSource']['name'],
            'board_name': data['board']['name'],
            'board_link': data['board']['shortLink'],
            'card_name': data['card']['name'],
        }
        payload = u':incoming_envelope: Card moved: "[{card_name}](https://trello.com/b/{board_link})" moved to "[{board_name}](https://trello.com/b/{board_link}) from board "{board_source_name}"'

        return payload.format(**context)

@@ -917,32 +917,24 @@
      if data
-%5B'card'%5D
 .get('old', 
@@ -957,38 +957,170 @@
     
-return self.renameCard(action)
+if 'name' in data%5B'old'%5D:%0A                return self.renameCard(action)%0A            if 'desc' in data%5B'old'%5D:%0A                return self.renameCardDesc(action)%0A
 %0A   
@@ -1205,32 +1205,33 @@
 (action=action)%0A
+%0A
         return s
@@ -2371,32 +2371,460 @@
 mat(**context)%0A%0A
+    def renameCardDesc(self, action):%0A        data = action%5B'data'%5D%0A        context = %7B%0A            'board_link': data%5B'board'%5D%5B'shortLink'%5D,%0A            'card_name': data%5B'card'%5D%5B'name'%5D,%0A            'card_desc': data%5B'card'%5D%5B'desc'%5D,%0A        %7D%0A        payload = u''':incoming_envelope: Card updated: %22%5B%7Bcard_name%7D%5D(https://trello.com/b/%7Bboard_link%7D)%22%0A**Description**: %7Bcard_desc%7D'''%0A%0A        return payload.format(**context)%0A%0A
     def commentC

Support updateDescCard actio