/*******************************************************************
 * 
 * Copyright (C) 2013 Kevin Reverchon
 * This file/program is part of gZFS free software
 * See COPYING file for details
 * 
 *******************************************************************/
package gZFS;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.List;


public class zsnapshots {

	public Hashtable<String,zProperties> zProperties = new Hashtable<String, zProperties>();
	public Hashtable<String,zProperties> zSchedulerProperties = new Hashtable<String, zProperties>();

	private Hashtable<Object, Object> cronMinute = new Hashtable<Object, Object>();
	private Hashtable<Object, Object> cronHour= new Hashtable<Object, Object>();
	private Hashtable<Object, Object> cronMonth= new Hashtable<Object, Object>();
	private Hashtable<Object, Object> cronDayOfMonth = new Hashtable<Object, Object>();
	private Hashtable<Object, Object> cronDayOfWeek = new Hashtable<Object, Object>();
	
	zsnapshots(){
		String valueForHash = null;
		for (int i=1;i<61;i++){
			
			if ( i<10){
				valueForHash="0"+String.valueOf(i);
			}else{
				valueForHash=String.valueOf(i);
			}
			cronMinute.put(String.valueOf(i), valueForHash);
			if ( i ==1){
				cronMinute.put("*", "All");
			}else{
				cronMinute.put("*/"+i, "Every "+valueForHash+" Minutes");	
			}
		}
		
		for (int i=0;i<24;i++){
			if ( i<10){
				valueForHash="0"+String.valueOf(i);
			}else{
				valueForHash=String.valueOf(i);
			}
			cronHour.put(String.valueOf(i), valueForHash);
			if ( i ==1){
				cronHour.put("*", "All");
			}else{
				cronHour.put("*/"+i, "Every "+valueForHash+" Hours");	
			}
		}
		
		cronMonth.put("01", "January");
		cronMonth.put("02", "February");
		cronMonth.put("03", "March");
		cronMonth.put("04", "April");
		cronMonth.put("05", "May");
		cronMonth.put("06", "June");
		cronMonth.put("07", "July");
		cronMonth.put("08", "August");
		cronMonth.put("09", "September");
		cronMonth.put("10", "October");
		cronMonth.put("11", "November");
		cronMonth.put("12", "December");
		cronMonth.put("*", "All");
		
		cronDayOfWeek.put("0", "Sunday");
		cronDayOfWeek.put("1", "Monday");
		cronDayOfWeek.put("2", "Tuesday");
		cronDayOfWeek.put("3", "Wednesday");
		cronDayOfWeek.put("4", "Thursday");
		cronDayOfWeek.put("5", "Friday");
		cronDayOfWeek.put("6", "Saturday");
		cronDayOfWeek.put("*", "All");
		
		
		for (int i=1;i<31;i++){
			if ( i<10){
				valueForHash="0"+String.valueOf(i);
			}else{
				valueForHash=String.valueOf(i);
			}
			cronDayOfMonth.put(String.valueOf(i), valueForHash);
			if ( i ==1){
				cronDayOfMonth.put("*", "All");
			}else{
				cronDayOfMonth.put("*/"+i, "Every "+valueForHash+" Days");	
			}
		}

		Hashtable<Object, Object> permitValueNameVolFS = new Hashtable<Object, Object>();
		zProperties zPropertyNameVolFS = new zProperties();
		permitValueNameVolFS.clear();
		zPropertyNameVolFS.setNameProperty("name");
		zPropertyNameVolFS.setLabelProperty("Snapshot Name");
		zPropertyNameVolFS.setType("noupdateable");
		zPropertyNameVolFS.setValue("");
		zPropertyNameVolFS.setPermitValue(null);
		zPropertyNameVolFS.setMultivalues(null);
		zSchedulerProperties.put("name", zPropertyNameVolFS);
		
		Hashtable<Object, Object> permitValueFileConf = new Hashtable<Object, Object>();
		zProperties zPropertyFileConf = new zProperties();
		permitValueFileConf.clear();
		zPropertyFileConf.setNameProperty("fileconf");
		zPropertyFileConf.setLabelProperty("Configuration File");
		zPropertyFileConf.setType("noupdateable");
		zPropertyFileConf.setValue("");
		zPropertyFileConf.setPermitValue(null);
		zPropertyFileConf.setMultivalues(null);
		zSchedulerProperties.put("fileconf", zPropertyFileConf);

		Hashtable<Object, Object> permitValueType = new Hashtable<Object, Object>();
		zProperties zPropertyType = new zProperties();
		permitValueType.clear();
		zPropertyType.setNameProperty("type");
		zPropertyType.setLabelProperty("Snapshot Type");
		zPropertyType.setType("noupdateable");
		zPropertyType.setValue("");
		zPropertyType.setPermitValue(null);
		zPropertyType.setMultivalues(null);
		zSchedulerProperties.put("type", zPropertyType);

		Hashtable<Object, Object> permitValueNbSnapshots = new Hashtable<Object, Object>();
		zProperties zPropertyNbSnapshots = new zProperties();
		permitValueNbSnapshots.clear();
		zPropertyNbSnapshots.setNameProperty("nbsnapshots");
		zPropertyNbSnapshots.setLabelProperty("Number Snapshots to keep");
		zPropertyNbSnapshots.setType("freeentry");
		zPropertyNbSnapshots.setValue("");
		zPropertyNbSnapshots.setPermitValue(null);
		zPropertyNbSnapshots.setMultivalues(null);
		zSchedulerProperties.put("nbsnapshots", zPropertyNbSnapshots);

		Hashtable<Object, Object> permitValueMinutesOfSnapshots = new Hashtable<Object, Object>();
		zProperties zPropertyMinutesOfSnapshots = new zProperties();
		permitValueMinutesOfSnapshots.clear();
		zPropertyMinutesOfSnapshots.setNameProperty("minutesofsnapshots");
		zPropertyMinutesOfSnapshots.setLabelProperty("Minutes of snapshots [0-60]");
		zPropertyMinutesOfSnapshots.setType("nofreeentry");
		zPropertyMinutesOfSnapshots.setValue("");
		zPropertyMinutesOfSnapshots.setPermitValue(cronMinute);
		zPropertyMinutesOfSnapshots.setMultivalues(null);
		zSchedulerProperties.put("minutesofsnapshots", zPropertyMinutesOfSnapshots);

		Hashtable<Object, Object> permitValueSnapshotsHours = new Hashtable<Object, Object>();
		zProperties zPropertySnapshotsHours = new zProperties();
		permitValueSnapshotsHours.clear();
		zPropertySnapshotsHours.setNameProperty("hoursofsnapshots");
		zPropertySnapshotsHours.setLabelProperty("Hour of snapshots [0-23]");
		zPropertySnapshotsHours.setType("nofreeentry");
		zPropertySnapshotsHours.setValue("");
		zPropertySnapshotsHours.setPermitValue(cronHour);
		zPropertySnapshotsHours.setMultivalues(null);
		zSchedulerProperties.put("hoursofsnapshots", zPropertySnapshotsHours);

		Hashtable<Object, Object> permitValueDayOfWeekSnapshots = new Hashtable<Object, Object>();
		zProperties zPropertyDayOfWeekSnapshots = new zProperties();
		permitValueDayOfWeekSnapshots.clear();
		zPropertyDayOfWeekSnapshots.setNameProperty("dayofweeksnapshots");
		zPropertyDayOfWeekSnapshots.setLabelProperty("Day of week of Snapshots [0-6] 0 for Sunday");
		zPropertyDayOfWeekSnapshots.setType("nofreeentry");
		zPropertyDayOfWeekSnapshots.setValue("");
		zPropertyDayOfWeekSnapshots.setPermitValue(cronDayOfWeek);
		zPropertyDayOfWeekSnapshots.setMultivalues(null);
		zSchedulerProperties.put("dayofweeksnapshots", zPropertyDayOfWeekSnapshots);

		Hashtable<Object, Object> permitValueDayOfMonthSnapshots = new Hashtable<Object, Object>();
		zProperties zPropertyDayOfMonthSnapshots = new zProperties();
		permitValueDayOfMonthSnapshots.clear();
		zPropertyDayOfMonthSnapshots.setNameProperty("dayofmonthsnapshots");
		zPropertyDayOfMonthSnapshots.setLabelProperty("Day of Month of Snapshots [1-30]");
		zPropertyDayOfMonthSnapshots.setType("nofreeentry");
		zPropertyDayOfMonthSnapshots.setValue("");
		zPropertyDayOfMonthSnapshots.setPermitValue(cronDayOfMonth);
		zPropertyDayOfMonthSnapshots.setMultivalues(null);
		zSchedulerProperties.put("dayofmonthsnapshots", zPropertyDayOfMonthSnapshots);

		Hashtable<Object, Object> permitValueMonthSnapshots = new Hashtable<Object, Object>();
		zProperties zPropertyMonthSnapshots = new zProperties();
		permitValueMonthSnapshots.clear();
		zPropertyMonthSnapshots.setNameProperty("monthsnapshots");
		zPropertyMonthSnapshots.setLabelProperty("Month");
		zPropertyMonthSnapshots.setType("nofreeentry");
		zPropertyMonthSnapshots.setValue("");
		zPropertyMonthSnapshots.setPermitValue(cronMonth);
		zPropertyMonthSnapshots.setMultivalues(null);
		zSchedulerProperties.put("monthsnapshots", zPropertyMonthSnapshots);

		/*Hashtable<Object, Object> permitValueFreqSnapshots = new Hashtable<Object, Object>();
		zProperties zPropertyFreqSnapshots = new zProperties();
		permitValueFreqSnapshots.clear();
		zPropertyFreqSnapshots.setNameProperty("frequency");
		zPropertyFreqSnapshots.setLabelProperty("Frequency");
		zPropertyFreqSnapshots.setType("internaluse");
		zPropertyFreqSnapshots.setValue("");
		zPropertyFreqSnapshots.setPermitValue(null);
		zSchedulerProperties.put("frequency", zPropertyFreqSnapshots);*/


	}

	public void formatCronValue(String frequency, String DaySnapshot){
		String cronValue ="";

		if (frequency.contains("Hours")){
			this.zSchedulerProperties.get("minutesofsnaphots").setValue("*");
			this.zSchedulerProperties.get("hoursofsnaphots").setValue("*/"+frequency.replaceAll("Every", "").replaceAll("Hours", ""));
			this.zSchedulerProperties.get("dayofmonthsnapshots").setValue("*");
			this.zSchedulerProperties.get("monthsnapshots").setValue("*");
			this.zSchedulerProperties.get("dayofweeksnapshots").setValue("*");

		}else{
			this.zSchedulerProperties.get("hour").setValue(this.zSchedulerProperties.get("time").getValue().split(":")[0]);
			this.zSchedulerProperties.get("minutes").setValue(this.zSchedulerProperties.get("time").getValue().split(":")[1]);
			if (frequency.contains("Days")){
				this.zSchedulerProperties.get("dayofmonthsnapshots").setValue("*/"+frequency.replaceAll("Every", "").replaceAll("Days", ""));
				this.zSchedulerProperties.get("monthsnapshots").setValue("*");
				this.zSchedulerProperties.get("dayofweeksnapshots").setValue("*");

			}else{
				if (frequency.contains("Months")){
					this.zSchedulerProperties.get("dayofmonthsnapshots").setValue(this.zSchedulerProperties.get("dayofmonthsnapshots").getValue());
					this.zSchedulerProperties.get("monthsnapshots").setValue("*/"+frequency.replaceAll("Every", "").replaceAll("Months", ""));
					this.zSchedulerProperties.get("dayofweeksnapshots").setValue("*");

				}else{
					if (frequency.contains("Hour")){
						this.zSchedulerProperties.get("minutes").setValue("*");
						this.zSchedulerProperties.get("hours").setValue("*/"+frequency.replaceAll("Every", "").replaceAll("Hour", ""));
						this.zSchedulerProperties.get("dayofmonthsnapshots").setValue("*");
						this.zSchedulerProperties.get("monthsnapshots").setValue("*");
						this.zSchedulerProperties.get("dayofweeksnapshots").setValue("*");
					}else{
						if (frequency.contains("Day")){
							this.zSchedulerProperties.get("dayofmonthsnapshots").setValue("*/"+frequency.replaceAll("Every", "").replaceAll("Day", ""));
							this.zSchedulerProperties.get("monthsnapshots").setValue("*");
							this.zSchedulerProperties.get("dayofweeksnapshots").setValue("*");
						}else{
							if (frequency.contains("Month")){
								this.zSchedulerProperties.get("dayofmonthsnapshots").setValue(this.zSchedulerProperties.get("dayofmonthsnapshots").getValue());
								this.zSchedulerProperties.get("monthsnapshots").setValue("*/"+frequency.replaceAll("Every", "").replaceAll("Month", ""));
								this.zSchedulerProperties.get("dayofweeksnapshots").setValue("*");
							}
						}
					}
				}
			}
		}
	}

	public static int getIntDayOfWeek(String dayOfWeek)  
	{  
		try   
		{   
			DateFormat formatter ;   
			Date date ;   
			formatter = new SimpleDateFormat("EEE");  
			date = (Date)formatter.parse(dayOfWeek);    
			GregorianCalendar g = new GregorianCalendar();  
			g.setTime(date);  
			return g.get(Calendar.DAY_OF_WEEK);  
		}   
		catch (ParseException e)  
		{  
			System.out.println("Exception :"+e);    
		}    
		return 0;  
	}
}

