package com.example.myapplication.Tool;

import android.content.Context;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class ShowError {
    private Context context;
    private final Handler mainHandler;
    public ShowError(Context context) {
        this.context = context;
        this.mainHandler = new Handler(Looper.getMainLooper());
    }

    public void showErrorDialog(String title, String message) {
        if (context instanceof AppCompatActivity) {
            AppCompatActivity activity = (AppCompatActivity) context;
            activity.runOnUiThread(() -> {
                new AlertDialog.Builder(activity)
                        .setTitle(title)
                        .setMessage(message)
                        .setPositiveButton("确定", null)
                        .setNegativeButton("复制错误", (dialog, which) -> {
                            // 将错误复制到剪贴板
                            ClipboardManager clipboard =
                                    (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clip =
                                    ClipData.newPlainText("错误信息", message);
                            clipboard.setPrimaryClip(clip);
                            showToast("已复制错误信息");
                        })
                        .show();
            });
        }
    }

    private void showToast(String message) {
        mainHandler.post(() -> Toast.makeText(context, message, Toast.LENGTH_SHORT).show());
    }

}