package netmanager.common;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.sun.jna.Platform;

public class GlobalTool 
{
	public static void createDirectory(String strDirPath)
	{
		File file = new File(strDirPath);
		if(!file.exists())
		{
			try {
				Thread.sleep(25);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			file.mkdir();
		}
		else
		{
			delDirectory(strDirPath);
			createDirectory(strDirPath);
		}
	}
	
	public static void delDirectory(String strDirPath)
	{
		// 先删除文件夹中的文件
		delFiles(strDirPath);
		
		// 删除空文件夹
		File file = new File(strDirPath);
		file.delete();
	}
	
	public static void delFiles(String strPath) 
	{
		File file = new File(strPath);
		if(file.exists() && file.isDirectory())
		{
			String[] tmpList = file.list();
			File tmpFile = null;
			String strTmpSubPath = null;
			
			for(int i=0; i<tmpList.length; i++)
			{
				if(strPath.endsWith(File.separator))
				{
					strTmpSubPath = strPath + tmpList[i];
					tmpFile = new File(strPath + tmpList[i]);
				}
				else
				{
					strTmpSubPath = strPath + File.separator + tmpList[i];
					tmpFile = new File(strPath + File.separator + tmpList[i]);
				}
				
				if(tmpFile.isFile())
				{
					tmpFile.delete();
				}
				else if(tmpFile.isDirectory())
				{
					// 先删除文件夹中的文件
					delFiles(strTmpSubPath);
					
					// 删除空文件
					delDirectory(strTmpSubPath);
				}
			}
		}
	}
	
	public static String fileChooser(String strChooserTitle, String strExtName, String strExtFilter)
	{
		JFileChooser jfc = new JFileChooser();
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		FileNameExtensionFilter filter = new FileNameExtensionFilter(strExtName, strExtFilter);
		jfc.setFileFilter(filter);
		jfc.showDialog(new JLabel(), strChooserTitle);
		File file = jfc.getSelectedFile();
		
		String strFilePath = "";
		if(null != file && file.isFile())
		{
			strFilePath = file.getAbsolutePath();
		}
		
		return strFilePath;
	}
	
	public static String folderChooser(String strChooserTitle)
	{
		JFileChooser jfc = new JFileChooser();
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		jfc.showDialog(new JLabel(), strChooserTitle);
		File folder = jfc.getSelectedFile();
		
		String strFloderPath = "";
		if(null != folder && folder.isDirectory())
		{
			strFloderPath = folder.getAbsolutePath();
		}
		
		return strFloderPath;
	}
	
	public static Date convertStringToDate(String strTime, String strFormat) 
	{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
		try {
			date = sdf.parse(strTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return date;
	}
	
	public static String getEncodeType()
	{
		String strEncodeType = "";
		
		int nPlatform = Platform.getOSType();
		if(Platform.WINDOWS == nPlatform)
		{
			strEncodeType = "GBK";
		}
		else if(Platform.LINUX == nPlatform)
		{
			strEncodeType = "UTF-8";
		}
		
		return strEncodeType;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////
	// 定时器回调通知
	public static interface TimeTickerCallback {
	    void funcCb(long lTotalTime, long lTimeUsed, boolean bStop, Object userData);
	} 
	
	// 定时器
	public static class TimeTicker extends Thread
	{
		// 需计时多久, ms
		private long m_lTotalTime = 0;
		// 开始计时, ms
		private long m_lStartTick = 0;
		// 当前用时, ms
		private long m_lTickDelta = 0;
		// 停止标记
		private boolean m_bStop = true;
		// 回调
		private TimeTickerCallback m_func; 
		private Object m_userData;
		// 运行间隔
		private int m_tickInterval = 50;
		
		/********************************************************************************
			[in] nInterval 		回调通知间隔
			[in] nInterval 		回调通知间隔
			[in] lTotal			计时总时长，0表示不停止
			[in] func			计时回调通知函数
			[in] userData		用户自定义参数
		*********************************************************************************/
		public TimeTicker(int nInterval, long lTotal, TimeTickerCallback func, Object userData)
		{
			m_tickInterval = nInterval;
			m_lTotalTime = lTotal;
			m_func = func;
			m_userData = userData;
		}
		
		@Override
		public void run()
		{
			while(!m_bStop)
			{
				long lCurTick = getTimeTick();
				m_lTickDelta = lCurTick - m_lStartTick;
				
				if(0 < m_lTotalTime && m_lTickDelta >= m_lTotalTime)
				{
					// 停止
					m_bStop = true;
				}
				
				m_func.funcCb(m_lTotalTime, m_lTickDelta, m_bStop, m_userData);
				
				if(m_bStop)
				{
					break;
				}
				
				try {
					Thread.sleep(m_tickInterval);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		public void startTicking()
		{
			m_lStartTick = getTimeTick();
			m_bStop = false;
			this.start();
		}
		
		public void stopTicking()
		{
			m_bStop = true;
		}
		
		private long getTimeTick()
		{
			return System.currentTimeMillis();
		}
	}
	
	//////////////////////////////////////////////////////////////////////////////////////
}
