package com.qy.ui;

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

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChuanShanJiaUi {
    private JPanel mainPanel;
    private JButton selectButton;
    private JTextArea log;
    private JCheckBox useAppPackageName;

    public ChuanShanJiaUi() {
        selectButton.addActionListener(new ActionListener() {
            File apkFile,csjDemo;
            @Override
            public void actionPerformed(ActionEvent e) {
               JFileChooser jfile=new JFileChooser(new File("").getAbsolutePath());
               jfile.showOpenDialog(null);
               apkFile=jfile.getSelectedFile();
               if(apkFile!=null){
                   append("已选择文件:"+apkFile.getAbsolutePath());
                   File csjPath=new File("input"+File.separator+"csjDemo"+File.separator);
                   if(csjPath.exists()){
                       File[] fs=csjPath.listFiles();
                       if(fs==null||fs.length==0){
                           append("没有找到穿山甲demo，请将穿山甲demo放置在 input"+File.separator+"csjDemo文件夹中");
                       }else{
                           selectButton.setEnabled(false);
                           append("穿山甲Demo选择为 "+fs[0].getName());
                           csjDemo=fs[0];
                           new Thread(new Runnable() {
                               @Override
                               public void run() {
                                   CsjTools tools=new CsjTools();
                                   tools.setUserPackageName(useAppPackageName.isSelected());
                                   tools.build(csjDemo.getAbsolutePath(), apkFile.getAbsolutePath(), new SmaliApkToolsPath() {
                                       @Override
                                       public void smaliPath(String path) {
                                           append(path);
                                           if(path.startsWith("已出包")) {
                                               new EditToDalog("成功", "打包完成，请输入版本号", "5600", new SmaliApkToolsPath() {
                                                   @Override
                                                   public void smaliPath(String out) {
                                                       System.out.println(">>" + out);
                                                       tools.out(path.replace("已出包", ""),
                                                               new File("csj").getAbsolutePath() + File.separator + "csc",
                                                               Integer.parseInt(out));
                                                       append("加密包位置:" + new File("csj").getAbsolutePath() + File.separator + "csc");
                                                       selectButton.setEnabled(true);
                                                   }
                                               });
                                           }

                                       }
                                   });
                               }
                           }).start();

                       }
                   }else{
                       append("没有找到穿山甲demo，请将穿山甲demo放置在 input"+File.separator+"csjDemo文件夹中");
                       csjPath.mkdirs();
                   }
               }
            }
        });
    }
    private void append(String log){
        this.log.append("["+new SimpleDateFormat("HH:mm:ss").format(new Date()) +"]"+log+"\n");
    }
    private void show(){
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        JFrame frame = new JFrame("穿山甲资源包生成v1.0");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setContentPane(new ChuanShanJiaUi().mainPanel);
        frame.pack();
        frame.setSize(420,400);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        JFrame frame = new JFrame("穿山甲资源包生成v1.0");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setContentPane(new ChuanShanJiaUi().mainPanel);
        frame.pack();
        frame.setSize(420,400);
        frame.setVisible(true);
    }


}
