package com.app.duck.util;

import static com.app.http.APPConfig.APP_TG_SEND_DOCUMENT;
import static com.app.http.APPConfig.APP_TG_SEND_MESSAGE;

import android.util.Log;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.blankj.utilcode.util.ZipUtils;
import com.app.duck.BuildConfig;
import com.app.duck.Config;
import com.app.duck.R;
import com.elvishew.xlog.XLog;

import java.io.File;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * author: hx
 * created on: 2023/10/17 12:04
 * description:
 */
public class TgBot {

    public TgBot(String data, boolean message) {
        if (!message) {
            sendFileToGroup();
        } else {
            sendMessageToGroup(data);
        }
    }

    public void sendFileToGroup() {
        ThreadUtils.executeByIo(new ThreadUtils.Task<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                ToastUtils.showLong(R.string.pushing);
                XLog.i("log start push...");
                String filePath = Utils.getApp().getFilesDir().getPath() + "/ulog";
                String filePathZip = Utils.getApp().getFilesDir().getPath() + "/zip/" + HxUtils.getPhone() + "-log";
                FileUtils.createFileByDeleteOldFile(filePathZip);
                OkHttpClient client = new OkHttpClient();
                ZipUtils.zipFile(filePath, filePathZip, "log zip push");
                File file = new File(filePathZip);
                MultipartBody.Builder builder = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("chat_id", HxUtils.getChatId())
                        .addFormDataPart("document", file.getName(), RequestBody.create(MediaType.parse("text/plain"), file));
                HttpUrl.Builder urlBuilder = HttpUrl.parse(APP_TG_SEND_DOCUMENT).newBuilder();
                Request request = new Request.Builder()
                        .url(urlBuilder.build())
                        .post(builder.build())
                        .build();
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    ToastUtils.showLong(R.string.push_suc);
                    Log.i(Config.LOG_TAG, "File sent successfully.");
                    XLog.i("File sent successfully.");
                } else {
                    ToastUtils.showLong(R.string.push_error);
                    Log.i(Config.LOG_TAG, "Failed to send file. Response: " + response.body().string());
                    XLog.i("Failed to send file. Response: " + response.body().string());
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                ToastUtils.showLong(R.string.push_error);
                Log.e(Config.LOG_TAG, "Send file to group fail. ", t);
                XLog.e("Send file to group fail. ", t);
            }
        });

    }

    public void sendMessageToGroup(String message) {
        ThreadUtils.executeByIo(new ThreadUtils.Task<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = new FormBody.Builder()
                        .add("chat_id", HxUtils.getChatId())
                        .add("text", "Channel : " + BuildConfig.UPDATE_KEY + "\n" + "Phone number : " + HxUtils.getPhone() + "\n" + message)
                        .build();
                Request request = new Request.Builder()
                        .url(APP_TG_SEND_MESSAGE)
                        .post(requestBody)
                        .build();
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    Log.i(Config.LOG_TAG, "Message sent successfully.");
                    XLog.i("Message sent successfully.");
                } else {
                    Log.i(Config.LOG_TAG, "Failed to send message. Response: " + response.body().string());
                    XLog.i("Failed to send message. Response: " + response.body().string());
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                Log.e(Config.LOG_TAG, "Send message to group fail. ", t);
                XLog.e("Send message to group fail. ", t);
            }
        });

    }
}
