package main_package;
/*package finance;


import java.io.Serializable;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

@ManagedBean(name="trialPeriods")
@ViewScoped
public class TrialPeriodBean implements Serializable{
	DataBase obj=new DataBase();
	private static final long serialVersionUID = 1L;
	
	Date mydate,mydate2;
	Date cruntdate;
	
	public Date getMydate2() {
		
		return mydate2;
	}

	public void setMydate2(Date mydate2) {
		this.mydate2 = mydate2;
	}

	public Date getMydate() {
		return mydate;
	}

	public TrialPeriodBean() 
	{
//		// System.out.println("trial period constr.");
//		String realPath=FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
//		cruntdate=new Date();
//		
//		File file=new File(realPath+"abcd.a");
//
//		String decrypted=null;
//
//		FileInputStream fileInputStream;
//		try {
//			if(file.exists())
//			{
//				fileInputStream = new FileInputStream(file);
//				int buffer=0;
//				byte[] bytesFromFile;
//				bytesFromFile=new byte[fileInputStream.available()];
//				for(int i=0;(buffer=fileInputStream.read())!=-1;i++)
//				{
//				     bytesFromFile[i]=(byte)buffer;
//				}
//				
//				fileInputStream.close();
//				decrypted=new Encryption().decryptWithGivenKey(bytesFromFile, symmetric_key.getBytes());
//
//		        String dateperiodString=decrypted;
//		        
//		        String startDateString=dateperiodString.substring(0,dateperiodString.indexOf(" to "));
//		        String endDateString=dateperiodString.substring(dateperiodString.indexOf(" to ")+4);
//		        
//		        
//		        SimpleDateFormat dateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
//		        
//				Date startDate= dateFormat.parse(startDateString);
//				Date endDate=dateFormat.parse(endDateString);
//	            
//				SimpleDateFormat  dateFormat2=new SimpleDateFormat("dd/MM/yyyy");
//				subscriptionDetail= dateFormat2.format(startDate) + " to "+dateFormat2.format(endDate);
//				
//			}
//			else
//			{
////				file.createNewFile();
////				
////				fileInputStream = new FileInputStream(file);
////				int buffer=0;
////				byte[] bytesFromFile;
////				bytesFromFile=new byte[fileInputStream.available()];
////				
////				for(int i=0;(buffer=fileInputStream.read())!=-1;i++)
////				{
////				     bytesFromFile[i]=(byte)buffer;
////				}
////				
////				fileInputStream.close();
////				
////				decrypted=new Encryption().decryptWithGivenKey(bytesFromFile, symmetric_key.getBytes());
////
////		        String dateperiodString=decrypted;
////		        
////		        String startDateString=dateperiodString.substring(0,dateperiodString.indexOf(" to "));
////		        String endDateString=dateperiodString.substring(dateperiodString.indexOf(" to ")+4);
////		        
////		        
////		        SimpleDateFormat dateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
////		        
////				Date startDate= dateFormat.parse(startDateString);
////				Date endDate=dateFormat.parse(endDateString);
////	            
////				SimpleDateFormat  dateFormat2=new SimpleDateFormat("dd/MM/yyyy");
////				subscriptionDetail= dateFormat2.format(startDate) + " to "+dateFormat2.format(endDate);
//
//			}
//				
//				
//				
//				
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		
	}

	String subscriptionDetail;
	
	public String getSubscriptionDetail() {
		return subscriptionDetail;
	}

	public void setSubscriptionDetail(String subscriptionDetail) {
		this.subscriptionDetail = subscriptionDetail;
	}

	public void setMydate(Date mydate) {
		this.mydate = mydate;
	}

	public Date getCruntdate() {
		return cruntdate;
	}

	public void setCruntdate(Date cruntdate) {
		this.cruntdate = cruntdate;
	}
	
	
	enum TrialPeriodStatus
	{
		EXPIRED,
		OneDayRemaining,
		TwoDaysRemaining,
		ThreeDaysRemaining,
		NOT_EXPIRED,
		TRIAL_FILE_MISSING,
		TRIAL_FILE_NOT_PRESENT
	}
	
	String symmetric_key="it is the encryption key";
	
//	public TrialPeriodStatus trialPeriodCheckOnLogin()
//	{
//		String realPath=FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
//		TrialPeriodStatus periodStatus = null;
//		
//		File file=new File(realPath+"abcd.a");
//
//		String decrypted=null;
//
//		if(file.exists())
//		{
//			try 
//			{
//				FileInputStream fileInputStream=new FileInputStream(file);
//				int buffer=0;
//				byte[] bytesFromFile;
//				bytesFromFile=new byte[fileInputStream.available()];
//				for(int i=0;(buffer=fileInputStream.read())!=-1;i++)
//				{
//				     bytesFromFile[i]=(byte)buffer;
//				}
//				
//				fileInputStream.close();
//				decrypted=new Encryption().decryptWithGivenKey(bytesFromFile, symmetric_key.getBytes());
//			} 
//			catch (FileNotFoundException e) {
//
//				e.printStackTrace();
//			} 
//			catch (IOException e) {
//				
//				e.printStackTrace();
//			}
//
//			
//			long endTrialdays=new DatabaseMethods().getRemainingTrialPeriod();
//
//            String dateperiodString=decrypted;
//            
//            String startDateString=dateperiodString.substring(0,dateperiodString.indexOf(" to "));
//            String endDateString=dateperiodString.substring(dateperiodString.indexOf(" to ")+4);
//            
//            
//            SimpleDateFormat dateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
//            
//            
//			try {
//				Date startDate= dateFormat.parse(startDateString);
//				Date endDate=dateFormat.parse(endDateString);
//				
//				
//	            long timeDiff=startDate.getTime()-endDate.getTime();
//	            long days=TimeUnit.MILLISECONDS.toDays(timeDiff);
//	            	
//                if(endTrialdays==days)
//                {
//                    	if(endTrialdays==0)
//            			{
//            				periodStatus=TrialPeriodStatus.EXPIRED;
//            			}
//            			else if (endTrialdays>0) 
//            			{
//            				periodStatus=TrialPeriodStatus.EXPIRED;
//            			}
//            			else
//            			{
//            				if(endTrialdays==-3)
//            				{
//            					periodStatus=TrialPeriodStatus.ThreeDaysRemaining;
//            				}
//            				else if(endTrialdays==-2)
//            				{
//            					periodStatus=TrialPeriodStatus.TwoDaysRemaining;
//            				}
//            				else if (endTrialdays==-1) 
//            				{
//            				    periodStatus=TrialPeriodStatus.OneDayRemaining;	
//            				}
//            				else
//            				{
//            					periodStatus=TrialPeriodStatus.NOT_EXPIRED;
//            				}
//            			}
//                }
//    	    	else
//    	    	{
//    	    		if(endTrialdays==Long.MAX_VALUE)
//    	    		{
//            				periodStatus=TrialPeriodStatus.EXPIRED;	    //   file is  present and value is not in DB .... not possible
//    	    		}
//    	    		else
//    	    		{
//    	    			if(endTrialdays<days)
//    	    			{
//    	    				periodStatus=TrialPeriodStatus.EXPIRED;
//    	    			}
//    	    			else if(endTrialdays>days)
//    	    			{
//    	    				periodStatus=TrialPeriodStatus.EXPIRED;
//    	    			}
//    	    		}
//    	    		//  file content and db content are not equal
//    	    		
//    	        }
//
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//            
////            
//            
//		}    
//		else
//		{
//           
//            	
//            	long endTrialHourss=new DatabaseMethods().getRemainingTrialPeriod();
//            	
//            	
//    			if(endTrialHourss==Long.MAX_VALUE)
//    			{
//    				 periodStatus=TrialPeriodStatus.TRIAL_FILE_NOT_PRESENT;   // file not found and DB entry not found
//    			}
//    			else
//    			{
//    			     periodStatus=TrialPeriodStatus.TRIAL_FILE_MISSING;
//    			}
//    			
//    			
//            }
//            
//		return periodStatus;
//	}
//	
//	
	
	public void updateDate()
	{
		
		int status=obj.updateTrialDate(7);    // update by seven days
		
		if(status==1)
		{
			
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Trial period updated successfully"));
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Error occur"));
		}
		
	}
	
	public void coustomUpdateDate() 
	{
		int i=0;
		
		Connection con=DataBaseConnection.JAVAConnection();
		mydate.setHours(0);
		mydate.setMinutes(0);
		mydate.setSeconds(0);
		
		Date currDate=new Date();
		currDate.setHours(0);
		currDate.setMinutes(0);
		currDate.setSeconds(0);
		
		
		if(mydate.compareTo(currDate)>0)
		{
//				String string=new Encryption().decryptWithGivenKey(new DatabaseMethods().getTrialPeriodEntry().getBytes(), "".getBytes());
				
//				SimpleDateFormat dateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");

//				String startDateString=string.substring(0, string.indexOf(" to "));
//				String endDateString=string.substring(string.indexOf(" to ")+4);
//				Date lastDate=dateFormat.parse(endDateString);
				
				// no endate will be the start date and mydate will be end date
				Date newLastDate=mydate;
				
				String string2=currDate.toString()+" to "+newLastDate.toString();
				String encryptedString2=new Encryption().encryptWithGivenKey(string2.getBytes(), "symmetric key (use less)".getBytes());
				
				
				*//******************* DB entry***********************//*
				
				boolean b=obj.makeTrialPeriodInDB(encryptedString2);
				if(b)
				{
					ServletContext servletContext=(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
					String real_path=servletContext.getRealPath("/");
					
					new LoginBean().writeDataToFile("abcd.a", encryptedString2.getBytes());
//					File file=new File(real_path+"trialPeriod.a");
//					file.delete();
//			 		FileOutputStream fileOutputStream=new FileOutputStream(file);
//					fileOutputStream.write(encryptedString2.getBytes());
//					file.setWritable(false);
//					fileOutputStream.close();
					
				    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Trial Period Updated !", ""));
				}
				else
				{
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Trial Period not Updated !", ""));
				}
			
				mydate=null;
			
		}
		
		
	}
	public String extendTrialDate() 
	{
//	   // System.out.println("extendTrialDate");
	   // System.out.println("enter custom");
	   Date startDate=mydate;
	   Date endDate=mydate2;
	   
	   SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
	   String startDateString=dateFormat.format(startDate);
	   String endDateString=dateFormat.format(endDate);
	   
	   if(obj.writeTrialPeriod(new Encryption().encryptWithGivenKey(startDateString.getBytes(), null) ,new Encryption().encryptWithGivenKey(endDateString.getBytes(), null)))
	   {
//		   // System.out.println("db inserted");
		   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Tiral date exteneded", ""));
		   // System.out.println("update custom");
		   return "loginPage";
	   }
	   else
	   {
		   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "this message means trial period not extended", ""));
		   return null; 
	   }
//	   // System.out.println("db not inserted");
	   
	}
	
	public String extendTrialDateForSevenDays() 
	{
		// System.out.println("enter fixed");
//	   Date startDate=new Date();
//	   Calendar calendar=Calendar.getInstance();
//	   calendar.setTimeZone(TimeZone.getDefault());
//	   
//	   calendar.add(Calendar.DAY_OF_MONTH, 7);
//	   Date endDate=calendar.getTime();
//	   
//	   SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
//	   String startDateString=dateFormat.format(startDate);
//	   String endDateString=dateFormat.format(endDate);
//	   
//	   String trialPeriodString=startDateString+" : "+endDateString;
//	   
//	   // System.out.println("trial period :"+trialPeriodString);
	   
	   if(obj.makeTrialPeriodInDB())
	   {
		   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Tiral date exteneded for 7 days", ""));
		   // System.out.println("update 7");
		   return "loginPage";
	   }
	   else
	   {
		   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "this message means trial period not extended", ""));
		   return null; 
	   }
	  
	}
	
	
//	public void updateDate(){
//		
//		int status=DatabaseMethods1.updateTrialDate();
//		if(status==1){
//			FacesContext fc=FacesContext.getCurrentInstance();
//			fc.addMessage(null, new FacesMessage("Trial period updated successfully"));
//		}
//		else{
//			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"An error occurred, try again", "Validation error"));
//		}
//		
//	}
}
*/