package agenda;

import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by gjoosen on 06/02/15.
 */
public class ActTime implements Serializable {
    
	private GregorianCalendar beginTime, endTime;

    public ActTime(int y1, int m1, int d1, int hh1, int mm1, int y2, int m2, int d2, int hh2, int mm2){
    	beginTime = new GregorianCalendar();
    	endTime = new GregorianCalendar();
    	setBeginTime(y1, m1, d1, hh1, mm1);
    	setEndTime(y2, m2, d2, hh2, mm2);
    }
    
    public ActTime(String beginTime, String endTime){
        this.beginTime = new GregorianCalendar();
        String[] timeParts = beginTime.split("-");
        int y1 = Integer.valueOf(timeParts[0]);
        int m1 = Integer.valueOf(timeParts[1]);
        int d1 = Integer.valueOf(timeParts[2]);
        int hh1 = Integer.valueOf(timeParts[3]);
        int mm1 = Integer.valueOf(timeParts[4]);
        
        this.endTime = new GregorianCalendar();
        String[] timeParts2 = endTime.split("-");
        int y2 = Integer.valueOf(timeParts2[0]);
        int m2 = Integer.valueOf(timeParts2[1]);
        int d2 = Integer.valueOf(timeParts2[2]);
        int hh2 = Integer.valueOf(timeParts2[3]);
        int mm2 = Integer.valueOf(timeParts2[4]);

        this.setBeginTime(y1, m1, d1, hh1, mm1);
        this.setEndTime(y2, m2, d2, hh2, mm2);
    }
    
    public void setBeginTime(int y, int m, int d, int hh, int mm){
    	beginTime.set(y,m-1,d,hh,mm);
    }
    
    public void setEndTime(int y, int m, int d, int hh, int mm){
    	endTime.set(y,m-1,d,hh,mm);
    }
    
    public String getBeginTimeString(){
    	String minutes = "";
    	if(beginTime.get(Calendar.MINUTE)>=0 && beginTime.get(Calendar.MINUTE)<10){
    		minutes = "0"+beginTime.get(Calendar.MINUTE);
    	} else {
    		minutes = ""+beginTime.get(Calendar.MINUTE);
    	}
    	
    	return 	beginTime.get(Calendar.YEAR) + "-" + 
    			(beginTime.get(Calendar.MONTH)+1) + "-" + 
    			beginTime.get(Calendar.DATE) + " " + 
    			beginTime.get(Calendar.HOUR_OF_DAY) + ":" +
    			minutes;
    }

    public String getBeginTimeStringField(){
        String minutes = "";
        if(beginTime.get(Calendar.MINUTE)>=0 && beginTime.get(Calendar.MINUTE)<10){
            minutes = "0"+beginTime.get(Calendar.MINUTE);
        } else {
            minutes = ""+beginTime.get(Calendar.MINUTE);
        }

        return 	beginTime.get(Calendar.YEAR) + "-" +
                (beginTime.get(Calendar.MONTH)+1) + "-" +
                beginTime.get(Calendar.DATE) + "-" +
                beginTime.get(Calendar.HOUR_OF_DAY) + "-" +
                minutes;
    }
    
    public String getEndTimeString(){
    	String minutes = "";
    	if(endTime.get(Calendar.MINUTE)>=0 && endTime.get(Calendar.MINUTE)<10){
    		minutes = "0"+endTime.get(Calendar.MINUTE);
    	} else {
    		minutes = ""+endTime.get(Calendar.MINUTE);
    	}
    	return 	endTime.get(Calendar.YEAR) + "-" + 
    			(endTime.get(Calendar.MONTH)+1) + "-" + 
    			endTime.get(Calendar.DATE) + " " + 
    			endTime.get(Calendar.HOUR_OF_DAY) + ":" +
    			minutes;
    }

    public String getEndTimeStringField(){
        String minutes = "";
        if(endTime.get(Calendar.MINUTE)>=0 && endTime.get(Calendar.MINUTE)<10){
            minutes = "0"+endTime.get(Calendar.MINUTE);
        } else {
            minutes = ""+endTime.get(Calendar.MINUTE);
        }
        return 	endTime.get(Calendar.YEAR) + "-" +
                (endTime.get(Calendar.MONTH)+1) + "-" +
                endTime.get(Calendar.DATE) + "-" +
                endTime.get(Calendar.HOUR_OF_DAY) + "-" +
                minutes;
    }
    
    public GregorianCalendar getBeginTime() {
		return beginTime;
	}

	public GregorianCalendar getEndTime() {
		return endTime;
	}
  
    public int getLength(){
    	return (int) ((endTime.getTimeInMillis()/60000)-(beginTime.getTimeInMillis()/60000));
    }
  
    @Override
    public String toString(){
        return "Start time: " + this.getBeginTimeString() + "<br/>End time: " + this.getEndTimeString() + "<br/>Length: " + getLength() + " Minutes";
    }
}
