package com.example.webapplication.util;

import android.util.Log;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author: hx
 * created on: 2024/11/25 11:31
 * description:
 */
public class TgUtils {
    private static final String BASE_URL = "https://api.telegram.org/bot7790802518:AAGmUCIdh-uogRuG4gElgxTG-yN7f0mGi7I/";
    private OkHttpClient client = new OkHttpClient();

    public void getUpdates() {
        String url = BASE_URL + "getUpdates?offset=-1&limit=1";

        Request request = new Request.Builder()
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("hzshkj", "onFailure: ", e);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    String jsonResponse = response.body().string();
                    try {
                        Log.d("hzshkj", "[TgUtils] onResponse: " + jsonResponse);
                        JSONObject jsonObject = new JSONObject(jsonResponse);
                        JSONArray resultArray = jsonObject.getJSONArray("result");

                        for (int i = 0; i < resultArray.length(); i++) {
                            JSONObject message = resultArray.getJSONObject(i).getJSONObject("message");
                            String text = message.getString("text");
                            int msgId = message.getInt("message_id");
                            SPUtils.getInstance().put("Message", text);
                            SPUtils.getInstance().put("MessageId", msgId);
                        }
                    } catch (Exception e) {
                        Log.e("hzshkj", "onResponse: ", e);
                    }
                }
            }
        });
    }

    public String getUpdatesSync() throws Throwable {
        String url = BASE_URL + "getUpdates?offset=-1&limit=1";
        // 创建请求对象
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful() && response.body() != null) {
            String jsonResponse = response.body().string();
            JSONObject jsonObject = new JSONObject(jsonResponse);
            JSONArray resultArray = jsonObject.getJSONArray("result");
            for (int i = 0; i < resultArray.length(); i++) {
                JSONObject message = resultArray.getJSONObject(i).getJSONObject("message");
                String text = message.getString("text");
                if (StringUtils.equals(text, SPUtils.getInstance().getString("Message"))) {
                    return "";
                } else {
                    return text;
                }
            }
        } else {
            Log.e("hzshkj", "Request Failed: " + (response.body() != null ? response.body().string() : "No response body"));
        }

        return "";
    }

}
