package com.qy.ui;

import javax.swing.JFrame;
import javax.swing.UIManager;

import org.json.JSONException;
import org.json.JSONObject;

import com.qy.Interfaces.SmaliApkToolsPath;
import com.qy.utils.AutoRandomAdSDK;
import com.qy.utils.Tools;

import javax.swing.JList;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;

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

public class ModifySdkUi extends JFrame{
	private JList<String> list;
	private File sdkFile=new File("D:\\IdeaProjects\\android_sdk_tools\\classes.jar");
	private Map<Integer, Boolean> userEncrypt;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public ModifySdkUi() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("\u7B2C\u4E09\u65B9SDK\u4FEE\u6539\u5DE5\u5177v1.5");
		// TODO Auto-generated constructor stub
		userEncrypt=new HashMap<Integer, Boolean>();
		initView();
		initData();
	}
	private void initView() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		setSize(473, 356);
		getContentPane().setLayout(null);
		
		list = new JList<String>();
		
		list.setBounds(45, 43, 145, 200);
		getContentPane().add(list);
		
		JButton btnNewButton = new JButton("\u5F00\u59CB");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(sdkFile!=null&&list.getSelectedIndex()>=0) {
					if(userEncrypt.get(list.getSelectedIndex())) {
						new EditToDalog("需要版本号", "请输入资源版本号", "", new SmaliApkToolsPath() {
							String version;
							@Override
							public void smaliPath(String path) {
								// TODO Auto-generated method stub
								version=path;
								if(version==null) {
									return;
								}
								new EditToDalog("资源类型", "请输入资源类型\n0=内核\n4=自主广告\n1=广点通\n5=聚量\n11=万维\n13=穿山甲", "", new SmaliApkToolsPath() {
									@Override
									public void smaliPath(String path) {
										// TODO Auto-generated method stub
										new AutoRandomAdSDK(sdkFile,version,path, list.getSelectedIndex(),new SmaliApkToolsPath() {
											@Override
											public void smaliPath(String path) {
												// TODO Auto-generated method stub
												new TextToDalog("提示", "处理完毕，文件路径："+path);
											}
										});
									}
								});
							}
						});
					}else {
						new AutoRandomAdSDK(sdkFile,null,null, list.getSelectedIndex(),new SmaliApkToolsPath() {
							@Override
							public void smaliPath(String path) {
								// TODO Auto-generated method stub
								new TextToDalog("提示", "处理完毕，文件路径："+path);
							}
						});
						
					}
					
				}else {
					new TextToDalog("警告", "未选择文件或未选择列表配置");
				}
			}
		});
		btnNewButton.setBounds(216, 143, 200, 100);
		getContentPane().add(btnNewButton);
		
		JButton btnSelectfile = new JButton("\u9009\u62E9jar\u6587\u4EF6");
		btnSelectfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser(new File("").getAbsolutePath());
				chooser.showOpenDialog(null);
				sdkFile = chooser.getSelectedFile();
			}
		});
		btnSelectfile.setBounds(260, 70, 105, 27);
		getContentPane().add(btnSelectfile);
		
		
		setVisible(true);
		
	}
	
	void initData() {
		JSONObject json=Tools.loadConfig(new File("ad_config.cfg"));
		Vector<String> vector = new Vector<>();
		for (int i = 0; i < json.getJSONArray("array").length(); i++) {
			try {
				vector.add(json.getJSONArray("array").getJSONObject(i).getString("title"));
				userEncrypt.put(i
						, !json.getJSONArray("array").getJSONObject(i).getBoolean("isJarLib"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		list.setListData(vector);
	}

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