from nextactions.card import Card


class SyncTool:

    def __init__(self, config, trello):
        self._config = config
        self._trello = trello

    def getNextActionCards(self):
        board = self._trello.getBoardById(self._config.get('gtd_board_id'))
        list_ = board.getListByName('Next Actions')
        return [c for c in list_.getCards() if c.isAutoGenerated()]

    def reset(self):
        next_actions = self.getNextActionCards()
        for c in next_actions:
            c.archive()
        return next_actions

    def getProjectBoards(self):
        board = self._trello.getBoardById(self._config.get('gtd_board_id'))
        list_ = board.getListByName('Projects')
        return [c.getProjectBoard() for c in list_.getCards()]

    def getTopTodoCards(self):
        todo_cards = []
        for board in self.getProjectBoards():
            todo_list = board.getListByName('Todo')
            if todo_list is not None:
                todo_cards.append(todo_list.getTopCard())
        return todo_cards

    def syncCard(self, card):
        card_board = self._trello.getBoardById(card.board_id)
        gtd_board = self._trello.getBoardById(self._config.get('gtd_board_id'))
        next_actions_list = gtd_board.getListByName('Next Actions')

        name = card_board.name + " - " + card.name
        description = card.url + "\n\n" + Card.AUTO_GENERATED_TEXT
        next_actions_list.createCard(name, description)

    def sync(self):
        next_actions = self.getNextActionCards()
        all_cards = self._trello.getOwnedCards() + self.getTopTodoCards()

        cards_to_sync = [c for c in all_cards
                         if not any(a.linksTo(c) for a in next_actions)]
        actions_to_archive = [a for a in next_actions
                              if not any(a.linksTo(c) for c in all_cards)]
        for card in cards_to_sync:
            self.syncCard(card)
        for action in actions_to_archive:
            action.archive()
        return (cards_to_sync, actions_to_archive)

from nextactions.card import Card


class SyncTool:

    def __init__(self, config, trello):
        self._config = config
        self._trello = trello

    def getNextActionCards(self):
        board = self._trello.getBoardById(self._config.get('gtd_board_id'))
        list_ = board.getListByName('Next Actions')
        return [c for c in list_.getCards() if c.isAutoGenerated()]

    def reset(self):
        next_actions = self.getNextActionCards()
        for c in next_actions:
            c.archive()
        return next_actions

    def getProjectBoards(self):
        board = self._trello.getBoardById(self._config.get('gtd_board_id'))
        list_ = board.getListByName('Projects')
        return [c.getProjectBoard() for c in list_.getCards()]

    def getTopTodoCards(self):
        todo_cards = []
        for board in self.getProjectBoards():
            todo_list = board.getListByName('Todo')
            if todo_list is not None:
                todo_cards.append(todo_list.getTopCard())
        return todo_cards

    def syncCard(self, card):
        card_board = self._trello.getBoardById(card.board_id)
        gtd_board = self._trello.getBoardById(self._config.get('gtd_board_id'))
        next_actions_list = gtd_board.getListByName('Inbox')

        name = card_board.name + " - " + card.name
        description = card.url + "\n\n" + Card.AUTO_GENERATED_TEXT
        next_actions_list.createCard(name, description)

    def sync(self):
        next_actions = self.getNextActionCards()
        all_cards = self._trello.getOwnedCards() + self.getTopTodoCards()

        cards_to_sync = [c for c in all_cards
                         if not any(a.linksTo(c) for a in next_actions)]
        actions_to_archive = [a for a in next_actions
                              if not any(a.linksTo(c) for c in all_cards)]
        for card in cards_to_sync:
            self.syncCard(card)
        for action in actions_to_archive:
            action.archive()
        return (cards_to_sync, actions_to_archive)

Add cards to Inbox instead of Next Actions