package com.realnet.helper;

import java.nio.charset.StandardCharsets;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.realnet.utils.Port_Constant;

public class CnsHelper {

	static Logger log = org.slf4j.LoggerFactory.getLogger(CnsHelper.class);

	// CONNECTOR CALL
	public static String callconnector(String name) throws JsonProcessingException {

		String token = null;
		ResponseEntity<Object> u = null;
		RestTemplate restTemplate = new RestTemplate();
		String url = Port_Constant.BACKEND_PORTAL_DOMAIN + "/token/Sure_Connectbyname/" + name;

//			
		try {

			u = restTemplate.getForEntity(url, Object.class);

		} catch (Exception e) {

			System.out.println(" token get error " + e);

			// TODO: handle exception
		}
		Object object = u.getBody();

		if (object != null) {

			ObjectMapper mapper = new ObjectMapper();
			String str = mapper.writeValueAsString(object);

			JsonParser parser = new JsonParser();
			JsonElement element = parser.parse(str);

			JsonObject obj = element.getAsJsonObject();
			JsonElement tok = obj.get("access_token");
			System.out.println("token is == " + token);
			token = tok.getAsString();
		} else {
			System.out.println(" empty totek get ...");
		}
		return token;
	}

	public static ResponseEntity<?> run(String urll, String param, String method, String token, String username,
			String password) {

		log.info("executing no parameters");

		if (method.equalsIgnoreCase("DELETE")) {
			Object body = DELETE(urll);

			return new ResponseEntity<>(body, HttpStatus.OK);
		} else {
			Object object = callmethod(urll, param, method, token, username, password);

			System.out.println(object);
			return new ResponseEntity<>(object, HttpStatus.OK);

		}
	}

	public static boolean DELETE(String url) {
		RestTemplate restTemplate = new RestTemplate();

		restTemplate.delete(url, Object.class);

		return true;

	}

	// CALL METHOD
	public static Object callmethod(String url, String param, String method, String token, String username,
			String password) {

		if (method.equalsIgnoreCase("GET")) {
			ResponseEntity<Object> get = GET(url);
			Object body = get.getBody();
			System.out.println(body);
			return get.getBody();
		}

		else if (method.equalsIgnoreCase("POST")) {

			ResponseEntity<Object> post;
			if (username != null && password != null) {
				post = POSTForBasicAuth(url, param, username, password);

			} else {

				post = POST(url, param, token);
			}
			Object body = post.getBody();
			System.out.println(body);

			return post.getBody();

		} else if (method.equalsIgnoreCase("PUT")) {
			ResponseEntity<Object> put = PUT(url, param, token);
			Object body = put.getBody();
			System.out.println(body);

			return put.getBody();

		} else {
			return null;
		}

	}

	public static ResponseEntity<Object> GET(String get) {
		RestTemplate restTemplate = new RestTemplate();

		ResponseEntity<Object> u = restTemplate.getForEntity(get, Object.class);
		int statusCodeValue = u.getStatusCodeValue();
		System.out.println(statusCodeValue);

		return u;

	}

	public static ResponseEntity<Object> GET(String get, String token) {
		RestTemplate restTemplate = new RestTemplate();
		String resourceUrl = get;
		String token1 = "Bearer " + token;
		HttpHeaders headers = getHeaders();
		headers.set("Authorization", token1);
		HttpEntity<Object> request = new HttpEntity<Object>(headers);
		ResponseEntity<Object> u = restTemplate.exchange(resourceUrl, HttpMethod.GET, request, Object.class);

		int statusCodeValue = u.getStatusCodeValue();
		System.out.println(statusCodeValue);

		return u;

	}

	public static ResponseEntity<Object> POST(String jobinfo, Object user, String token) {
		RestTemplate restTemplate = new RestTemplate();
		String resourceUrl = jobinfo;
		String token1 = "Bearer " + token;
		HttpHeaders headers = getHeaders();
		headers.set("Authorization", token1);
		HttpEntity<Object> request = new HttpEntity<Object>(user, headers);
		ResponseEntity<Object> res = restTemplate.postForEntity(resourceUrl, request, Object.class);

		return res;

	}

	public static ResponseEntity<Object> POSTForBasicAuth(String jobinfo, Object user, String username,
			String password) {

		System.out.println(" call basic auth..");
		RestTemplate restTemplate = new RestTemplate();
		String resourceUrl = jobinfo;

		String auth = username + ":" + password;
		byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII));
		String authHeader = "Basic " + new String(encodedAuth);

		HttpHeaders headers = getHeaders();

		headers.set("Authorization", authHeader);

		HttpEntity<Object> request = new HttpEntity<>(user.toString(), headers);
		ResponseEntity<Object> res = null;
		try {

			res = restTemplate.postForEntity(resourceUrl, request, Object.class);
		} catch (Exception e) {

			System.out.println(" call basic auth error " + e);
			// TODO: handle exception
		}

		return res;

	}

	public static ResponseEntity<Object> PUT(String jobinfo, Object user, String token) {
		RestTemplate restTemplate = new RestTemplate();
		String resourceUrl = jobinfo;
		String token1 = "Bearer " + token;
		HttpHeaders headers = getHeaders();
		headers.set("Authorization", token1);
		HttpEntity<Object> request = new HttpEntity<Object>(user, headers);
//		ResponseEntity<Object> res = restTemplate.put(resourceUrl, request, Object.class);
		ResponseEntity<Object> res = restTemplate.exchange(resourceUrl, HttpMethod.PUT, request, Object.class);

		return res;

	}

	private static HttpHeaders getHeaders() {
		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
		headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
		return headers;
	}

}
