package com.qy.ui;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

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

public class SmaliUi extends JFrame {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JTextField jarPath, savePath, smaliPath;
	private SmaliUtils utils;
	private Vector<String> vector;
	private JList<String> list;
	private List<File> fileList;
	private JTextField lblInfo;
	private String smaliHome;
	private JCheckBox chckbxSavesmalipath;
	private Timer timer;

	public static void main(String[] args) {
		new SmaliUi();
	}

	public SmaliUi() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		utils = new SmaliUtils();
		vector = new Vector<>();
		fileList = new ArrayList<>();
		timer = new Timer();
		initView();
	}

	private void initView() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		setTitle("Smali\u53CD\u7F16\u8BD1\u5DE5\u5177v1.2");
		// TODO Auto-generated constructor stub
		setSize(900, 673);
		getContentPane().setLayout(null);

		smaliPath = new JTextField();
		smaliPath.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				changeList();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				changeList();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				changeList();
			}

		});

		smaliPath.setBounds(45, 55, 678, 24);
		getContentPane().add(smaliPath);
		smaliPath.setColumns(10);

		JLabel lblFilepath = new JLabel(
				"SmaliPath(\u4FEE\u6539\u8981\u5728smali\u8DEF\u5F84\u91CC\uFF0C\u6253\u5305\u9700\u8981\u5728apktool.yml\u540C\u7EA7)");
		lblFilepath.setBounds(45, 24, 544, 18);
		getContentPane().add(lblFilepath);

		JButton btnFilepath = new JButton("select");
		btnFilepath.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser = new JFileChooser(new File("").getAbsolutePath());
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.showOpenDialog(null);
				smaliPath.setText(chooser.getSelectedFile().getAbsolutePath());
			}
		});
		btnFilepath.setBounds(737, 54, 113, 27);
		getContentPane().add(btnFilepath);

		list = new JList<String>();
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int index = list.getSelectedIndex();
					if (fileList.get(index).isDirectory()) {
						smaliPath.setText(fileList.get(index).getAbsolutePath());
					}
				}
			}
		});
		JScrollPane pane = new JScrollPane(list);
		pane.setBounds(45, 118, 335, 449);
		getContentPane().add(pane);

		JButton btnOpen = new JButton("Edit");
		btnOpen.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = list.getSelectedIndex();
				if (index > 0) {
					File file = fileList.get(index);
					if (file.isFile()) {
						try {
							Desktop.getDesktop().open(file);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		});
		btnOpen.setBounds(435, 137, 113, 27);
		getContentPane().add(btnOpen);

		JButton btnRename = new JButton("ReName");
		btnRename.addMouseListener(new MouseAdapter() {
			File file;
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = list.getSelectedIndex();
				if (index > 0) {
					file = fileList.get(index);
					new EditToDalog("重命名", "希望将文件(夹) " + file.getName() + " 重命名为？", file.getName(),
							new SmaliApkToolsPath() {

								@Override
								public void smaliPath(String path) {
									// TODO Auto-generated method stub
									if (path.trim().length() != 0 && !file.getName().equals(path)) {
										File toFile = new File(file.getParentFile().getAbsolutePath() + File.separator + path);
										file.renameTo(toFile);
										lblInfo.setText("重命名:" + toFile.getName());
										changeList();
									}
								}
							});

				}
			}
		});
		btnRename.setBounds(435, 177, 113, 27);
		getContentPane().add(btnRename);

		JButton btnMove = new JButton("Move");
		btnMove.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = list.getSelectedIndex();
				if (index > 0) {
					JFileChooser chooser = new JFileChooser(smaliPath.getText());
					chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					chooser.showSaveDialog(null);
					File path = chooser.getSelectedFile();
					File file = fileList.get(index);
					file.renameTo(new File(path.getAbsolutePath() + File.separator + file.getName()));
					lblInfo.setText("移动:" + file.getAbsolutePath());
					changeList();
				}
			}
		});
		btnMove.setBounds(562, 137, 113, 27);
		getContentPane().add(btnMove);

		JButton btnDelete = new JButton("Delete");
		btnDelete.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = list.getSelectedIndex();
				if (index > 0) {
					File file = fileList.get(index);
					file.delete();
					changeList();
				}
			}
		});
		btnDelete.setBounds(562, 177, 113, 27);
		getContentPane().add(btnDelete);

		JLabel lblAdb = new JLabel("Tools");
		lblAdb.setBounds(404, 241, 72, 18);
		getContentPane().add(lblAdb);

		JLabel lblFile = new JLabel("File");
		lblFile.setBounds(394, 117, 72, 18);
		getContentPane().add(lblFile);

		JLabel lblSetjarpath = new JLabel("jar/apk\u6587\u4EF6 (jar\u9700\u8981\u662Fdex\u7684\uFF09");
		lblSetjarpath.setBounds(414, 284, 321, 18);
		getContentPane().add(lblSetjarpath);

		jarPath = new JTextField();
		jarPath.setBounds(404, 315, 319, 24);
		getContentPane().add(jarPath);

		JButton btnSelect = new JButton("select");
		btnSelect.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser = new JFileChooser(new File("").getAbsolutePath());
				chooser.showOpenDialog(null);
				File file = chooser.getSelectedFile();
				jarPath.setText(file.getAbsolutePath());
			}
		});
		btnSelect.setBounds(737, 315, 113, 27);
		getContentPane().add(btnSelect);

		JLabel lblSavedexpath = new JLabel("\u4FDD\u5B58\u4F4D\u7F6E");
		lblSavedexpath.setBounds(414, 352, 134, 18);
		getContentPane().add(lblSavedexpath);

		savePath = new JTextField();
		savePath.setBounds(404, 383, 319, 24);
		getContentPane().add(savePath);

		JButton btnSelect_1 = new JButton("select");
		btnSelect_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser = new JFileChooser(new File("").getAbsolutePath());
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.showOpenDialog(null);
				savePath.setText(chooser.getSelectedFile().getAbsolutePath());
			}
		});
		btnSelect_1.setBounds(737, 380, 113, 27);
		getContentPane().add(btnSelect_1);

		JButton button = new JButton("\u62C6\u5305");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				lblInfo.setText("开始拆包");
				timer.schedule(new TimerTask() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						if(jarPath.getText().trim().length()==0) {
							new TextToDalog("警告", "没有选择目标文件");
							return;
						}
						utils.unPack(jarPath.getText(), new SmaliApkToolsPath() {

							@Override
							public void smaliPath(String path) {
								// TODO Auto-generated method stub
								smaliPath.setText(path);
								smaliHome = path;
								lblInfo.setText("拆包完成");
							}
						});

					}
				}, 100);
			}
		});
		button.setBounds(573, 454, 113, 113);
		getContentPane().add(button);

		JButton button_1 = new JButton("\u6253\u5305");
		button_1.addMouseListener(new MouseAdapter() {
			private String path;
			@Override
			public void mouseClicked(MouseEvent e) {
				if (chckbxSavesmalipath.isSelected()&&smaliHome!=null)
					smaliPath.setText(smaliHome);
				if(savePath.getText().trim().length()==0) {
					savePath.setText(smaliHome);
					lblInfo.setText("没有填写保存路径，默认保存在当前目录");
					new TextToDalog("警告", "没有填写保存路径，默认保存在当前目录");
				}
				if(jarPath.getText().trim().length()==0) {
					path="dex.zip";
					new TextToDalog("警告", "没有填写保存文件名，默认为dex.zip");
				}else {
					path=new File(jarPath.getText()).getName();
				}
				lblInfo.setText("开始打包");
				timer.schedule(new TimerTask() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						utils.bale(smaliPath.getText(), savePath.getText() + File.separator+path);
						lblInfo.setText("打包完成，保存路径为:" + savePath.getText());
					}
				}, 100);
			}
		});
		button_1.setBounds(715, 454, 113, 113);
		getContentPane().add(button_1);

		JButton btnOpenexlprer = new JButton("OpenExlprer");
		btnOpenexlprer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int index = list.getSelectedIndex();
				if (index > 0) {
					File file = fileList.get(index);
					String path;
					if (file.isDirectory()) {
						path = file.getAbsolutePath();
					} else {
						path = file.getParentFile().getAbsolutePath();
					}
					try {
						lblInfo.setText("已打开");
						Desktop.getDesktop().open(new File(path));
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		btnOpenexlprer.setBounds(689, 137, 139, 27);
		getContentPane().add(btnOpenexlprer);

		JButton btnNewButton = new JButton("\u4FEE\u6539");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (chckbxSavesmalipath.isSelected())
					smaliPath.setText(smaliHome);
				File file = new File(smaliPath.getText());
				for (File f : file.listFiles()) {
					if (f.getName().equals("smali") && f.isDirectory()) {
						smaliPath.setText(f.getAbsolutePath());
						break;
					}
				}
				lblInfo.setText("开始根据目录结构修改smali 请等待...");
				utils.setPath(smaliPath.getText());
				timer.schedule(new TimerTask() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						utils.start(new SmaliApkToolsPath() {
							@Override
							public void smaliPath(String path) {
								lblInfo.setText("完成");
							}
						});

					}
				}, 100);
			}
		});
		btnNewButton.setBounds(435, 454, 113, 113);
		getContentPane().add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Reload");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				changeList();
			}
		});
		btnNewButton_1.setBounds(689, 177, 139, 27);
		getContentPane().add(btnNewButton_1);

		lblInfo = new JTextField("info");
		lblInfo.setBorder(new LineBorder(Color.BLACK, 0));
		lblInfo.setHorizontalAlignment(SwingConstants.LEFT);
		lblInfo.setForeground(Color.BLACK);
		lblInfo.setEditable(false);
		lblInfo.setBounds(45, 580, 592, 18);
		getContentPane().add(lblInfo);

		chckbxSavesmalipath = new JCheckBox("\u8BB0\u5F55\u62C6\u5305\u7684Smali\u8DEF\u5F84");
		chckbxSavesmalipath.setSelected(true);
		chckbxSavesmalipath.setBounds(562, 9, 196, 37);
		getContentPane().add(chckbxSavesmalipath);

		JSeparator separator = new JSeparator();
		separator.setBounds(394, 220, 453, 2);
		getContentPane().add(separator);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(45, 92, 805, 2);
		getContentPane().add(separator_1);
		setVisible(true);
	}

	private void changeList() {
		// TODO Auto-generated method stub
		try {
			File file = new File(smaliPath.getText());
			File[] files = file.listFiles();
			vector.clear();
			fileList.clear();
			vector.add("返回上层");
			fileList.add(file.getParentFile());
			for (File f : files) {
				if (!f.isHidden()) {
					vector.add(f.getName());
					fileList.add(f);
				}
			}
			list.setListData(vector);
			lblInfo.setText("刷新界面");
		} catch (Exception e) {
			// TODO: handle exception
			lblInfo.setText("刷新失败");
		}

	}
}
