package com.qy.ui;

import javax.swing.JFrame;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

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

import com.qy.Interfaces.DalogAbs;
import com.qy.sms.SDKTools;

import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class SMSToolsUi extends JFrame {

	/**
	 * 更新日志  v1.0： 初始版，默认热包版本 v2.22
	 * 			v1.1 修复Eclipse无法使用的问题
	 * 			v1.2 新增对话框回调 热更包更新至2.24
	 * 			v1.3 修复Eclipse无法打包的问题 ，自带jdk降低到1.7版本，采用jar.exe打包sdk
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnNewButton;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new SMSToolsUi();
	}

	public SMSToolsUi() {
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		setResizable(false);
		setTitle("\u77ED\u4EE3SDK\u81EA\u52A8\u751F\u6210\u5DE5\u5177v1.3");
		setSize(425, 336);
		getContentPane().setLayout(null);

		btnNewButton = new JButton("\u4E00\u952E\u751F\u6210SDK\u53CA\u6587\u6863");
		btnNewButton.addMouseListener(new MouseAdapter() {
			String path;

			@Override
			public void mouseClicked(MouseEvent e) {
				path = btnNewButton.getText();
				btnNewButton.setEnabled(false);
				btnNewButton.setText("正在编译SDK,请稍后...");
				new Timer().schedule(new TimerTask() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						SDKTools.start(new File("").getAbsolutePath());
						btnNewButton.setText(path);
						btnNewButton.setEnabled(true);
						new TextToDalog("提示", "计费SDK生成成功，请选择保存位置", new DalogAbs() {
							File sdk;

							@Override
							public void onSuccess() {
								// TODO Auto-generated method stub
								JFileChooser chooser = new JFileChooser(new File(new File("").getAbsolutePath()));
								chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
								chooser.showSaveDialog(null);
								sdk = chooser.getSelectedFile();
								if (sdk != null) {
									File sdk_ = new File(
											sdk.getAbsolutePath() + "/" + "计费sdk" + SDKTools.version + ".zip");
									SDKTools.buildSDK(sdk_);
									new TextToDalog("完成", "任务已完成", new DalogAbs() {

										@Override
										public void onSuccess() {
											// TODO Auto-generated method stub
											try {
												Desktop.getDesktop().open(sdk);
												System.out.println("\n本窗口可以关掉了");
											} catch (IOException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											}
										}
									});

								} else {
									new TextToDalog("错误", "已放弃本次任务");
								}
							}
						});

					}
				}, 100);

			}
		});
		btnNewButton.setBounds(48, 13, 324, 216);
		getContentPane().add(btnNewButton);
		JLabel label = new JLabel("(\u5185\u90E8\u4F7F\u7528\uFF0C\u5207\u52FF\u5916\u6F0F\uFF01\uFF09");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(65, 242, 278, 46);
		getContentPane().add(label);
		setVisible(true);
	}
}
