package Controllers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;

import toolbox.JSONResponse;
import models.Reminder;
import models.User;
import flexjson.JSONSerializer;
import MVC.Controller;

/**
 * @author Alexander
 *
 */
public class ReminderCtl extends Controller {

    @Override
    public void doRequest(String actionName, Integer id) throws Exception {
        
        EntityManager em = this.getServletContext().getEM();
        
        if(actionName.equals("get")) {
            if(!loggedIn())
            {
                return;
            }
            
            JSONSerializer serializer = new JSONSerializer();
            JSONResponse response;
            try
            {
                User u = (User) this.getRequest().getSession().getAttribute("user");
                response = new JSONResponse(u.getReminders());
            }
            catch(Exception ex)
            {
                response = new JSONResponse(ex.getMessage());
            }
            this.getRequest().setAttribute("json", serializer.serialize(response));
            forward("/json.jsp");
        }
        
        if(actionName.equals("edit")) {
        	if(!loggedIn())
            {
                return;
            }
        	JSONSerializer serializer = new JSONSerializer();
            JSONResponse response;
            try
            {
            Reminder r = em.find(Reminder.class, id);
            r.setText(getRequest().getParameter("text"));
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            r.setDatestart(formatter.parse(this.getRequest().getParameter("startingat")));
            r.setFrequency(Byte.parseByte(getRequest().getParameter("frequency")));
            
            em.getTransaction().begin();
            em.persist(r);
            em.getTransaction().commit();
            response = new JSONResponse(true, null, null);
            
            } catch(Exception e) {
            	response = new JSONResponse(e.getMessage());
            }
            this.getRequest().setAttribute("json", serializer.serialize(response));
            this.forward("/json.jsp");
            
            
            
        }

        if(actionName.equals("delete")) {
            if(!loggedIn())
            {
                return;
            }
            
            JSONSerializer serializer = new JSONSerializer();
            JSONResponse response;
            try
            {
                User u = (User) this.getRequest().getSession().getAttribute("user");
                Reminder r = em.find(Reminder.class, id);
                u.removeReminder(r);
                em.getTransaction().begin();
                em.remove(r);
                em.getTransaction().commit();
                response = new JSONResponse(true, null, null);
            }
            catch(Exception ex)
            {
                response = new JSONResponse(ex.getMessage());
            }
            this.getRequest().setAttribute("json", serializer.serialize(response));
            this.forward("/json.jsp");

        }

        if(actionName.equals("add")) {
            if(!loggedIn())
            {
                return;
            }
            
            JSONResponse response;
            JSONSerializer serializer = new JSONSerializer();
            try
            {
                if(this.getRequest().getParameter("text") == null)
                {
                    throw new Exception("text parameter missing");
                }
                if(this.getRequest().getParameter("frequency") == null)
                {
                    throw new Exception("frequency parameter missing");
                }
                if(!(this.getRequest().getParameter("text").length() > 1))
                {
                    throw new Exception("Text cannot be empty");
                }
                Reminder newReminder = new Reminder();
                newReminder.setText(getRequest().getParameter("text"));
                newReminder.setFrequency(Byte.parseByte(getRequest().getParameter("frequency")));
                newReminder.setCreationDate(new Date());
                
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                newReminder.setDatestart(formatter.parse(this.getRequest().getParameter("startingat")));
                
                User u = (User) this.getRequest().getSession().getAttribute("user");
                u.addReminder(newReminder);
                
                em.getTransaction().begin();
                em.persist(newReminder);
                em.getTransaction().commit();
                response = new JSONResponse(newReminder);
            }
            catch(Exception ex)
            {
                response = new JSONResponse(ex.getMessage().toString());
            }

            this.getRequest().setAttribute("json", serializer.serialize(response));
            forward("/json.jsp");
        }
        

    }
    
    public boolean loggedIn() throws ServletException, IOException
    {
        if(this.getRequest().getSession().getAttribute("user") != null)
        {
            return true;
        }
        
        JSONSerializer serializer = new JSONSerializer();
        this.getRequest().setAttribute("json", serializer.serialize(new JSONResponse("User must be logged in for this operation")));

        this.forward("/json.jsp");
        
        return false;
    }
    
    

}

@@ -1331,21 +1331,21 @@
  %7B%0D%0A    
+%09
     
-%09
 if(!logg
@@ -1408,24 +1408,30 @@
      %7D%0D%0A
+%0D%0A    
         
 %09JSONSer
@@ -1422,17 +1422,16 @@
         
-%09
 JSONSeri
@@ -1545,32 +1545,36 @@
  %7B%0D%0A            
+    
 Reminder r = em.
@@ -1592,32 +1592,36 @@
 er.class, id);%0D%0A
+    
             r.se
@@ -1667,32 +1667,36 @@
 );%0D%0A            
+ 
+   
 SimpleDateFormat
@@ -1737,32 +1737,36 @@
 %22yyyy-MM-dd%22);%0D%0A
+    
             r.se
@@ -1849,24 +1849,28 @@
             
+  
+  
 r.setFrequen
@@ -1934,34 +1934,41 @@
 );%0D%0A            
-%0D%0A
+    %0D%0A   
             em.g
@@ -1955,32 +1955,33 @@
 %0A               
+ 
 em.getTransactio
@@ -1998,32 +1998,36 @@
 );%0D%0A            
+    
 em.persist(r);%0D%0A
@@ -2022,24 +2022,28 @@
 ersist(r);%0D%0A
+    
             
@@ -2077,32 +2077,36 @@
 );%0D%0A            
+    
 response = new J
@@ -2117,32 +2117,17 @@
 esponse(
-true, null, null
+r
 );%0D%0A    
@@ -2126,32 +2126,33 @@
 );%0D%0A            
+%7D
 %0D%0A            %7D 
@@ -2148,18 +2148,16 @@
         
- %7D
  catch(E
@@ -2167,16 +2167,29 @@
 ption e)
+%0D%0A           
  %7B%0D%0A    
@@ -2250,32 +2250,34 @@
 %0A            %7D%0D%0A
+%0D%0A
             this
@@ -2337,32 +2337,32 @@
 ze(response));%0D%0A
+
             this
@@ -2389,50 +2389,8 @@
 );%0D%0A
-            %0D%0A            %0D%0A            %0D%0A
     

Return edited object