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 = ?";
		
		String sql2 = "SELECT uuid FROM users WHERE username = ?";
		
		String sql3 = "SELECT email 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();
//		}
		
		//rows = template.queryForList(sql2, username, rseObject);
		
		rows = template.queryForList(sql3, username, rse);
		
		// 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
		
	}

}

@@ -3316,16 +3316,18 @@
 );%0A%09%09%0A%09%09
+//
 rows = t
@@ -3383,16 +3383,18 @@
 / Debug%0A
+//
 %09%09for(in
@@ -3426,16 +3426,18 @@
  i++) %7B%0A
+//
 %09%09%09Syste
@@ -3482,23 +3482,29 @@
 et(i));%0A
+//
 %09%09%7D%0A
+//
 %09%09%0A
+//
 %09%09for(Ma
@@ -3535,16 +3535,18 @@
 rows) %7B%0A
+//
 %09%09%09user.
@@ -3573,24 +3573,26 @@
 t(%22uuid%22));%0A
+//
 %09%09%09user.setE
@@ -3619,24 +3619,26 @@
 (%22email%22));%0A
+//
 %09%09%09user.setU
@@ -3667,32 +3667,34 @@
 t(%22username%22));%0A
+//
 %09%09%09user.setFirst
@@ -3721,32 +3721,34 @@
 (%22firstName%22));%0A
+//
 %09%09%09user.setLastN
@@ -3777,24 +3777,26 @@
 astName%22));%0A
+//
 %09%09%09user.setR
@@ -3825,16 +3825,18 @@
 ole%22));%0A
+//
 %09%09%09user.
@@ -3891,23 +3891,142 @@
 essary?%0A
+//
 %09%09%7D%0A%09%09%0A
+%09%09String email = null;%0A%09%09%0A%09%09email = template.query(sql3, new Object%5B%5D%7Busername%7D, rse);%0A%09%09%0A%09%09user.setEmail(email);%0A%09%09%0A
 %09%09return

Add getUserByUsername() endpoint FIX 19