package uk.ac.ebi.pride.proteinidentificationindex.search.service;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import uk.ac.ebi.pride.proteinidentificationindex.search.model.ProteinIdentification;
import uk.ac.ebi.pride.proteinidentificationindex.search.model.ProteinIdentificationSummary;
import uk.ac.ebi.pride.proteinidentificationindex.search.service.repository.SolrProteinIdentificationRepository;

import java.util.*;

/**
 * @author Jose A. Dianes
 * @version $Id$
 *          <p/>
 *          NOTE: protein accessions can contain chars that produce problems in solr queries ([,],:). They are replaced by _ when
 *          using the repository methods
 */
@SuppressWarnings("unused")
@Service
public class ProteinIdentificationSearchService {

    private SolrProteinIdentificationRepository solrProteinIdentificationRepository;

    public ProteinIdentificationSearchService(SolrProteinIdentificationRepository solrProteinIdentificationRepository) {
        this.solrProteinIdentificationRepository = solrProteinIdentificationRepository;
    }

    public void setSolrProteinIdentificationRepository(SolrProteinIdentificationRepository solrProteinIdentificationRepository) {
        this.solrProteinIdentificationRepository = solrProteinIdentificationRepository;
    }

    // find by id methods
    public List<ProteinIdentification> findById(String id) {
        return solrProteinIdentificationRepository.findById(id);
    }

    public List<ProteinIdentification> findById(Collection<String> ids) {
        return solrProteinIdentificationRepository.findByIdIn(ids);
    }

    // Synonym query methods
    public List<ProteinIdentification> findBySynonymAndProjectAccession(String synonym, String projectAccession) {
        return solrProteinIdentificationRepository.findBySynonymAndProjectAccession(synonym, projectAccession);
    }

    // find by accession methods
    public List<ProteinIdentification> findByAccession(String accession) {
        return solrProteinIdentificationRepository.findByAccession(accession);
    }

    public List<ProteinIdentification> findByAccession(Collection<String> accessions) {
        return solrProteinIdentificationRepository.findByAccessionIn(accessions);
    }

    // Project accession query methods
    public List<ProteinIdentification> findByProjectAccession(String projectAccession) {
        return solrProteinIdentificationRepository.findByProjectAccession(projectAccession);
    }
    public Long countByProjectAccession(String projectAccession) {
        return solrProteinIdentificationRepository.countByProjectAccession(projectAccession);
    }

    public List<ProteinIdentification> findByProjectAccession(Collection<String> projectAccessions) {
        return solrProteinIdentificationRepository.findByProjectAccessionIn(projectAccessions);
    }

    public Page<ProteinIdentification> findByProjectAccession(String projectAccession, Pageable pageable) {
        return solrProteinIdentificationRepository.findByProjectAccession(projectAccession, pageable);
    }

    public Page<ProteinIdentification> findByProjectAccession(Collection<String> projectAccessions, Pageable pageable) {
        return solrProteinIdentificationRepository.findByProjectAccessionIn(projectAccessions, pageable);
    }

    // Assay accession query methods
    public List<ProteinIdentification> findByAssayAccession(String assayAccession) {
        return solrProteinIdentificationRepository.findByAssayAccession(assayAccession);
    }
    public Long countByAssayAccession(String assayAccession) {
        return solrProteinIdentificationRepository.countByAssayAccession(assayAccession);
    }

    public List<ProteinIdentification> findByAssayAccession(Collection<String> assayAccessions) {
        return solrProteinIdentificationRepository.findByAssayAccessionIn(assayAccessions);
    }

    public Page<ProteinIdentification> findByAssayAccession(String assayAccession, Pageable pageable) {
        return solrProteinIdentificationRepository.findByAssayAccession(assayAccession, pageable);
    }

    public Page<ProteinIdentification> findByAssayAccession(Collection<String> assayAccessions, Pageable pageable) {
        return solrProteinIdentificationRepository.findByAssayAccessionIn(assayAccessions, pageable);
    }

    // Submitted accession query methods
    public List<ProteinIdentification> findBySubmittedAccessionAndAssayAccession(String submittedAccession, String assayAccession){
        return solrProteinIdentificationRepository.findBySubmittedAccessionAndAssayAccession(submittedAccession, assayAccession);
    }

    public List<ProteinIdentificationSummary> findSummaryByProjectAccession(String projectAccession) {
        return solrProteinIdentificationRepository.findSummaryByProjectAccession(projectAccession);
    }

    public Set<String> getUniqueProteinAccessionsByProjectAccession(String projectAccession) {
        List<ProteinIdentificationSummary> results = findSummaryByProjectAccession(projectAccession);
        Set<String> accessions = new HashSet<String>(results.size());
        for (ProteinIdentificationSummary result : results) {
            accessions.add(result.getAccession());
        }
        return accessions;
    }

    public List<ProteinIdentificationSummary> findSummaryByAssayAccession(String assayAccession) {
        return solrProteinIdentificationRepository.findSummaryByAssayAccession(assayAccession);
    }

    public Set<String> getUniqueProteinAccessionsByAssayAccession(String assayAccession) {
        List<ProteinIdentificationSummary> results = findSummaryByAssayAccession(assayAccession);
        Set<String> accessions = new HashSet<String>(results.size());
        for (ProteinIdentificationSummary result : results) {
            accessions.add(result.getAccession());
        }
        return accessions;
    }
}

@@ -5161,37 +5161,41 @@
 ize());%0A        
-for (
+Iterator%3C
 ProteinIdentific
@@ -5206,36 +5206,263 @@
 nSummary
- result : results) %7B
+%3E iterator = results.iterator();%0A        while (iterator.hasNext()) %7B%0A            // in order to make it work we need to cast to the real bean before accessing data%0A            ProteinIdentification ident = (ProteinIdentification) iterator.next();
 %0A       
@@ -5473,37 +5473,36 @@
  accessions.add(
-resul
+iden
 t.getAccession()
@@ -6021,13 +6021,17 @@
     
-for (
+Iterator%3C
 Prot
@@ -6058,28 +6058,255 @@
 mary
- result : results) %7B
+%3E iterator = results.iterator();%0A        while (iterator.hasNext()) %7B%0A            // in order to make it work we need to cast to the real bean before accessing data%0A            ProteinIdentification ident = (ProteinIdentification) iterator.next();
 %0A   
@@ -6329,21 +6329,20 @@
 ons.add(
-resul
+iden
 t.getAcc

fix class cast issue with projected results