package com.devcru.journowatch.api.controllers;

import com.devcru.journowatch.api.daoimpl.UserDaoImpl;
import com.devcru.journowatch.api.objects.User;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import static org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString;

import java.io.IOException;

/**
 * Created by Monitored on 12/25/2015.
 * User Controller Class
 */

@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
    private UserDaoImpl userDao;
	
	@Autowired
	private Configuration freemarkerConfiguration;
	
	/* 
	 * Sample landing page thing
	 * Note that this is not REST by any means
	 * This is a full-fledged MVC application
	 */
	@RequestMapping(value="/test", method=RequestMethod.GET)
	public @ResponseBody
	String getIndexView() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		System.out.println("MainController > test hit! Returning data...");
		
		User user = new User();
		user.setUsername("regular");
		user.setPassword("password");
		
		return processTemplateIntoString(freemarkerConfiguration.getTemplate("sample.ftl"), new Object());
	}
	
	@RequestMapping(value="/user", method=RequestMethod.POST)
	@ResponseBody
	public String createUser(@RequestBody User user) {
		
		boolean status = userDao.addUser(user);
		
		return status + "";
	}
	
	@RequestMapping(value="/user/{username}", method=RequestMethod.GET)
	@ResponseBody
	public User createUser(@PathVariable("username") String username) {
		
		User user = new User();
		user.setUsername(username);
		
		user = userDao.getUserByUsername(user);
		
		//user.setUuid(userDao.getUuid(username));
		
		return user;
	}
	
	@RequestMapping(value = "/user/{username}", method=RequestMethod.PUT)
	@ResponseBody
	public String updateUser(@PathVariable("username") String username, @RequestBody User user) {
		
		// Should we first associate the username with the JSON struct?
		// i.e. is there a data integrity risk here?  Analyze further...
		//user.setUsername(username);
		
		boolean status = userDao.updateUser(user);
		
		return status + "";
		// ???: return JsonResponse;
	}
	
	@RequestMapping(value = "/user/{username}", method=RequestMethod.DELETE)
	@ResponseBody
	public String deleteUser(@PathVariable("username") String username) {
		User user = new User();
		user.setUsername(username);
		
		boolean status = userDao.deleteUser(user);
		
		return status + "";
		// ??? return JsonResponse;
	}
    
    @ResponseStatus(value=HttpStatus.OK)
    // TODO: Test this to see if this gets sent along with a successful 200 OK
    public String okay() {
    	System.out.println(">>>>>>>>>>>>>>>: OKAY!");
    	return "HPOASGBJHSJHDFGGJHSD";
    }
    
    @ResponseStatus(value=HttpStatus.NOT_FOUND)
    public String notFound() {
    	System.out.println(">>>>>>>>>>>>>>>: NOT FOUND!");
    	return "Yotsuba!!";    	
    }
    
    @RequestMapping(value="/login", method=RequestMethod.POST)
    public static Object logTheFuckIn(User user) {

        // This is where we would call in a UserDAOImpl and execute the login method
        return null;
    }

}

@@ -1162,11 +1162,13 @@
 user
+/*
 %22)%0A
-
 publ
@@ -1454,17 +1454,16 @@
 (value=%22
-/
 test%22, m
@@ -1943,20 +1943,16 @@
 value=%22/
-user
 %22, metho
@@ -2139,22 +2139,16 @@
 (value=%22
-/user/
 %7Busernam
@@ -2455,38 +2455,32 @@
 apping(value = %22
-/user/
 %7Busername%7D%22, met
@@ -2915,22 +2915,16 @@
 alue = %22
-/user/
 %7Busernam
@@ -3627,24 +3627,24 @@
 %0A    %7D%0A    %0A
+
     @Request
@@ -3658,17 +3658,16 @@
 (value=%22
-/
 login%22, 

Add JournoController and JournoDao 4