package com.devcru.journowatch.api.daoimpl;

import com.devcru.journowatch.api.dao.UserDao;
import com.devcru.journowatch.api.objects.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.sql.DataSource;

/**
 * Created by Monitored on 12/25/2015. Implementation for UserDao
 */

public class UserDaoImpl implements UserDao {

	// Use LOCAL SCOPE later

	private String loginSql = "";
	private String rateVenueSql = "";
	private String rateJourno = "";

	protected JdbcTemplate template;

	@Autowired
	@Qualifier("dataSource")
	public void setDataSource(DataSource ds) {
		this.template = new JdbcTemplate(ds);
	}
	

	// To be used for all query() calls since they allow for possible null returns
	// whereas queryForWhatever() does not
	ResultSetExtractor<String> rse = new ResultSetExtractor<String>() {
		@Override
		public String extractData(ResultSet rs) throws SQLException,
				DataAccessException {
			return (rs.next() ? rs.getString(1) : null);
		}
	};
	
	ResultSetExtractor<Object> rseObject = new ResultSetExtractor<Object>() {
		@Override
		public Object extractData(ResultSet rs) throws SQLException,
				DataAccessException {
			return (rs.next() ? rs.getObject(1) : null);
		}
	};
	
	ResultSetExtractor<User> rseUser = new ResultSetExtractor<User>() {
		@Override
		public User extractData(ResultSet rs) throws SQLException,
				DataAccessException {
			return (rs.next() ? (User) rs.getObject(1) : null);
		}
	};

	public void login() {
		template.execute(loginSql);
	}

	public void rateVenue() {
		template.execute(rateVenueSql);
	}

	public void rateJourno() {
		template.execute(rateJourno);
	}
	
	/* CRUD Operations */

	@Override
	public boolean addUser(User user) {
		boolean isSuccess = false;
		String sql = "INSERT INTO users (username, email, firstname, lastname, role, password) VALUES (?, ?, ?, ?, ?, ?)";
		
		String username = user.getUsername();
		String email = user.getEmail();
		String firstName = user.getFirstName();
		String lastName = user.getLastName();
		String role = user.getRole();
		String password = user.getPassword();
		
		try {
			template.update(sql, username, email, firstName, lastName, role, password);
			isSuccess = true;
		} catch (DataAccessException e) {
			isSuccess = false;
			e.printStackTrace();
		}
		
		return isSuccess;
		
	}

	@Override
	public User getUserByUsername(User user) {
		
		//boolean isSuccess = false;
		String sql = "SELECT uuid, username, email, firstname, lastname FROM users WHERE username = ?";
		List<Map<String, Object>> rows = null;
		
		String username = user.getUsername();
		
		try {
			rows = template.queryForList(sql, username, rse);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Debug
		for(int i = 0; i < rows.size(); i++) {
			System.out.println("rows.get(i): " + rows.get(i));
		}
		
		for(Map<String, Object> row : rows) {
			user.setUuid((UUID)row.get("uuid"));
			user.setEmail((String)row.get("email"));
			user.setUsername((String)row.get("username"));
			user.setFirstName((String)row.get("firstName"));
			user.setLastName((String)row.get("lastName"));
			user.setRole((String)row.get("role"));
			user.setPassword((String) "REDACTED"); // Is this necessary?
		}
		
		return user;		
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteUser(User user) {
		// TODO Auto-generated method stub
		
	}

}

@@ -2853,16 +2853,83 @@
 e = ?%22;%0A
+%09%09%0A%09%09String sql2 = %22SELECT uuid FROM users WHERE username = ?%22;%0A%09%09%0A
 %09%09List%3CM
@@ -3007,24 +3007,26 @@
 e();%0A%09%09%0A
+//
 %09%09try %7B%0A
 %09%09%09rows 
@@ -3017,16 +3017,18 @@
 %09%09try %7B%0A
+//
 %09%09%09rows 
@@ -3072,16 +3072,18 @@
 , rse);%0A
+//
 %09%09%7D catc
@@ -3110,16 +3110,18 @@
 on e) %7B%0A
+//
 %09%09%09// TO
@@ -3150,16 +3150,18 @@
 h block%0A
+//
 %09%09%09e.pri
@@ -3176,23 +3176,87 @@
 race();%0A
+//
 %09%09%7D%0A%09%09%0A
+%09%09rows = template.queryForList(sql2, username, rseObject);%0A%09%09%0A
 %09%09// Deb

Add getUserByUsername() endpoint FIX 17