package com.dragonsoft.tryapp.common;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import com.dragonsoft.tryapp.constants.TryConstants;

/**
 * Representation of a course. Depending on where it's used, 
 * different fields may be available.
 */
public class CourseObj implements Serializable {

    /*---- NON-OPTIONAL FIELDS ----*/

    /**
     * Unique identifier of this course across all parts of the system.
     */
    private String strCourseID;

    /**
     * The name of this course.
     */
    private String strCourseName;

    /**
     * The course number.
     */
    private String strCourseNumber;

    /**
     * The sections that make up this course (String objects).
     */
    private List strCourseSections;

    /**
     * The term that this course is scheduled for.
     */
    private String strCourseTerm;

    /*---- OPTIONAL FIELDS ----*/

    /**
     * The assignments assigned to this course (Assignment objects).
     */
    private List courseAssignments;

    /**
     * Creates a course, without any optional fields.
     * 
     * @param    courseID          the unique identifier
     * @param    courseName        the course name
     * @param    courseNumber      the course number
     * @param    courseSections    the course sections (String objects)
     * @param    courseTerm        the course term
     */
    public CourseObj(String courseID, String courseName, String courseNumber,
            List courseSections, String courseTerm) {

        setCourseID(courseID);
        setCourseName(courseName);
        setCourseNumber(courseNumber);
        setCourseSections(courseSections);
        setCourseTerm(courseTerm);

        // initialize optional fields
        setAssignments(null);
    }

    /**
     * Returns the unique identifier of this course across all 
     * parts of the system.
     * 
     * @return    the unique identifier of this course
     */
    public String getCourseID() {
        return (strCourseID);
    }

    /**
     * Sets the unique identifier of this course. Should not 
     * be changed, unless it's not available when this course instance 
     * is created, and then it needs to be set at a later point in time.
     * 
     * @param    courseID    the unique identifier
     */
    public void setCourseID(String courseID) {

        if (courseID != null) {
            strCourseID = courseID;
        } else {
            strCourseID = TryConstants.BLANK_VALUE;
        }
    }

    /**
     * Returns the name of this course.
     * 
     * @return    the name of this course
     */
    public String getCourseName() {
        return (strCourseName);
    }

    /**
     * Sets the name of this course.
     * 
     * @param    courseName    the name of this course
     */
    public void setCourseName(String courseName) {

        if (courseName != null) {
            strCourseName = courseName;
        } else {
            strCourseName = TryConstants.BLANK_VALUE;
        }
    }

    /**
     * Returns the course number.
     * 
     * @return    the course number
     */
    public String getCourseNumber() {
        return (strCourseNumber);
    }

    /**
     * Sets the course number.
     * 
     * @param    courseNumber    the course number
     */
    public void setCourseNumber(String courseNumber) {

        if (courseNumber != null) {
            strCourseNumber = courseNumber;
        } else {
            strCourseNumber = TryConstants.BLANK_VALUE;
        }
    }

    /**
     * Returns the sections that make up this course.
     * 
     * @return    the sections that make up this course (String objects)
     */
    public List getCourseSections() {
        return (strCourseSections);
    }

    /**
     * Sets the sections of this course all at once.
     * 
     * @param    courseSections    the sections of this course
     *                             (String objects)
     */
    public void setCourseSections(List courseSections) {

        if (courseSections != null) {
            strCourseSections = courseSections;
        }
        // in case there is ever a need to erase the sections
        else {
            strCourseSections = new LinkedList();
        }
    }

    /**
     * Adds the given section to this course.
     * 
     * @param    courseSection    the course section
     */
    public void addCourseSection(String courseSection) {

        if (courseSection != null) {
            strCourseSections.add(courseSection);
        }
    }

    /**
     * Returns the term that this course is scheduled for.
     * 
     * @return    the term that this course is scheduled for
     */
    public String getCourseTerm() {
        return (strCourseTerm);
    }

    /**
     * Sets the term that this course is scheduled for.
     * 
     * @param    courseTerm    the term that this course is scheduled for
     */
    public void setCourseTerm(String courseTerm) {

        if (courseTerm != null) {
            strCourseTerm = courseTerm;
        } else {
            strCourseTerm = TryConstants.BLANK_VALUE;
        }
    }

    /**
     * Returns all the assignments assigned to this course.
     * 
     * @return    all the assignments (Assignment objects)
     */
    public List getAssignments() {
        return (courseAssignments);
    }

    /**
     * Sets the assignments all at once.
     * 
     * @param    courseAssignments    the assignments (Assignment objects)
     */
    public void setAssignments(List courseAssignments) {

        if (courseAssignments != null) {
            this.courseAssignments = courseAssignments;
        }
        // in case there is ever a need to erase the assignments
        else {
            this.courseAssignments = new LinkedList();
        }
    }

    /**
     * Adds the given assignment to this course. If the assignment
     * is part of the course already it will simply update the course.
     * 
     * @param    courseAssignment    the assignment
     */
    public void addAssignment(AssignmentObj courseAssignment) {
    	Iterator iter = null;
    	boolean found = false;
        if (courseAssignment != null ) {
        	iter = courseAssignments.iterator();
        	while(iter.hasNext() && !found){
        		if(((AssignmentObj)iter.next()).getAssignmentID().equals(courseAssignment.getAssignmentID())){
        			found = true;
        			iter.remove();
        		}
        	}
        	
            courseAssignments.add(courseAssignment);
        }
    }

    /**
     * Returns the assignments viewable by students.
     * 
     * @return    the assignments viewable by students (Assignment objects)
     */
    public List getStudentAssignments() {

        List returnValue = new LinkedList();

        ListIterator assignmentIterator = courseAssignments.listIterator();
        AssignmentObj currentAssignment;
        while (assignmentIterator.hasNext()) {
            currentAssignment = (AssignmentObj) assignmentIterator.next();

            if (currentAssignment.viewableByStudents()) {
                returnValue.add(currentAssignment);
            }
        }

        return (returnValue);
    }

    /**
     * Returns the assignments with active activities.
     * 
     * @return    the assignments with active activities (Assignment objects)
     */
    public List getOpenAssignments() {

        List returnValue = new LinkedList();

        ListIterator assignmentIterator = courseAssignments.listIterator();
        AssignmentObj currentAssignment;
        while (assignmentIterator.hasNext()) {
            currentAssignment = (AssignmentObj) assignmentIterator.next();

            if (currentAssignment.hasActiveActivities()) {
                returnValue.add(currentAssignment);
            }
        }

        return (returnValue);
    }
    
    public String toString(){
    	StringBuffer buffer = new StringBuffer();
    	buffer.append("courseID=").append(this.strCourseID).append(",Name=").append(this.strCourseName).append(",Number=").append(this.strCourseNumber).append(",Sections=[");
    	for(int i =0;i<this.strCourseSections.size();i++){
    		buffer.append(this.strCourseSections.get(i));
    	}
    	buffer.append("], Term=").append(this.strCourseTerm).append("\nAssignments[");
    	for(int i =0;i < this.courseAssignments.size();i++){
    		buffer.append(this.courseAssignments.get(i)).append(",");
    	}
    	buffer.append("]");
    	return buffer.toString();
    }

} // Course