package com.hpay.hpay_mobile_api.utils;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SMSapi {

    private boolean SEND = true; // False = Not send MSG, True = Send MSG

    public void alaSMS(String recever, String message) {
        if (SEND) {

            String from = "HPAY";
            char doo = recever.charAt(0);

            if (doo == '1') {
                from = "12366136787";
            }

            try {
                    URL url = new URL("http://api.emergence-consultant.com/sms/");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Content-Type", "application/json");
                    connection.setRequestProperty("accept", "application/json");
                    connection.setRequestProperty("authorization", "Basic SFBBWToxMjMjQEhwYVkw");
                    connection.setDoOutput(true);

                    // Create JSON body
                    String jsonData = String.format("{\"from\":\"%s\",\"to\":[\"%s\"],\"text\":\"%s\"}", from, recever, message);

                    // Send request
                    try (OutputStream os = connection.getOutputStream()) {
                        byte[] input = jsonData.getBytes("utf-8");
                                os.write(input, 0, input.length);
                    }

                    // Get the response
                    int responseCode = connection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        System.out.println("Message sent successfully");
                    } else {
                        System.out.println("Error sending message, response code: " + responseCode);
                    }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}