package com.mycompany.moduloseguridad.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import okhttp3.*;
import org.json.JSONObject;

public final class HttpRequest {

	public static final String POST = "POST";
	public static final String GET = "GET";
	public static final String PUT = "PUT";

	private URL getConn(String path) {
		URL url = null;
		try {
			url = new URL(path);
		} catch (MalformedURLException ex) {
			System.out.println("Error :  HttpRequest : " + ex.getMessage());
		}
		return url;
	}

	private HttpURLConnection setDefaultValues(URL url, String requestMethod, String authorization) throws IOException {
		HttpURLConnection conn = null;
		conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
		conn.setRequestProperty("Authorization", authorization);
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod(requestMethod);
		return conn;
	}

	public String getRespuesta(String path, String requestMethod, JSONObject obj, String authorization) throws IOException {
		StringBuilder respuesta = new StringBuilder();
		URL url = getConn(path);
		HttpURLConnection conn = setDefaultValues(url, requestMethod, authorization);
		OutputStream os = conn.getOutputStream();
		os.write(obj.toString().getBytes(StandardCharsets.UTF_8));
		os.close();
		BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
		String linea;
		while ((linea = rd.readLine()) != null) {
			respuesta.append(linea);
		}
		rd.close();
		conn.disconnect();
		return respuesta.toString();
	}

	public String securityRequest (String path,JSONObject obj, String authorization) {
		try {
			OkHttpClient client = new OkHttpClient();
			String respuesta = "";
			URL url = getConn(path);
			System.out.println(url);
			MediaType JSON = MediaType.parse("application/json; charset=utf-8");
			RequestBody body = RequestBody.create(JSON, obj.toString());
			Request request = new Request.Builder()
					.url(url)
					.header("Authorization", authorization)
					.post(body)
					.build();
			try (Response response = client.newCall(request).execute()) {
				if (response.isSuccessful()) {
					assert response.body() != null;
					respuesta = response.body().string();
				} else {
					System.out.println("Error: " + response.code() + " - " + response.message());
					assert response.body() != null;
					System.out.println("Response: " + response.body().string());
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Error: " + e.getMessage());
			}
			return respuesta;
		} catch (Exception ex) {
			System.out.println("Error :  HttpRequest : " + ex.getMessage());
			return null;
		}
	}
}