package org.kne.cloud.klalb.uitool;

import javax.swing.JDialog;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.Window;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import org.kne.cloud.network.klalb.ui.UIEnv;

import javax.swing.ImageIcon;
import java.awt.Label;
import java.awt.Color;
import java.awt.Font;
import java.awt.TextField;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Random;

public class CrashReport extends JDialog{
	private JLabel name;
	private TextField type;
	private JTextArea stack;
	public CrashReport(Window parent ,boolean modal) {
		super(parent);
		setModal(modal);
		setTitle("错误");
		setIconImage(UIEnv.getIcon());
		setSize(500, 400);
		setLocationRelativeTo(null);
		stack = new JTextArea();
		stack.setEditable(false);
		getContentPane().add(new JScrollPane(stack), BorderLayout.CENTER);
		
		Panel panel = new Panel();
		panel.setPreferredSize(new Dimension(500, 100));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(CrashReport.class.getResource("/assets/error.jpg")));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(10, 10, 80, 80);
		panel.add(lblNewLabel);
		
		name = new JLabel("");
		name.setFont(new Font("宋体", Font.PLAIN, 30));
		name.setForeground(Color.RED);
		name.setBounds(96, 10, 378, 33);
		panel.add(name);
		
		type = new TextField();
		type.setEditable(false);
		type.setBounds(96, 67, 378, 23);
		panel.add(type);
		
		fny = new JLabel("...?");
		fny.setFont(new Font("MS Song", Font.PLAIN, 10));
		getContentPane().add(fny, BorderLayout.SOUTH);
	}
	String []s=new String[]{"鎴戜滑鍥㈤槦鍙湁涓�涓▼搴忓憳",
			"鎯虫洿鏂帮紝娌￠棬",
			"鎴戜滑鐨勬湇鍔″櫒鐪熺殑鏄笓涓氣�滃埌瀹垛�濅簡",
			"杩炴帴宸蹭涪澶憋細涓嶅幓鍋氬惎鍔ㄥ櫒灏卞幓濂宠锛屼笉鍘诲コ瑁呭氨鍘诲仛鍚姩鍣�",
			"鍦熻眴鏈嶅姟鍣ㄥ張鍙戣娊浜�"
	};
	private JLabel fny;
	public void report(Throwable e,String t){
		e.printStackTrace();
		name.setText(t);
		type.setText(e.getClass().getName());
		StringWriter sw=new StringWriter();
		PrintWriter pw=new PrintWriter(sw);
		e.printStackTrace(pw);
		pw.close();
		stack.setText(sw.toString());fny.setText(s[new Random().nextInt(s.length)]);
		setVisible(true);
		
	}
}
