package com.example.myapplication.Tool;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
//测试时使用
public class ErrorDisplayUtil {

    // 显示全量错误信息的对话框
    public static void showErrorDialog(Activity activity, String title, String errorDetail) {
        activity.runOnUiThread(() -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(title)
                    .setMessage(errorDetail)
                    .setPositiveButton("复制错误", (dialog, which) -> {
                        ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("错误详情", errorDetail);
                        clipboard.setPrimaryClip(clip);
                        Toast.makeText(activity, "已复制到剪贴板", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("关闭", null)
                    .show();
        });
    }

    // 在界面上固定位置显示错误面板（建议放在登录按钮下方）
    public static void showErrorPanel(Activity activity, String errorType, String solution) {
        activity.runOnUiThread(() -> {
            ViewGroup rootView = activity.findViewById(android.R.id.content);

            // 创建错误面板
            LinearLayout errorPanel = new LinearLayout(activity);
            errorPanel.setOrientation(LinearLayout.VERTICAL);
            errorPanel.setBackgroundColor(0x22FF0000); // 半透明红色背景
            errorPanel.setPadding(16, 16, 16, 16);

            TextView errorView = new TextView(activity);
            errorView.setTextColor(Color.RED);
            errorView.setText("⚠️ 错误类型: " + errorType);

            TextView solutionView = new TextView(activity);
            solutionView.setTextColor(Color.BLACK);
            solutionView.setText("💡 解决方案: " + solution);

            Button detailBtn = new Button(activity);
            detailBtn.setText("查看技术详情");
            detailBtn.setOnClickListener(v -> showErrorDialog(activity, "技术详情", errorType + "\n\n" + solution));

            errorPanel.addView(errorView);
            errorPanel.addView(solutionView);
            errorPanel.addView(detailBtn);

            // 添加到界面底部
            rootView.addView(errorPanel);

            // 5秒后自动隐藏
            new Handler().postDelayed(() -> rootView.removeView(errorPanel), 5000);
        });
    }
}
