package com.pack.service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.util.Logger;

public class PackServerProject extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 990146541904048926L;


	private static final Object lock = new Object();
	
	private static final String[] project_path = {"E:/elipse_work/leying/game_java/","E:/elipse_work/leying/server_web/"};
	private static final String[] upload_path = {"E:/upload/leying/update_game_java/","E:/upload/leying/"};
	
	private static void findall(HttpServletResponse resp) throws Exception{
		ITArray arr = DataBase.use("db2").executeQueryByTArray("select * from leying");
		String t = "<table border='1'><tr><th>项目名称</th><th>项目路径</th><th>上传路径</th></th><th>包文件名</th></th><th>操作</th></tr>";
		for (int i = 0; i < arr.size(); i++) {
			ITObject obj = arr.getTObject(i);
			int id = obj.getInt("id");
			String name = obj.getUtfString("name");
			String file_name = obj.getUtfString("file_name");
			String upload_path = obj.getUtfString("upload_path");
			String pro_path = obj.getUtfString("pro_path");
			t +=String.format("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href='/pack_server?id=%s'>打包</a></td></tr>", name,pro_path,upload_path,file_name,id);
		}
		t+="</table>";
		resp.getOutputStream().write(t.getBytes("GBK"));
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		if(req.getParameterMap().containsKey("id")){
			try {
				int id = Integer.parseInt(req.getParameter("id"));
				synchronized (lock) {
					ITArray arr = DataBase.use("db2").executeQueryByTArray("select * from leying where id="+id);
					if(arr.size()>0){
						ITObject obj = arr.getTObject(0);
						int type = obj.getInt("type");
						String name = obj.getUtfString("name");
						Logger log = Logger.getLogger(name);
						String path = project_path[type-1];
						String file_name = obj.getUtfString("file_name");
						String upload = upload_path[type-1] + obj.getUtfString("upload_path")+"/"+file_name;
						PackUtilService.exeCmd(path+"build.bat " +obj.getUtfString("pro_path")+" "+file_name+" " +upload ,log);
						PackUtilService.exeCmd("svn update "+upload_path[1], log);
						String tem_path = path + obj.getUtfString("pro_path")+"/target/"+file_name;
						Files.copy(new File(tem_path).toPath(), new File(upload).toPath(),StandardCopyOption.REPLACE_EXISTING);
						PackUtilService.exeCmd(upload_path[type-1]+"svn_add.bat \""+getIpAddr(req)+"["+name+"]\"",log);
						String t="<p>打包成功!</p><a href='/pack_server'>返回</a>";
						resp.getOutputStream().write(t.getBytes("GBK"));
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				findall(resp);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
