package com.foxx.androidcast.crash;

/*********************************************************************
 * CrashUploadClient.java
 * Created at: Wed 20 May 2026 14:31:55 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class CrashUploadClient
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 64 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	b92a804c279390649ca8c27d041dbc2bac31f86968a009e12e7a7c679d718cc7
**********************************************************************/
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPOutputStream;

/** POST crash JSON to the PHP receiver. */
public final class CrashUploadClient {
    private static final int CONNECT_MS = 12_000;
    private static final int READ_MS = 20_000;

    private CrashUploadClient() {}

    private static byte[] gzip(byte[] plain) throws java.io.IOException {
        ByteArrayOutputStream buf = new ByteArrayOutputStream(Math.max(256, plain.length / 4));
        try (GZIPOutputStream gz = new GZIPOutputStream(buf)) {
            gz.write(plain);
        }
        return buf.toByteArray();
    }

    public static boolean upload(String uploadUrl, JSONObject report) {
        if (uploadUrl == null || uploadUrl.isEmpty() || report == null) {
            return false;
        }
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(uploadUrl).openConnection();
            conn.setConnectTimeout(CONNECT_MS);
            conn.setReadTimeout(READ_MS);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Content-Encoding", "gzip");
            byte[] json = report.toString().getBytes(StandardCharsets.UTF_8);
            byte[] body = gzip(json);
            try (OutputStream out = conn.getOutputStream()) {
                out.write(body);
            }
            int code = conn.getResponseCode();
            if (code < 200 || code >= 300) {
                return false;
            }
            try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
                    ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
                byte[] tmp = new byte[1024];
                int n;
                while ((n = in.read(tmp)) != -1) {
                    buf.write(tmp, 0, n);
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
}
