package com.qy.ui;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.qy.utils.ApkSignTools;

import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;

public class ApkSignUi extends JFrame {
	public static void main(String[] args) {
		new ApkSignUi();
	}

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtPass;
	private JTextField txtSpass;
	private JTextField txtName;
	private ApkSignTools tools;
	private JLabel lblApkpath ;
	private JLabel lblSginpath;

	public ApkSignUi() {
		setTitle("\u7B7E\u540D\u5DE5\u5177v1.1");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		// TODO Auto-generated constructor stub
		tools=new ApkSignTools();
		setSize(448, 355);
		getContentPane().setLayout(null);
		lblApkpath = new JLabel("APKPath");
		JButton btnapk = new JButton("\u9009\u62E9APK");
		btnapk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jFile = new JFileChooser(new File(new File("").getAbsolutePath()));
				jFile.showOpenDialog(null);
				File file = jFile.getSelectedFile();
				if(file!=null&&file.exists()) {
					tools.setApkFile(file);
					lblApkpath.setText(file.getAbsolutePath());

				}
			}
		});
		btnapk.setBounds(36, 28, 105, 27);
		getContentPane().add(btnapk);


		lblApkpath.setBounds(46, 67, 289, 17);
		getContentPane().add(lblApkpath);

		JButton button = new JButton("\u9009\u62E9\u7B7E\u540D");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jFile = new JFileChooser(new File(new File("").getAbsolutePath()));
				jFile.showOpenDialog(null);
				File file = jFile.getSelectedFile();
				if(file!=null&&file.exists()) {
					tools.setSignFile(file);
					lblSginpath.setText(file.getAbsolutePath());
				}
			}
		});
		button.setBounds(36, 96, 105, 27);
		getContentPane().add(button);

		JButton button_1 = new JButton("\u4F7F\u7528\u9ED8\u8BA4\u7B7E\u540D");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File sign=new File("/media/yutou/_dde_data/AndroidKeys/langtian.jks");
				lblSginpath.setText(sign.getAbsolutePath());
				tools.setSignFile(sign);
				txtName.setText("langtian");
				txtPass.setText("134679");
				txtSpass.setText("134679");
			}
		});
		button_1.setBounds(220, 96, 130, 27);
		getContentPane().add(button_1);

		lblSginpath = new JLabel("sginPath");
		lblSginpath.setBounds(46, 142, 260, 17);
		getContentPane().add(lblSginpath);

		txtPass = new JTextField();
		txtPass.setText("\u5BC6\u7801");
		txtPass.setBounds(27, 173, 114, 21);
		getContentPane().add(txtPass);
		txtPass.setColumns(10);

		txtSpass = new JTextField();
		txtSpass.setText("\u522B\u540D\u5BC6\u7801");
		txtSpass.setBounds(27, 206, 114, 21);
		getContentPane().add(txtSpass);
		txtSpass.setColumns(10);

		txtName = new JTextField();
		txtName.setText("\u522B\u540D");
		txtName.setBounds(27, 239, 114, 21);
		getContentPane().add(txtName);
		txtName.setColumns(10);

		JButton btnSign = new JButton("\u7B7E\u540D");
		btnSign.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tools.setName(txtName.getText());
				tools.setKeyStorePassword(txtSpass.getText());
				tools.setSignKey(txtPass.getText());
				if(tools.chack()) {
					if(tools.sign()) {
						new TextToDalog("成功", "签名完成");
					}else{
						new TextToDalog("失败", "签名异常");
					}
				}else {
					new TextToDalog("错误", "APK文件或签名文件不存在");
				}
			}
		});
		btnSign.setBounds(263, 246, 105, 27);
		getContentPane().add(btnSign);
		setVisible(true);
	}
}
