//package lu.tudor.santec.gecamed.agenda.ejb.session.beans;
//
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.text.DateFormat;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.Collection;
//import java.util.Date;
//import java.util.regex.Pattern;
//
//import javax.ejb.EJB;
//import javax.ejb.Stateful;
//import javax.ejb.TransactionManagement;
//import javax.ejb.TransactionManagementType;
//import javax.persistence.EntityManager;
//import javax.persistence.PersistenceContext;
//
//import org.apache.log4j.Logger;
//
//import bizcal.util.DateUtil;
//import lu.tudor.santec.gecamed.agenda.ejb.entity.beans.AgendaCalendar;
//import lu.tudor.santec.gecamed.agenda.ejb.entity.beans.Appointment;
//import lu.tudor.santec.gecamed.agenda.ejb.entity.beans.AppointmentType;
//import lu.tudor.santec.gecamed.agenda.ejb.session.interfaces.AppointmentManager;
//import lu.tudor.santec.gecamed.agenda.ejb.session.interfaces.SMSSendingInterface;
//import lu.tudor.santec.gecamed.agenda.utils.AgendaAdminSettingsConstants;
//import lu.tudor.santec.gecamed.agenda.utils.DoctenaFTPManager;
//import lu.tudor.santec.gecamed.agenda.utils.IcalImportExporter;
//import lu.tudor.santec.gecamed.core.ejb.entity.beans.PhoneType;
//import lu.tudor.santec.gecamed.core.ejb.session.beans.GECAMedSessionBean;
//import lu.tudor.santec.gecamed.core.utils.ManagerFactory;
//import lu.tudor.santec.gecamed.patient.ejb.entity.beans.Patient;
//import lu.tudor.santec.gecamed.patient.ejb.entity.beans.PatientPhone;
//import lu.tudor.santec.gecamed.patient.ejb.session.beans.PatientAdminBean;
//import lu.tudor.santec.gecamed.patient.ejb.session.interfaces.PatientAdminInterface;
//import lu.tudor.santec.gecamed.patient.gui.PatientManagerModule;
//import lu.tudor.santec.gecamed.usermanagement.ejb.session.interfaces.LoginInterface;
//
//@Stateful
//@TransactionManagement (TransactionManagementType.BEAN)
//public class SMSSendingBean extends GECAMedSessionBean implements SMSSendingInterface {
//
//	@PersistenceContext(unitName = "gecam")
//	EntityManager em;
//
//
//	@EJB
//	AppointmentManager appManager;
//
//	private AgendaCalendar agenda;
//	private PatientAdminInterface manager;
//
//	/**
//	 * static logger for this class
//	 */
//	private static Logger logger = Logger.getLogger(DoctenaSyncBean.class.getName());
//
//	public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	public static final DateFormat TIMESTAMP = new SimpleDateFormat("yyyyMMddHHmmss");
//
//	@EJB
//	LoginInterface login;
//	
//private Boolean enabled=false;
//private String days;
//
//	public void sendReminders(){
//		manager = (PatientAdminInterface) ManagerFactory.getRemote(PatientAdminBean.class);
//		
//		this.enabled = 	(Boolean) this.login.getAdminSettingValue(AgendaAdminSettingsConstants.NAME, AgendaAdminSettingsConstants.SMS_ENABLED, AgendaAdminSettingsConstants.DEFAULT_SMS_ENABLED);
//		this.days = 	(String) this.login.getAdminSettingValue(AgendaAdminSettingsConstants.NAME, AgendaAdminSettingsConstants.SMS_DAYS, AgendaAdminSettingsConstants.DEFAULT_SMS_DAYS);
//		
//		if(this.enabled)
//		{	
//		System.out.println("--------- Send Reminders method ");
//		
//		//Check in the db if appointments coming
//
//		Collection<AgendaCalendar> cals 	= appManager.getCalendars();
//		Collection<AppointmentType> types 	= appManager.getAppointmentTypes(null, false);
//
//		
//		System.out.println(cals.size());
//		
//		for(AgendaCalendar cal : cals)
//		{
//			System.out.println("---------calendar : "+cal.getTitle());
//			
//			Date today=DateUtil.move2Midnight(new Date());
//			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
//			Calendar c = Calendar.getInstance();
//			c.setTime(today); // Now use today date.
//			c.add(Calendar.DATE, Integer.valueOf(this.days)); // Adding 3 days
//			
//			Date toDate=DateUtil.move2Midnight(c.getTime());
//			
//			
//			
//			Collection<Appointment> appointmentList =appManager.getAppointments(cal.getId(), today, toDate,null);
//			for(Appointment app : appointmentList)
//			{
//				System.out.println("appointment :\n--------------"
//						+ "\n "+app.toString()
//						+ "\n "+app.getStartDate().toString());
//		
//				
//				
//				//Check if sms already send
//				//Otherwise send one
//				
//				if(app.getNotifyDate()==null)
//				{
//				
//					Patient patient = null;
//
//					if(app.getPatientId()!=null)
//					{
//				
//						try {
//							patient = manager.getPatient(app.getPatientId());
//							PatientPhone phone = null ;
//							
//							if(patient.getPhone(PhoneType.MOBILE)!=null)
//							{					
//								phone = patient.getPhone(PhoneType.MOBILE);
//								
//							} 
//							else if (patient.getPhone(PhoneType.HOME)!=null)
//							{
//								phone = patient.getPhone(PhoneType.HOME);
//								
//							}
//							else if (patient.getPhone(PhoneType.OFFICE)!=null)
//							{
//								phone = patient.getPhone(PhoneType.OFFICE);
//								
//							}
//							if(phone!=null)
//							{		
//								System.out.println("IF NOT NULL");
//								String number= phone.getNumber();
//								String message = "Event on "+app.getStartDate().toString()+" : "+app.getSummary();
//								
//								//Remove all spaces and replace with html equivalent
//								message = message.replaceAll("\\s", "&nbsp;");
//								//Format the phone number
//								number = number.replaceAll("\\s", "");
//								
//								//Number format has to be Country PREFIX (XX) + Number
//								// => XX123 456789
//								//format mobile phone and message : http://api.clickatell.com/http/sendmsg?user=danyleduc&password=ReaQebPKJXaEPP&api_id=3576234&to=mobile: 188 99 765&text=Event on Thu May 01 13:15:00 CEST 2014 : EXPORTER, The
// 
//								String url = "http://api.clickatell.com/http/sendmsg?user=danyleduc&password=ReaQebPKJXaEPP&api_id=3576234&to="+number+"&text="+message;
//	
//								URL obj = new URL(url);
//								HttpURLConnection con;
//	
//								con = (HttpURLConnection) obj.openConnection();
//	
//	
//								// optional default is GET
//								con.setRequestMethod("GET");
//	
//								//add request header
//								//	con.setRequestProperty("User-Agent", USER_AGENT);
//	
//								int responseCode = con.getResponseCode();
//								System.out.println("\nSending 'GET' request to URL : " + url);
//								System.out.println("Response Code : " + responseCode);
//	
//								BufferedReader in = new BufferedReader(
//										new InputStreamReader(con.getInputStream()));
//								String inputLine;
//								StringBuffer response = new StringBuffer();
//	
//								while ((inputLine = in.readLine()) != null) {
//									response.append(inputLine);
//								}
//								in.close();
//								
//	
//								System.out.println(response.toString());
//							//	agenda = appManager.saveCalendar(null, agenda)
//							
//								//Set the apointment as notified
//								app.setNotifyDate(new Date());
//								appManager.saveAppointment(app);
//								
//							}
//						}
//						catch (Exception e) {
//							System.out.println("Exception :\n");
//							System.out.println(e.getMessage());
//						}
//
//					}
//					else
//					{
//						System.out.println("----- NO PATIENT");
//					}
//				}
//				else
//				{
//					System.out.println("----- Appointment already notified");
//				}
//			}	
//		}
//		}	
//	}
//}
