package de.mpg.mpiz_koeln.featureClient.test;

import java.util.Collection;
import java.util.Map;

import org.biomoby.shared.MobyException;
import org.biomoby.shared.datatypes.AminoAcidSequence;
import org.biomoby.shared.datatypes.FASTA_AA;
import org.biomoby.shared.datatypes.MobyInteger;
import org.biomoby.shared.datatypes.MobyObject;
import org.biomoby.shared.datatypes.MobyString;

import de.mpg.mpiz_koeln.featureClient.FeatureClient;
import de.mpg.mpiz_koeln.featureClient.FeatureClientException;
import de.mpg.mpiz_koeln.featureClient.FeatureClientResult;

/**
 * This example shows how to call a service multiple times which consumes more than one input. It also shows the mixture
 * of a simple object input and a complex data type input.
 */
class MultiCallWithParameters {
    public static void main( String[] args ) throws MobyException, FeatureClientException {
        FeatureClient client = new FeatureClient();
        // the service might take longer, so we set the timeout to 120 seconds
        client.setTimeout( 120 );
        // give the service name which shall be called
        client.addService( "GetInAndOrthologsFromRefSeq" );

        // You need to know what are the inputs of the service and how the inputs are called to work properly
        // the first input of this service is the organism which is a simple object
        MobyObject organism = new MobyObject();
        // the id always contains the actual information of the object (only for the simple object type)
        organism.setId( "Arabidopsis thaliana" );
        // if the service input has a namespace set you need to do this also
        organism.setNamespace( "taxon" );
        // this is important - if a service consumes multiple inputs you have to tell each input the article name as
        // defined in the service definition. If you do not provide this, the client will throw an exception.
        organism.setName( "organism" );

        // the second input is a amino acid sequence
        AminoAcidSequence sequence = new AminoAcidSequence();
        sequence.set_SequenceString( new MobyString( "MASKKMTKSYFDVLGICCTSEVPLIENILNSMDGVKEFSVIVPSRTVIVV"
                + "HDTLILSQFQIVKALNQAQLEANVRVTGETNFKNKWPSPFAVVSGILLLL"
                + "SFFKYLYSPFRWLAVAAVVAGIYPILAKAVASLARFRIDINILVVVTVGA" ) );
        sequence.set_Length( new MobyInteger( 50 ) );
        // and also here, you have to set the name of the input parameter
        sequence.setName( "sequence" );

        // the last input is a simple object again with the name of the input gene
        MobyObject geneName = new MobyObject();
        // we just give a dummy value here and as no namespace is required we dont set any
        geneName.setId( "my_gene" );
        // and also here, you have to set the name of the input parameter
        geneName.setName( "gene_name" );

        // adds the inputs to the client and give it an identifier, so that you are able to distinguish the several
        // calls later. This identifier is completely free to choice !
        client.addMultipleCallInput( "gene1", organism, sequence, geneName );

        // we want to call the same service a second time so we build again some input values
        organism = new MobyObject();
        organism.setId( "Arabidopsis thaliana" );
        organism.setNamespace( "taxon" );
        organism.setName( "organism" );

        sequence = new AminoAcidSequence();
        sequence.set_SequenceString( new MobyString( "MASQLDLIGGDYIAGISINPPTNSRVTSVYSEVQASRIDHTLPLPSVFKT"
                + "PFKIIDGPPSSSAGHPEEIEKLFPNLFGQPSALLVPNQSNEVSSDQKLKI"
                + "GVVLSGGQAPGGHNVICGIFDYLQEYARGSSLFGFRGGPAGIMKGKYIEL" ) );
        sequence.set_Length( new MobyInteger( 50 ) );
        sequence.setName( "sequence" );

        geneName = new MobyObject();
        geneName.setId( "my_gene" );
        geneName.setName( "gene_name" );

        // adds the inputs to the client for the second call with a different identifier as before
        client.addMultipleCallInput( "gene2", organism, sequence, geneName );

        // the service is called
        Collection< FeatureClientResult > result = client.call();

        for ( FeatureClientResult mobyServiceResult : result ) {
            // the map stores for each identifier given the list of results
            Map< String, Collection< FASTA_AA > > fasta_aaMap = mobyServiceResult.getMultipleCallResult();

            // print out the results for each service call individually
            for ( String id : fasta_aaMap.keySet() ) {
                System.out.println( "FOR " + id + ":" );

                for ( FASTA_AA fasta_aa : fasta_aaMap.get( id ) ) {
                    System.out.println( fasta_aa.get_content() );
                }
                System.out.println();
            }
        }
        System.exit( 0 );
    }

}
