package com.dragonsoft.tryapp.sysinterface;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.dragonsoft.tryapp.common.ActivityObj;
import com.dragonsoft.tryapp.common.AssignmentObj;
import com.dragonsoft.tryapp.common.CourseObj;
import com.dragonsoft.tryapp.common.UserCredentials;
import com.dragonsoft.tryapp.common.exceptions.AccessDeniedException;
import com.dragonsoft.tryapp.common.exceptions.TryException;
import com.dragonsoft.tryapp.ejb.session.interfaces.CourseController;
import com.dragonsoft.tryapp.ejb.session.interfaces.CourseControllerHome;
import com.dragonsoft.tryapp.ejb.session.interfaces.ProcessMonitor;
import com.dragonsoft.tryapp.ejb.session.interfaces.ProcessMonitorHome;

import fireTester.messages.TestUnitResults;
import fireTester.messages.TesterException;

/**
 * Class used to find which roles can be associated with a user. The role of 
 * the user within the course is designated by the by the mechanism used to 
 * receive the course data.
 * 
 * @author    Greg, Jean-Paul
 */
public class DisplayWorker {

    private CourseControllerHome courseControllerHome;
    private ProcessMonitorHome processHome;
    
    public DisplayWorker() {
        init();
    }

    public CourseObj[] getGraderPerspective(UserCredentials creds)
            throws AccessDeniedException {

        CourseObj[] rtVal = null;
        Collection temp = null;
        CourseController ejbRemote = null;
        try {

            ejbRemote = courseControllerHome.create();
            temp = ejbRemote.getGraderCourses(creds);
            if (temp.size() > 0) {
                rtVal = new CourseObj[temp.size()];
                temp.toArray(rtVal);
            }
            ejbRemote.remove();

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (CreateException e) {
            e.printStackTrace();
        } catch (RemoveException e) {
            e.printStackTrace();
        }

        return rtVal;
    }

    public CourseObj[] getInstructorPerspective(UserCredentials creds)
            throws AccessDeniedException {

        CourseObj[] rtVal = null;
        Collection temp = null;
        CourseController ejbRemote = null;
        try {
            ejbRemote = courseControllerHome.create();
            temp = ejbRemote.getProfessorCourses(creds);
            if (temp.size() > 0) {
                rtVal = new CourseObj[temp.size()];
                temp.toArray(rtVal);
            }
            ejbRemote.remove();

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (CreateException e) {
            e.printStackTrace();
        } catch (RemoveException e) {
            e.printStackTrace();
        }

        return rtVal;
    }

    public CourseObj[] getStudentPerspective(UserCredentials creds)
            throws TryException {

        CourseObj[] returnValue = null;
        Collection people = null;
        CourseController ejbRemote;
        try {
            ejbRemote = courseControllerHome.create();
            people = ejbRemote.getStudentCourses(creds);
            if (people.size() > 0) {
                returnValue = new CourseObj[people.size()];
                people.toArray(returnValue);
            }
            ejbRemote.remove();
            if (returnValue == null)
                throw new TryException();

        } catch (RemoteException e) {
            e.printStackTrace();
            init();
        } catch (CreateException e) {
            e.printStackTrace();
            init();
        } catch (RemoveException e) {
            e.printStackTrace();
        }
        for(int i = 0 ; i < returnValue.length;i++){
        	System.out.println("COURSE#"+(i+1)+returnValue[i]);
        }
        return returnValue;

    }

    public ActivityObj getStudentActivity(UserCredentials usrCred,
            String strActivityID) {

        ActivityObj returnValue = null;

        // use this to get the student's username
        String strUsername = usrCred.getUsername();
        try {
        	ActivityObj swap = null;
        	CourseObj courseObj = null;
			CourseController course = courseControllerHome.create();
			List assigns = null;
			List acts = null;
			List crs = course.getStudentCourses(usrCred);
			course.remove();
			Iterator iter = crs.iterator(), iter2,iter3;
			while(iter.hasNext()&& returnValue==null){
				courseObj = (CourseObj)iter.next();
				assigns = courseObj.getAssignments();
				iter2=assigns.iterator();
				while(iter2.hasNext() && returnValue==null){
					acts = ((AssignmentObj)iter2.next()).getActivities();
					iter3 = acts.iterator();
					while(iter3.hasNext() && returnValue==null){
						if((swap =(ActivityObj)iter3.next()).getActivityID().equals(strActivityID)){
							returnValue=swap;
						}
					}
				}
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			init();
		} catch (CreateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoveException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (returnValue);
    }
    
    public TestUnitResults[] getResults(String subID)throws TesterException{
    	ProcessMonitor process;
    	TestUnitResults[] rtVal = null;
		try {
			process = processHome.create();
			rtVal= process.getResults(subID);
			process.remove();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		} catch (RemoveException e) {
			e.printStackTrace();
		}
		 
    	return rtVal;
    }

    private void init() {

        try {
            Object o = null;
            Context ctx = new InitialContext();
            o = ctx.lookup(CourseControllerHome.JNDI_NAME);
            courseControllerHome = (CourseControllerHome) PortableRemoteObject.narrow(o,
                    CourseControllerHome.class);
            o = ctx.lookup(ProcessMonitorHome.JNDI_NAME);
            processHome = (ProcessMonitorHome)PortableRemoteObject.narrow(o,ProcessMonitorHome.class);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

} // DisplayWorker