package com.icontrols.oauth.utils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import org.apache.tomcat.util.json.JSONParser;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.icontrols.oauth.model.ClientInfo;
import com.icontrols.oauth.model.Token;
import com.icontrols.oauth.repo.ClientInfoRepository;

public class DanziAuthUtils {
	private static final Logger logger = LoggerFactory.getLogger(DanziAuthUtils.class);

	@Autowired
	ClientInfoRepository clientInfoRepo;

	public static String create(Token newToken, String url)
			throws UnsupportedEncodingException, NoSuchAlgorithmException, GeneralSecurityException {

		Map<String, Object> body = new HashMap<>();

		Map<String, Object> token = new HashMap<>();
		token.put("accessToken", newToken.getAccess_token());
		token.put("refreshToken", newToken.getRefresh_token());

		body.put("id", System.currentTimeMillis() + "");
		body.put("type", "CREATE");
		body.put("body", token);

		logger.info(body.toString());

		String complex = JWTUtils.getCmpIpFromToken(newToken.getAccess_token());
		String endpoint = "http://" + complex + url + "/accessToken";

		HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
		factory.setReadTimeout(3000);
		factory.setConnectTimeout(3000);
		RestTemplate template = new RestTemplate(factory);
		HttpEntity<Object> entity = new HttpEntity<Object>(body);
		String answer = null;
		try {
			answer = template.postForObject(endpoint, entity, String.class);
		} catch (Exception e) {
			return null;
		}

//		logger.info(answer);
//		JSONObject obj = new JSONObject(answer);
//		if (!obj.get("result").toString().equalsIgnoreCase("success")) {
//			return false;
//		}

		return answer;
	}

	public static String refresh(String url, Token newToken, String prevToken) {

		Map<String, Object> body = new HashMap<>();
		Map<String, Object> token = new HashMap<>();
		token.put("prevRefreshToken", prevToken);
		token.put("newAccessToken", newToken.getAccess_token());
		token.put("newRefreshToken", newToken.getRefresh_token());

		body.put("id", System.currentTimeMillis() + "");
		body.put("type", "REFRESH");
		body.put("body", token);

		String complex = JWTUtils.getCmpIpFromToken(prevToken);
		String endpoint = "http://" + complex + url + "/accessToken";

		HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
		factory.setReadTimeout(3000);
		factory.setConnectTimeout(3000);
		RestTemplate template = new RestTemplate(factory);
		HttpEntity<Object> entity = new HttpEntity<Object>(body);
		String answer = null;
		try {
			answer = template.postForObject(endpoint, entity, String.class);
		} catch (Exception e) {
			return null;
		}

		return answer;
	}

	public static String get(String url, String refreshToken) {

		Map<String, Object> body = new HashMap<>();
		Map<String, Object> token = new HashMap<>();
		token.put("refreshToken", refreshToken);

		body.put("id", System.currentTimeMillis() + "");
		body.put("type", "GET");
		body.put("body", token);

		String complex = JWTUtils.getCmpIpFromToken(refreshToken);
		String endpoint = "http://" + complex + url + "/accessToken";

		HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
		factory.setReadTimeout(3000);
		factory.setConnectTimeout(3000);
		RestTemplate template = new RestTemplate(factory);
		HttpEntity<Object> entity = new HttpEntity<Object>(body);
		String answer = null;
		try {
			answer = template.postForObject(endpoint, entity, String.class);
		} catch (Exception e) {
			return null;
		}

		logger.info(answer);

		return answer;
	}

}
