package uk.ac.reload.editor.upload.repository.uvigo;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.util.Properties;
import java.util.Vector;
import org.campusproject.components.RepositoryComponent;

/**
 * 
 * @author Gonzalo
 */
import org.osid.OsidContext;
import uk.ac.reload.editor.upload.repository.cesga.RepositoryConstantsCesga;
import uk.ac.reload.editor.upload.repository.osami.RepositoryConstantsOsami;
import uk.ac.reload.editor.upload.repository.suma.RepositoryConstantsSuma;


public class RepositoryClient {

    private RepositoryComponent re;
    private RepositoryServiceSoapBindingStub binding;
    private static RepositoryClient instance = null;

    public static RepositoryClient getInstance(String repository) {
        System.out.println("RepositoryClient getInstance");

        //otra solucion
        if (repository.equals("Suma")) {
            RepositoryConstants.RepositoryService_address =   RepositoryConstantsSuma.RepositoryService_address ;
            RepositoryConstants.namespace =  RepositoryConstantsSuma.namespace;
            RepositoryConstants.ServiceName =  RepositoryConstantsSuma.ServiceName;
        } else if (repository.equals("Cesga")) {
            RepositoryConstants.RepositoryService_address =   RepositoryConstantsCesga.RepositoryService_address ;
            RepositoryConstants.namespace =  RepositoryConstantsCesga.namespace;
            RepositoryConstants.ServiceName =  RepositoryConstantsCesga.ServiceName;
        } else if (repository.equals("Osami")) {
            RepositoryConstants.RepositoryService_address =   RepositoryConstantsOsami.RepositoryService_address ;
            RepositoryConstants.namespace =  RepositoryConstantsOsami.namespace;
            RepositoryConstants.ServiceName =  RepositoryConstantsOsami.ServiceName;
        }



       // if (instance == null) {
            instance = new RepositoryClient();
        //}
        return instance;
    }

    private RepositoryClient() {
        try {
            binding = (RepositoryServiceSoapBindingStub) new RepositoryServiceLocator().getRepositoryService();
        } catch (javax.xml.rpc.ServiceException jre) {
            if (jre.getLinkedCause() != null) {
                jre.getLinkedCause().printStackTrace();
            }
            jre.printStackTrace();
        }
    }

    public java.util.Vector<String> registerCourse(java.lang.String repository, String courseId) throws Exception {

        if (repository.compareTo("Suma") == 0) {
            try {
                OsidContext o = new OsidContext();
                re = new RepositoryComponent(o);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            java.util.Vector<String> dataVector = new Vector<String>();
            // dataVector =
            String[] aux = null;
            aux = re.registerCourse(courseId, courseId);

            for (int i = 0; i < aux.length; i++) {
                System.out.println(aux[i]);
                dataVector.add(aux[i]);
            }
            dataVector.add(courseId);

        }

        java.util.Vector<String> dataVector = null;
        dataVector = binding.registerCourse(repository);
        return dataVector;
    }

    public String getCourseURL(String courseId, java.lang.String repository) throws Exception {

        String url = null;
        if (repository.equals("Suma")) {
            url = re.getCourseURL(courseId, courseId);
        }

        try {
            url = binding.getCourseURL(courseId, repository);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return url;
    }

    public String[] getCoursesURL(java.lang.String repository) throws Exception {
        System.out.println("getCoursesURL");
        String[] urls = null;
        if (repository.equals("Suma")) {
            urls = re.getCoursesURL();
        }
        try {
            urls = binding.getCoursesURL(repository);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return urls;
    }

    public String[] getCoursesId(java.lang.String repository) throws Exception {
        String[] ids = null;
        if (repository.equals("Suma")) {
            String[][] a = re.getCoursesId();
            if (a.length > 0) {
                return a[0];
            }
        }
        try {
            ids = binding.getCoursesId(repository);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return ids;
    }

    public void endUpload(String courseId, java.lang.String repository) throws Exception {
        if (repository.equals("Suma")) {
            re.endUpload(courseId, courseId);
        }
        binding.endUpload(courseId, repository);
    }

    /*
    private static String getProperty(String propertyName) {
        InputStream inputStream = RepositoryClient.class.getResourceAsStream("/uk/ac/reload/editor/upload/repository/uvigo/prop.properties");


        Properties properties = new Properties();
        // load the inputStream using the Properties
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // get the value of the property
        String aux = properties.getProperty(propertyName);
        if (aux == null) {
            System.out.println("problema");
            return "";
        } else {
            return aux;
        }

    }
     * */
     
}
