import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;

public class ShahkarApi {

    // این تابع معادل callShahkarApi در PHP است
    // پارامترهای ورودی: شماره موبایل و کد ملی به صورت رشته
    public static JSONObject callShahkarApi(String mobile, String nationalCode) {
        String url = "https://shabanic.ir/api/v1/shahkar"; // آدرس API
        String apiKey = "کلید Api دریافت شده از پنل شبانیک را اینجا قرار دهید"; // کلید API

        HttpURLConnection connection = null;

        try {
            // ایجاد اتصال به API
            URL obj = new URL(url);
            connection = (HttpURLConnection) obj.openConnection();

            // تنظیم متد درخواست POST
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("SHABANIC-API-KEY", apiKey);
            connection.setDoOutput(true);

            // ساخت JSON برای ارسال در بدنه درخواست
            JSONObject payload = new JSONObject();
            payload.put("mobile", mobile);
            payload.put("national_code", nationalCode);

            // ارسال داده‌ها به API
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = payload.toString().getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // دریافت پاسخ از API
            int status = connection.getResponseCode();
            BufferedReader in;
            if (status == HttpURLConnection.HTTP_OK) {
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            } else {
                in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            }

            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // تبدیل پاسخ به JSON و بازگشت آن
            JSONObject jsonResponse = new JSONObject(response.toString());
            return jsonResponse;

        } catch (Exception e) {
            // در صورت بروز خطا، جزئیات خطا را باز می‌گرداند
            JSONObject errorResponse = new JSONObject();
            errorResponse.put("error", true);
            errorResponse.put("status", 500);
            errorResponse.put("message", e.getMessage());
            return errorResponse;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    // مثال استفاده از تابع بالا
    public static void main(String[] args) {
        // شماره موبایل و کد ملی مورد نظر را وارد کنید
        JSONObject result = callShahkarApi("09156012678", "0829940049");

        // نمایش نتیجه در کنسول
        System.out.println(result.toString());
    }
}
