package com.nms.server.thread.file.upload;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Future;

import org.apache.log4j.Logger;

import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.dao.CommonDao;
import com.nms.server.service.UpgradeService;
import com.nms.server.util.ExceptionPrintUtils;
import com.nms.server.util.FileUtils;

public class FileUploadManagerThread implements Runnable{
	Logger logger = Logger.getLogger(FileUploadManagerThread.class);
	public void run() {

		//将线程运行程序，尽可能的catch捕获异常
		try {
			
//			Thread.currentThread().setName("文件上传管理线程");
			Thread.currentThread().setName("File Upload Management Thread");
			String rootDir = Constants.MISSION_FILE_UPLOAD_DIR;
			
			File rootFile = new File(rootDir);
			if(FileUtils.listFilesEndWithout(rootFile, ".tp").length==0){
				logger.info("根目录:> " + rootDir+" 无新文件");
				return ;
			}
			
			//任务回传文件归类	 增加任务ID解析
			Map<Long,List<File>> missionFileMaps = new HashMap<Long, List<File>>();
			File [] files = FileUtils.listFilesEndWithout(rootFile, ".tp");
			logger.info("根目录:> " + rootDir+" 新文件数："+files.length);
			CommonDao dao = null;
			UpgradeService service = null;
			try {
				for(File taskFile : files){
					try {
						Long mid = resoveMissionIdByFileName(taskFile.getName());
						List<File> fileNameList = missionFileMaps.get(mid);
						if(fileNameList == null){
							fileNameList = new LinkedList<File>();
							missionFileMaps.put(mid, fileNameList);
						}
						fileNameList.add(taskFile);
						String hostIp =  Common.getWebIPByMissionId(mid);
						if(hostIp==null){
							if(dao == null){
								dao = new CommonDao();
								service = new UpgradeService(dao);
							}
							hostIp = service.getHostIpByMissionId(mid);
							Common.addMissionIdWebIPMap(mid,hostIp);
						}
					}catch (NumberFormatException e) {
						logger.error("Getting the task ID error from the file name["+taskFile.getName()+"]",e);
						FileUtils.copyFile(taskFile, new File(taskFile.getAbsoluteFile()+".Error.tp"));
						taskFile.delete();
					}catch (Exception e) {
						logger.error("File："+taskFile.getName(),e);
						FileUtils.copyFile(taskFile, new File(taskFile.getAbsoluteFile()+".Error.tp"));
						taskFile.delete();
					}
				}
			} catch (Exception e) {
				logger.error("",e);
			}finally{
				if(dao!=null){
					dao.close();
					dao=null;
				}
			}
			
			if(missionFileMaps.size()>0){
				Iterator<Entry<Long,List<File>>> ite = missionFileMaps.entrySet().iterator();
				while (ite.hasNext()) {
					Entry<Long,List<File>> endtry = ite.next();
					Future<?> future = Common.getFutureMap().get(rootFile.getAbsolutePath()+endtry.getKey());
					if(future != null && !future.isCancelled() && !future.isDone()){	//运行中
						logger.info(rootFile.getAbsolutePath()+" 上传文件线程 运行中 不再启动新线程");
					}else{	//为空或空闲中
						logger.info(rootFile.getAbsolutePath()+" 上传文件线程 空闲中 启动新线程");
						String hostIp = Common.getWebIPByMissionId(endtry.getKey());
						if(hostIp==null){
							hostIp = Constants.WEB_SOCKET_IP;
						}
						future = Common.service.submit(new FileUploadThread3("Upload File Thread",hostIp,endtry.getValue().toArray(new File[0])));
						//注册
						Common.getFutureMap().put(rootFile.getAbsolutePath()+endtry.getKey(), future);
					}
				}
			}
			
		} catch (Exception e) {
			logger.error(ExceptionPrintUtils.printExceptionStack(e));
		}finally{
			logger.info("操作结束");
		}
	}
	
	public static Long resoveMissionIdByFileName(String fileName) {
		Long missionId = null;
		if(fileName.lastIndexOf("_T")!=-1){
//			System.out.println(fileName.lastIndexOf("_T"));
//			System.out.println(fileName.lastIndexOf("_"));
//			System.out.println(fileName.indexOf("_",fileName.lastIndexOf("_T")+1));
			String mid = fileName.substring(fileName.lastIndexOf("_T")+2,fileName.indexOf("_",fileName.lastIndexOf("_T")+1));
			missionId = Long.parseLong(mid);
		}else{
			String fileName0 = fileName.substring(0,fileName.lastIndexOf("_"));
			String mid = fileName0.substring(fileName0.lastIndexOf("_")+1,fileName0.length());
			missionId = Long.parseLong(mid);
		}
		
		return  missionId;
	}
	public static void main(String [] args) {
//		System.out.println("mid "+resoveMissionIdByFileName("tasklist_0_T176_1322424525354"));
//		System.out.println("mid "+resoveMissionIdByFileName("test_script_T1778_1363762154602994_relative.tar.gz"));
//		System.out.println("mid "+resoveMissionIdByFileName("tasklist_0__176_1322424525354.txt"));
		Common.service.submit(new FileUploadManagerThread());
	}
}
