package com.projectning.service.web;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.stream.JsonParsingException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.projectning.auth.JWTSigner;
import com.projectning.auth.JWTVerifier;
import com.projectning.auth.JWTVerifyException;
import com.projectning.service.manager.HelperManager;
import com.projectning.service.manager.UserManager;

@Controller
public class AuthController {
	
	@Autowired private HelperManager helperManager;
	@Autowired private UserManager userManager;
	
	@RequestMapping("/register_for_salt")
    public ResponseEntity<String> registerForSalt(@RequestBody String request) {
		String salt = "";
		JsonObjectBuilder respond = Json.createObjectBuilder();
		try{
			JsonObject jsonObj = helperManager.stringToJsonHelper(request);
			salt = userManager.registerForSalt(jsonObj.getString("username"));
			respond.add("salt", salt);
		}catch(JsonParsingException e){
			return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}catch(IllegalStateException e){
			return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	
		return new ResponseEntity<String>(salt, HttpStatus.OK);
		
	}
	
	@RequestMapping("/register")
    public ResponseEntity<String> register(@RequestBody String request) {
		String username = "";
		try{
			JsonObject jsonObj = helperManager.stringToJsonHelper(request);
			username = userManager.registerForSalt(jsonObj.getString("username"));
		}catch(JsonParsingException e){
			
		}
	
		return new ResponseEntity<String>(username, HttpStatus.OK);
		
	}
	
	@RequestMapping("/auth/*")
    public ResponseEntity<String> home(HttpServletRequest request) {
		
        System.out.println(request.getRequestURI());
        
        Map<String, Object> authToken = new HashMap<String, Object>();
        authToken.put("username", "admin");
        authToken.put("expire", 20101010);
        authToken.put("username2", "admin");
        authToken.put("expire2", 20101010);
        authToken.put("username3", "admin");
        authToken.put("expire3", 20101010);
        authToken.put("username4", "admin");
        authToken.put("expire4", 20101010);
        
        JWTSigner signer = new JWTSigner("ProjectNing");
        
        String a = signer.sign(authToken);
        
        String b = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmUzIjoyMDEwMTAxMCwiZXhwaXJlNCI6MjAxMDEwMTAsImV4cGlyZTIiOjIwMTAxMDEwLCJleHBpcmUiOjIwMTAxMDEwLCJ1c2VybmFtZTQiOiJhZG1pbiIsInVzZXJuYW1lMyI6ImFkbWluIiwidXNlcm5hbWUyIjoiYWRtaW4iLCJ1c2VybmFtZSI6ImFkbWluIn0.2FYRg1Qd035JLNwAOz7MRcZ8iQEuV3ZjpWEmzByiOQ8";

        JWTVerifier verifier = new JWTVerifier("ProjectNing");
        
        Map decode;
        
        try {
			decode = verifier.verify(b);
			Iterator<?> it = decode.entrySet().iterator();
			while (it.hasNext()) {
		        Map.Entry pair = (Map.Entry)it.next();
		        System.out.println(pair.getKey() + " = " + pair.getValue());
		        it.remove(); // avoids a ConcurrentModificationException
		    }
		} catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | SignatureException
				| IOException | JWTVerifyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        return new ResponseEntity<String>(a, HttpStatus.OK);
    }

}

@@ -1526,16 +1526,45 @@
  salt);%0A
+%09%09%09respond.add(%22error%22, %22%22);%0A
 %09%09%7Dcatch
@@ -1598,211 +1598,129 @@
 %09%09re
-turn new ResponseEntity%3CString%3E(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);%0A%09%09%7Dcatch(IllegalStateException e)%7B%0A%09%09%09return new ResponseEntity%3CString%3E(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR
+spond.add(%22error%22, %22Request format incorrest%22);%0A%09%09%7Dcatch(IllegalStateException e)%7B%0A%09%09%09respond.add(%22error%22, e.getMessage()
 );%0A%09
@@ -1760,20 +1760,42 @@
 String%3E(
-salt
+respond.build().toString()
 , HttpSt
@@ -1922,29 +1922,8 @@
 %7B%0A%09%09
-String username = %22%22;
 %0A%09%09t
@@ -1996,16 +1996,23 @@
 st);%0A%09%09%09
+String 
 username
@@ -2018,36 +2018,8 @@
 e = 
-userManager.registerForSalt(
 json
@@ -2039,24 +2039,125 @@
 (%22username%22)
+;%0A%09%09%09String password = jsonObj.getString(%22password%22);%0A%09%09%09System.out.println(username + %22 %22 + password
 );%0A%09%09%7Dcatch(
@@ -2185,16 +2185,54 @@
 e)%7B%0A%09%09%09%0A
+%09%09%7Dcatch(NullPointerException e)%7B%0A%09%09%09%0A
 %09%09%7D%0A%09%0A%09%09
@@ -2265,24 +2265,18 @@
 String%3E(
-username
+%22%22
 , HttpSt

Update for handling null pointer and json parse exceptions