import iSenseAPI

# Field Types
TIMESTAMP = iSenseAPI.TIMESTAMP
NUMBER = iSenseAPI.NUMBER
TEXT = iSenseAPI.TEXT
LATITUDE = iSenseAPI.LATITUDE
LONGITUDE = iSenseAPI.LONGITUDE


class Project(object):

    def __init__(self, project_id): 

        dictionary = iSenseAPI.getProject(project_id, recur=True)

        # __ makes the variables private. That way they can only be accessed within this class
        self.__id = project_id
        self.__name = dictionary['name']
        self.__url = dictionary['url']
        self.__path = dictionary['path']
        self.__hidden = dictionary['hidden']
        self.__featured = dictionary['featured']
        self.__like_count = dictionary['likeCount']
        self.__content = dictionary['content']
        self.__time_ago_in_words = dictionary['timeAgoInWords']
        self.__created_at = dictionary['createdAt']
        self.__owner_name = dictionary['ownerName']
        self.__owner_url = dictionary['ownerUrl']
        self.__data_set_count = dictionary['dataSetCount']
        self.__data_set_ids = dictionary['dataSetIDs']
        self.__field_count = dictionary['fieldCount']

        # array of data set objects
        self.__data_sets = []
        for data_set in dictionary['dataSets']:
            data_set_object = DataSet(data_set)
            self.__data_sets.append(data_set_object)

        self.__fields = []
        self.__field_ids = []
        for field in dictionary['fields']:
            field_object = Field(field['id'], field['name'], field['type'], 
                                unit=field['unit'], restrictions=field['restrictions'])
            self.__field_ids.append(field['id'])
            self.__fields.append(field_object)

    def addKey(self, credentials, label, key):
        if credentials.isUser():
            iSenseAPI.createContributorKey(credentials.getUsername(), credentials.getPassword(), self.__id, label, key)

        return self