from flask import request, Response
from db.repository import Repository
from processing.similarityFiles.miscFunctions import convertSimilarityDictToJSON
#from db.entities import clusterset #REMOVE?

repo = Repository()

def get_similarity(layer_name,batchNr): 
    ''' Gets cluster_similarity from the database.

        :returns: Returns similarity objects from the DB
        :rtype: Dict
    '''  
    batchSize = 1000
    if int(batchNr)<0:
        print("Batch number needs to be a positive integer")
        return Response(status=404)
    skipNr = batchSize*int(batchNr)
    #get_similarity(self,skipNr,batchSize, cluster_layer: str= None, run_id: str=None)
    result = repo.get_similarity(skipNr, batchSize, layer_name)
    if result is None or len(result) == 0:        
        print("MongoDb Get Error: Response 404")
        return Response(status=404)
    else:
        return result

def get_similarity_use_case(use_case,batchNr): 
    ''' Gets cluster_similarity from the database.

        :returns: Returns similarity objects from the DB
        :rtype: Dict
    '''  
    batchSize = 1000
    if int(batchNr)<0:
        print("Batch number needs to be a positive integer")
        return Response(status=404)
    skipNr = batchSize*int(batchNr)
    #get_similarity(self,skipNr,batchSize, cluster_layer: str= None, run_id: str=None)
    result = repo.get_similarity_use_case(skipNr, batchSize, use_case)
    if result is None or len(result) == 0:        
        print("MongoDb Get Error: Response 404")
        return Response(status=404)
    else:
        return result

def get_similarity_table(use_case,table,batchNr): 
    ''' Gets cluster_similarity from the database.

        :returns: Returns similarity objects from the DB
        :rtype: Dict
    '''  
    batchSize = 1000
    if int(batchNr)<0:
        print("Batch number needs to be a positive integer")
        return Response(status=404)
    skipNr = batchSize*int(batchNr)
    #get_similarity(self,skipNr,batchSize, cluster_layer: str= None, run_id: str=None)
    result = repo.get_similarity_table(skipNr, batchSize, use_case,table)
    if result is None or len(result) == 0:        
        print("MongoDb Get Error: Response 404")
        return Response(status=404)
    else:
        return result

def get_similarity_layer(use_case,table,layer_name,batchNr): 
    ''' Gets cluster_similarity from the database.

        :returns: Returns similarity objects from the DB
        :rtype: Dict
    '''  
    batchSize = 1000
    if int(batchNr)<0:
        print("Batch number needs to be a positive integer")
        return Response(status=404)
    skipNr = batchSize*int(batchNr)
    #get_similarity(self,skipNr,batchSize, cluster_layer: str= None, run_id: str=None)
    result = repo.get_similarity_layer(skipNr, batchSize,use_case,table, layer_name)
    if result is None or len(result) == 0:        
        print("MongoDb Get Error: Response 404")
        return Response(status=404)
    else:
        return result
