import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class CheckOrderStatusApiClient {

    private static final String API_URL = "https://khilaadixpro.shop/api/check-order-status";

    public interface OnCheckOrderStatusListener {
        void onSuccess(JSONObject response);

        void onError(String errorMessage);
    }

    public void checkOrderStatus(String userToken, String orderId, OnCheckOrderStatusListener listener) {
        new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URL url = new URL(API_URL);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                    // Create the request payload
                    String payload = "user_token=" + userToken + "&order_id=" + orderId;

                    connection.setDoOutput(true);
                    OutputStream os = connection.getOutputStream();
                    os.write(payload.getBytes());
                    os.flush();
                    os.close();

                    int responseCode = connection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        StringBuilder response = new StringBuilder();
                        String inputLine;
                        while ((inputLine = in.readLine()) != null) {
                            response.append(inputLine);
                        }
                        in.close();
                        return response.toString();
                    } else {
                        return null;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            protected void onPostExecute(String response) {
                if (response != null) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        if (jsonResponse.has("status") && jsonResponse.getString("status").equals("COMPLETED")) {
                            listener.onSuccess(jsonResponse.getJSONObject("result"));
                        } else {
                            listener.onError(jsonResponse.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        listener.onError("Invalid response from the server");
                    }
                } else {
                    listener.onError("Unable to connect to the server");
                }
            }
        }.execute();
    }
}
