package com.reallinkbilling.infrainfotechsms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class SendMessage {
	
	private static final String API_URL = "http://123.108.46.13/sms-panel/api/http/v1/index.php?";
	private static final String REQUEST_TYPE_UNICODE = "Unicode";
	private static final String REQUEST_TYPE_WHATSAPP = "WhatsApp";
	
	public static String sendSms(String userName, String password, String sToPhoneNo, String sMessage, String senderId,
			String templateId, String requestType) {
		return sendMessage(userName, password, sToPhoneNo, sMessage, senderId, templateId, requestType);
	}

	public static String sendUnicodeMessage(String userName, String password, String sToPhoneNo, String sMessage,
			String senderId, String templateId) {
		return sendMessage(userName, password, sToPhoneNo, sMessage, senderId, templateId, REQUEST_TYPE_UNICODE);
	}

	public static String sendWhatsAppMessage(String userName, String password, String sToPhoneNo, String sMessage,
			String senderId, String templateId) {
		return sendMessage(userName, password, sToPhoneNo, sMessage, senderId, templateId, REQUEST_TYPE_WHATSAPP);
	}

	public static String sendWhatsAppMessageWithFallback(String userName, String password, String sToPhoneNo,
			String sMessage, String senderId, String templateId) {
		String response = sendWhatsAppMessage(userName, password, sToPhoneNo, sMessage, senderId, templateId);

		if (isFailedResponse(response)) {
			response = sendUnicodeMessage(userName, password, sToPhoneNo, sMessage, senderId, templateId);
		}

		return response;
	}

	public static boolean isFailedResponse(String response) {
		if (response == null) {
			return true;
		}
		String output = response.trim().toLowerCase();
		return output.isEmpty() || output.contains("failed") || output.contains("error");
	}

	private static String sendMessage(String userName, String password, String sToPhoneNo, String sMessage,
			String senderId, String templateId, String requestType) {
		String sResult = "";

		try {
			String apirequest = (requestType == null || requestType.trim().isEmpty()) ? REQUEST_TYPE_UNICODE
					: requestType;
			String route = "OTP";
			String requestUrl = "" + "username=" + URLEncoder.encode(userName, "UTF-8") + "&apikey="
					+ URLEncoder.encode(password, "UTF-8") + "&apirequest=" + URLEncoder.encode(apirequest, "UTF-8")
					+ "&route=" + URLEncoder.encode(route, "UTF-8") + "&sender=" + URLEncoder.encode(senderId, "UTF-8")
					+ "&mobile=" + URLEncoder.encode(sToPhoneNo, "UTF-8") + "&TemplateID="
					+ URLEncoder.encode(templateId, "UTF-8") + "&message=" + URLEncoder.encode(sMessage, "UTF-8")
					+ "&format=JSON";

			URL url = new URL(API_URL + requestUrl);
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(requestUrl);
			wr.flush();

			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;

			while ((line = rd.readLine()) != null) {
				sResult = sResult + line + " ";
			}
			wr.close();
			rd.close();

		} catch (Exception ex) {
			sResult = "failed: " + ex.getMessage();
		}

		return sResult;
	}

}
