package edu.ncsu.csc216.wolf_scheduler.course;

public class Activity {

	/** Upper possible hour */
	private static final int UPPER_HOUR = 24;
	/** Upper possible minute */
	private static final int UPPER_MINUTE = 60;
	/** Course's title. */
	protected String title;
	/** Course's meeting days */
	protected String meetingDays;
	/** Course's starting time */
	protected int startTime;
	/** Course's ending time */
	protected int endTime;

	public Activity() {
		super();
	}

	/**
	 * Returns the Course's title
	 * 
	 * @return the title
	 */
	public String getTitle() {
	    return title;
	}

	/**
	 * Sets the Course's title
	 * 
	 * @param title the title to set
	 */
	public void setTitle(String title) {
	    if (title == null || "".equals(title)) {
	        throw new IllegalArgumentException("Invalid title.");
	    }
	
	    this.title = title;
	}

	/**
	 * Returns the Course's meeting days
	 * 
	 * @return the meetingDays
	 */
	public String getMeetingDays() {
	    return meetingDays;
	}

	/**
	 * Returns the Course's start time
	 * 
	 * @return the startTime
	 */
	public int getStartTime() {
	    return startTime;
	}

	/**
	 * Returns the Course's end time
	 * 
	 * @return the endTime
	 */
	public int getEndTime() {
	    return endTime;
	}

	/**
	 * Sets the Course's meeting days
	 * 
	 * @param meetingDays the meetingDays to set
	 * @param startTime   start time of course
	 * @param endTime     end time of course
	 */
	public void setMeetingDaysAndTime(String meetingDays, int startTime, int endTime) {
	    // Check for null or empty string
	    if (meetingDays == null || "".equals(meetingDays)) {
	        throw new IllegalArgumentException("Invalid meeting days and times.");
	    }
	
	    // Handle arranged
	    if ("A".equals(meetingDays)) {
	    	if (startTime != 0 || endTime != 0) {
	    		throw new IllegalArgumentException("Invalid meeting days and times.");
	    	}
	        this.meetingDays = meetingDays;
	        this.startTime = 0;
	        this.endTime = 0;
	    } else {
	        // Check for valid characters in meeting days
	        int countM = 0;
	        int countT = 0;
	        int countW = 0;
	        int countH = 0;
	        int countF = 0;
	        for (int i = 0; i < meetingDays.length(); i++) {
	            char c = meetingDays.charAt(i);
	            switch (c) {
	            case 'M':
	                countM++;
	                break;
	            case 'T':
	                countT++;
	                break;
	            case 'W':
	                countW++;
	                break;
	            case 'H':
	                countH++;
	                break;
	            case 'F':
	                countF++;
	                break;
	            default:
	                throw new IllegalArgumentException("Invalid meeting days and times.");
	            }
	        }
	
	        if (countM > 1 || countT > 1 || countW > 1 || countH > 1 || countF > 1) {
	            throw new IllegalArgumentException("Invalid meeting days and times.");
	        }
	
	        // Check for valid meeting times
	        int startHour = startTime / 100;
	        int startMin = startTime % 100;
	        int endHour = endTime / 100;
	        int endMin = endTime % 100;
	
	        if (startHour < 0 || startHour >= UPPER_HOUR) {
	            throw new IllegalArgumentException("Invalid meeting days and times.");
	        }
	        if (startMin < 0 || startMin >= UPPER_MINUTE) {
	            throw new IllegalArgumentException("Invalid meeting days and times.");
	        }
	        if (endHour < 0 || endHour >= UPPER_HOUR) {
	            throw new IllegalArgumentException("Invalid meeting days and times.");
	        }
	        if (endMin < 0 || endMin >= UPPER_MINUTE) {
	            throw new IllegalArgumentException("Invalid meeting days and times.");
	        }
	        if (endTime < startTime) {
	            throw new IllegalArgumentException("Invalid meeting days and times.");
	        }
	
	        this.meetingDays = meetingDays;
	        this.startTime = startTime;
	        this.endTime = endTime;
	    }
	
	}

	/**
	 * Returns a string representation of the Course's meeting days and times.
	 * 
	 * @return Course's meeting days and times.
	 */
	public String getMeetingString() {
	    if ("A".equals(meetingDays)) {
	        return "Arranged";
	    }
	
	    return meetingDays + " " + getTimeString(startTime) + "-" + getTimeString(endTime);
	}

	/**
	 * Returns the time in AM/PM format.
	 * 
	 * @param time as an integer
	 * @return time as a string
	 */
	private String getTimeString(int time) {
	    int hour = time / 100;
	    int min = time % 100;
	    boolean morning = true;
	
	    if (hour >= 12) {
	        hour -= 12;
	        morning = false;
	    }
	    if (hour == 0) {
	        hour = 12;
	    }
	
	    String minS = "" + min;
	    if (min < 10) {
	        minS = "0" + minS;
	    }
	
	    String end = morning ? "AM" : "PM";
	
	    return hour + ":" + minS + end;
	}

}