from . import trelloobject


class Checklist(trelloobject.TrelloObject):

    '''
    Class representing a Trello Checklist
    '''

    def __init__(self, trello_client, checklist_id, name='', **kwargs):
        super(Checklist, self).__init__(trello_client, **kwargs)

        card_id = kwargs.get('card_id', None)
        if not card_id:
            # TODO: lookup card id
            pass
        self.idCard = card_id
        self.id = checklist_id
        self.name = name

        self.base_uri = '/checklists/' + self.id

    def get_checklist_information(self, query_params=None):
        '''
        Get all information for this Checklist. Returns a dictionary of values.
        '''
        return self.fetch_json(
            uri_path=self.base_uri,
            query_params=query_params or {}
        )

    def get_items(self, query_params=None):
        '''
        Get all the items for this checklist. Returns a list of dictionaries.
        Each dictionary has the values for an item.
        '''
        return self.fetch_json(
            uri_path=self.base_uri + '/checkItems',
            query_params=query_params or {}
        )

    def get_item_objects(self, query_params=None):
        """
        Get the items for this checklist. Returns a list of ChecklistItem objects.
        """
        checklistitems_list = []
        for checklistitem_json in self.getItems(query_params):
            checklistitems_list.append(self.create_checklist_item(self.idCard, self.id, checklistitem_json))

        return checklistitems_list


    def update_checklist(self, name):
        '''
        Update the current checklist. Returns a new Checklist object.
        '''
        checklist_json = self.fetch_json(
            uri_path=self.base_uri,
            http_method='PUT',
            query_params={'name': name}
        )

        return self.create_checklist(checklist_json)

    def add_item(self, query_params=None):
        '''
        Add an item to this checklist. Returns a dictionary of values of new
        item.
        '''
        return self.fetch_json(
            uri_path=self.base_uri + '/checkItems',
            http_method='POST',
            query_params=query_params or {}
        )

    def remove_item(self, item_id):
        '''
        Deletes an item from this checklist.
        '''
        return self.fetch_json(
            uri_path=self.base_uri + '/checkItems/' + item_id,
            http_method='DELETE'
        )


class ChecklistItem(TrelloObject):
    """
    Class representing a Trello Checklist Item
    """
    def __init__(self, trello_client, card_id, checklist_id, checklistitem_id, name='', state='incomplete'):
        super(ChecklistItem, self).__init__(trello_client)

        self.idCard = card_id
        self.idChecklist = checklist_id
        self.id = checklistitem_id
        self.name = name
        self.state = (state == 'complete')

        self.base_uri = '/cards/' + self.idCard + '/checklist/' + self.idChecklist + '/checkItem/' + self.id


    def update_name( self, name ):
        """
        Rename the current checklist item. Returns a new ChecklistItem object.
        """
        checklistitem_json = self.fetch_json(
            uri_path = self.base_uri + '/name',
            http_method = 'PUT',
            query_params = {'value': name}
        )

        return self.create_checklist_item(self.idCard, self.idChecklist, checklistitem_json)


    def update_state(self, state):
        """
        Set the state of the current checklist item. Returns a new ChecklistItem object.
        """
        checklistitem_json = self.fetch_json(
            uri_path = self.base_uri + '/state',
            http_method = 'PUT',
            query_params = {'value': 'complete' if state else 'incomplete'}
        )

        return self.create_checklist_item(self.idCard, self.idChecklist, checklistitem_json)

@@ -679,32 +679,107 @@
 es.%0A        '''%0A
+        # TODO: why not use trelloobject.TrelloObject.get_checklist_json ?%0A
         return s
@@ -877,32 +877,260 @@
 r %7B%7D%0A        )%0A%0A
+    def get_card(self):%0A        '''%0A        Get card this checklist is on.%0A        '''%0A        card_id = self.get_checklist_information().get('idCard', None)%0A        if card_id:%0A            return self.client.get_card(card_id)%0A%0A
     def get_item
@@ -1600,32 +1600,63 @@
 ts.%0A        %22%22%22%0A
+        card = self.get_card()%0A
         checklis
@@ -1714,17 +1714,18 @@
 self.get
-I
+_i
 tems(que
@@ -1798,34 +1798,30 @@
 cklist_item(
-self.idCar
+card.i
 d, self.id, 
@@ -2819,16 +2819,29 @@
 istItem(
+trelloobject.
 TrelloOb

Add Checklist.get_card; fix class reference