package com.cku.util;

import java.io.File;
import java.util.Date;

public class PathUtils
{
	public static String getFileExtension(String fileName)
	{
		int index = fileName.lastIndexOf(".");
        if (index != -1)
        {
        	return fileName.substring(index);
        }
        else
        {
        	return "";
        }
    }
	
	public static String removeFileSpec(String path)
	{
		int index = path.lastIndexOf(File.separator);
		if (index != -1) /* Root?. */
		{
			if (index == 0 || path.charAt(index - 1) == ':') /* For windows. */
			{
				return path;
			}
			return path.substring(0, index);
		}
		
		return path;
	}
	
	public static String getFilename(String path)
	{
		String path2 = path.replace('\\', '/');
		if (path2.length() > 0 && path2.charAt(path2.length() - 1) == '/') /* Remove last path */
		{
			path2 = path2.substring(0, path2.length() - 1);
		}
		int index = path2.lastIndexOf('/');
		if (index != -1) /* Root?. */
		{
			if (index == 2 && path.charAt(1) == ':') /* For windows drive */
			{
				return null;
			}
			return path.substring(index + 1);
		}
		
		return null;
	}
	
	public static String mimeToExt(String e)
	{
		if (e.equalsIgnoreCase("text/plain"))
		{
			return ".txt";
		}
		else if (e.equalsIgnoreCase("text/html"))
		{
			return ".html";
		}
		else if (e.equalsIgnoreCase("video/courseware_easyhao"))
		{
			return ".dvi";
		}
		else if (e.equalsIgnoreCase("video/mpg"))
		{
			return ".mpg";
		}
		else if (e.equalsIgnoreCase("video/mp4"))
		{
			return ".mp4";
		}
		else if (e.equalsIgnoreCase("video/avi"))
		{
			return ".avi";
		}
		else if (e.equalsIgnoreCase("video/mkv"))
		{
			return ".mkv";
		}
		else if (e.equalsIgnoreCase("video/rm"))
		{
			return ".rm";
		}
		else if (e.equalsIgnoreCase("video/flv"))
		{
			return ".flv";
		}
		else if (e.equalsIgnoreCase("video/wmv"))
		{
			return ".wmv";
		}
		else if (e.equalsIgnoreCase("image/jpeg"))
		{
			return ".jpg";
		}
		else if (e.equalsIgnoreCase("image/png"))
		{
			return ".png";
		}
		else if (e.equalsIgnoreCase("image/bmp"))
		{
			return ".bmp";
		}
		else if (e.equalsIgnoreCase("image/tiff"))
		{
			return ".tiff";
		}
		else if (e.equalsIgnoreCase("image/gif"))
		{
			return ".gif";
		}
		else if (e.equalsIgnoreCase("application/shockwave-flash"))
		{
			return ".swf";
		}
		else if (e.equalsIgnoreCase("audio/mp3"))
		{
			return ".mp3";			
		}
		else if (e.equalsIgnoreCase("audio/aac"))
		{
			return ".aac";			
		}
		else if (e.equalsIgnoreCase("text/ms-word"))
		{
			return ".doc";			
		}
		else if (e.equalsIgnoreCase("text/ms-wordx"))
		{
			return ".docx";			
		}
		else if (e.equalsIgnoreCase("text/ms-ppt"))
		{
			return ".ppt";			
		}
		else if (e.equalsIgnoreCase("text/ms-pptx"))
		{
			return ".pptx";			
		}
		else if (e.equalsIgnoreCase("text/ms-excel"))
		{
			return ".xls";			
		}
		else if (e.equalsIgnoreCase("text/ms-excelx"))
		{
			return ".xlsx";			
		}
		else if (e.equalsIgnoreCase("text/pdf"))
		{
			return ".pdf";			
		}
		else
		{
			return ".bin";			
		}
	}
	
	public static String extToMime(String e)
	{
		if (e.equalsIgnoreCase(".txt") || e.equalsIgnoreCase(".log"))
		{
			return "text/plain";
		}
		else if (e.equalsIgnoreCase(".html") || e.equalsIgnoreCase(".htm"))
		{
			return "text/html";
		}
		else if (e.equalsIgnoreCase(".dvi"))
		{
			return "video/courseware_easyhao";
		}
		else if (e.equalsIgnoreCase(".mpg"))
		{
			return "video/mpg";
		}
		else if (e.equalsIgnoreCase(".mp4"))
		{
			return "video/mp4";
		}
		else if (e.equalsIgnoreCase(".avi"))
		{
			return "video/avi";
		}
		else if (e.equalsIgnoreCase(".mkv"))
		{
			return "video/mkv";
		}
		else if (e.equalsIgnoreCase(".rm") || e.equalsIgnoreCase(".rmvb"))
		{
			return "video/rm";
		}
		else if (e.equalsIgnoreCase(".jpg") || e.equalsIgnoreCase(".jpeg"))
		{
			return "image/jpeg";
		}
		else if (e.equalsIgnoreCase(".png"))
		{
			return "image/png";
		}
		else if (e.equalsIgnoreCase(".bmp"))
		{
			return "image/bmp";
		}
		else if (e.equalsIgnoreCase(".tiff"))
		{
			return "image/tiff";
		}
		else if (e.equalsIgnoreCase(".gif"))
		{
			return "image/gif";
		}
		else if (e.equalsIgnoreCase(".flv"))
		{
			return "video/flv";
		}
		else if (e.equalsIgnoreCase(".wmv"))
		{
			return "video/wmv";
		}
		else if (e.equalsIgnoreCase(".swf"))
		{
			return "application/shockwave-flash";
		}
		else if (e.equalsIgnoreCase(".mp3"))
		{
			return "audio/mp3";			
		}
		else if (e.equalsIgnoreCase(".aac"))
		{
			return "audio/aac";			
		}
		else if (e.equalsIgnoreCase(".doc"))
		{
			return "text/ms-word";			
		}
		else if (e.equalsIgnoreCase(".docx"))
		{
			return "text/ms-wordx";			
		}
		else if (e.equalsIgnoreCase(".ppt"))
		{
			return "text/ms-ppt";			
		}
		else if (e.equalsIgnoreCase(".pptx"))
		{
			return "text/ms-pptx";			
		}
		else if (e.equalsIgnoreCase(".xls"))
		{
			return "text/ms-excel";			
		}
		else if (e.equalsIgnoreCase(".xlsx"))
		{
			return "text/ms-excelx";			
		}
		else if (e.equalsIgnoreCase(".pdf"))
		{
			return "text/pdf";			
		}
		else
		{
			return "application/octet-stream";			
		}
	}
	
	private static boolean _isThisType(String mimeType, String[] typeList)
	{
		for (String ext : typeList)
		{
			if (mimeType.equals(ext))
			{
				return true;
			}
		}
		
		return false;
	}

	public static boolean isVideo(String mimeType) {
		String[] vExts = new String[] {
				"video/mp4", "video/mkv", "video/wmv", "video/rm", "video/flv", "video/avi"
		};
		
		return _isThisType(mimeType, vExts);
	}

	public static boolean isStdCw(String mimeType) {
		String[] vExts = new String[] {	"video/courseware_easyhao" };
		
		return _isThisType(mimeType, vExts);
	}

	public static boolean isDocument(String mimeType) {
		String[] vExts = new String[] {
				"text/pdf", "text/ms-excel", "text/ms-ppt", "text/ms-word", "text/ms-excelx", "text/ms-pptx", "text/ms-wordx", 
				"text/html", "text/plain"
		};
		
		return _isThisType(mimeType, vExts);
	}

	public static boolean isPicture(String mimeType) {
		String[] vExts = new String[] {
				"image/jpeg", "image/png", "image/bmp", "image/gif", "image/tiff"
		};
		
		return _isThisType(mimeType, vExts);
	}

	public static boolean isFolder(String mimeType) {
		String[] vExts = new String[] {
				"application/recycle", "application/folder", "application/group-root"
		};
		
		return _isThisType(mimeType, vExts);
	}

	public static boolean isSystem(String mimeType) {
		String[] vExts = new String[] {
				"application/recycle", "application/group-root", "application/home"
		};
		
		return _isThisType(mimeType, vExts);
	}

	/**
	 * Judge if path1 is a child path of path2.
	 * @param path1
	 * @param path2
	 * @return True if path1 is a child path of path2, otherwise, false.
	 */
	public static boolean isSubPath(String path1, String path2)
	{
		String[] pathSubArr = path1.trim().split("/");
		String[] pathArr = path2.trim().split("/");
		
		if (pathSubArr.length < pathArr.length) return false;
		for (int i = 0; i < pathArr.length; ++i)
		{
			if (!pathArr[i].equals(pathSubArr[i]))
			{
				return false;
			}
		}
		// TODO Auto-generated method stub
		return true;
	}

	private static final ThreadLocal<Long> s_index = new ThreadLocal<Long>();
	
	public static String specFilename() {
		Date d = new Date();
		
		Long index = s_index.get();
		if (index == null)
		{
			index = 1L;
		}
		long threadId = Thread.currentThread().getId();
		
		s_index.set(index + 1);
		
		return String.format("%d_%d_%d", threadId, index, d.getTime());
	}
}
