package com.qy.ui;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionListener;

import com.qy.Interfaces.SmaliApkToolsPath;
import com.qy.utils.KeyTools;

import javax.swing.event.ListSelectionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class AndroidKey extends JFrame{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JTextField md5;
	private JTextField sha1;
	private JTextField sha256;
	private JTextPane textPane;
	private List<Map<String,String>> keys;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new AndroidKey();
	}
	public AndroidKey() {
		setResizable(false);
		setTitle("\u7B7E\u540D\u67E5\u770B\u5668");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initkeys();
		initView();
	}
	private void initkeys() {
		keys=new ArrayList<>();
		Map<String,String> secretKey=new HashMap<>();//amw.keystroe.jks
		secretKey.put("md5", "5D:E3:53:48:3A:48:1C:B7:56:BF:DA:FE:41:6F:10:69");
		secretKey.put("sha1", "34:5F:75:56:6E:EF:D4:73:CA:62:88:B8:3F:D2:AE:EE:35:1D:03:0A");
		secretKey.put("sha256", "42:CD:68:62:EC:D4:EE:56:BC:3E:1A:6B:54:0C:B4:38:2C:F6:F8:8A:2D:66:F5:3C:DB:56:E0:71:D1:0E:2F:4A");
		keys.add(secretKey);
		secretKey=new HashMap<>();//langtian.jks
		secretKey.put("md5", "23:69:26:CD:3E:A8:74:CF:5D:9C:32:77:EE:AF:65:AB");
		secretKey.put("sha1", "F5:19:13:91:70:05:B0:FE:3F:8B:56:99:4B:48:F3:73:4A:22:F2:5D");
		secretKey.put("sha256", "5C:BD:49:16:5B:3D:5F:26:6B:99:4A:10:1C:E5:DB:8F:A7:09:26:B5:6B:46:69:2D:12:9D:39:21:AF:24:75:7E");
		keys.add(secretKey);
		secretKey=new HashMap<>();//my.keystore
		secretKey.put("md5", "3B:F8:BA:42:7C:B3:91:A4:5C:3C:E0:1D:67:D6:17:49");
		secretKey.put("sha1", "3F:B3:26:07:13:84:BF:12:1A:41:E7:04:4E:63:7D:4F:F4:ED:FA:FA");
		secretKey.put("sha256", "C5:5C:4F:F7:5A:C3:5C:5A:A9:B5:8C:29:ED:AD:CE:CB:9E:62:DF:9C:12:94:39:3B:88:CF:5C:3F:0E:B6:96:29");
		keys.add(secretKey);

	}
	private JList<String> list;
	private void initView() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		setSize(849, 536);
		getContentPane().setLayout(null);

		list = new JList<String>();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				Map<String,String> map=keys.get(list.getSelectedIndex());
				if(map!=null) {
					md5.setText(map.get("md5"));
					sha1.setText(map.get("sha1"));
					sha256.setText(map.get("sha256"));
				}
			}
		});
		list.setModel(new AbstractListModel<String>() {
			/**
			 *
			 */
			private static final long serialVersionUID = 1L;
			String[] values = new String[] {"amw.keystroe.jks", "langtian.jks", "my.keystore"};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		list.setBounds(35, 87, 167, 167);
		getContentPane().add(list);

		JLabel lblMd = new JLabel("MD5:");
		lblMd.setBounds(269, 100, 72, 18);
		getContentPane().add(lblMd);

		JLabel lblSha = new JLabel("SHA1:");
		lblSha.setBounds(262, 131, 72, 18);
		getContentPane().add(lblSha);

		JLabel lblSha_1 = new JLabel("SHA256:");
		lblSha_1.setBounds(245, 162, 72, 18);
		getContentPane().add(lblSha_1);

		md5 = new JTextField();
		md5.setBounds(315, 99, 466, 24);
		getContentPane().add(md5);
		md5.setColumns(10);

		sha1 = new JTextField();
		sha1.setBounds(315, 132, 466, 24);
		getContentPane().add(sha1);
		sha1.setColumns(10);

		sha256 = new JTextField();
		sha256.setBounds(317, 170, 464, 24);
		getContentPane().add(sha256);
		sha256.setColumns(10);

		JButton btnSelectrsa = new JButton("\u9009\u62E9RSA\u6587\u4EF6");
		btnSelectrsa.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser=new JFileChooser(new File("").getAbsolutePath());
				chooser.showOpenDialog(null);
				File rsa=chooser.getSelectedFile();
				String data=KeyTools.getRSAkey(rsa, null, true);
				textPane.setText(data);
			}
		});
		btnSelectrsa.setBounds(254, 225, 140, 27);
		getContentPane().add(btnSelectrsa);

		JButton btnSelectkey = new JButton("\u9009\u62E9\u79D8\u94A5\u6587\u4EF6");
		btnSelectkey.addMouseListener(new MouseAdapter() {
			File rsa;
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser=new JFileChooser(new File("").getAbsolutePath());
				chooser.showOpenDialog(null);
				rsa=chooser.getSelectedFile();
				new EditToDalog("提示", "请输入密钥", null, new SmaliApkToolsPath() {

					@Override
					public void smaliPath(String path) {
						// TODO Auto-generated method stub
						String data=KeyTools.getRSAkey(rsa, path, false);
						textPane.setText(data);
					}
				});

			}
		});
		btnSelectkey.setBounds(450, 225, 148, 27);
		getContentPane().add(btnSelectkey);

		textPane = new JTextPane();
		JScrollPane scrollBar = new JScrollPane(textPane);
		scrollBar.setBounds(35, 289, 750, 187);
		getContentPane().add(scrollBar);
		setVisible(true);

	}
}
