package com.dev.swms.server.web.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

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

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.dev.energy.server.dao.CommonDao;
import com.dev.energy.server.model.security.User;
import com.dev.swms.server.model.base.Worker;
import com.dev.swms.server.service.interfaceLog.QueryAppManager;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class LoginAppServlet
 */
public class SystemSetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected static ApplicationContext ac;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SystemSetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init(ServletConfig sc) throws ServletException {
		super.init(sc);
		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc.getServletContext());
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		CommonDao commonDao = (CommonDao) ac.getBean("commonDao");
//		request.setCharacterEncoding("utf-8");
//		System.out.println("==========修改密码========");
//		QueryAppManager app = (QueryAppManager) ac.getBean("queryAppManager");
//		String methodType = URLDecoder.decode(request.getHeader("method_type"),"UTF-8");
////		Long workerId = app.convertLong(request.getHeader("workerId"));
////		String userType = app.convertString(request.getHeader("userType"));
////		String oldPwd = app.convertString(request.getHeader("oldPwd"));
////		String newPwd = app.convertString(request.getHeader("newPwd"));
//		JSONObject resultJson = new JSONObject();
//		String status="1";
//		String data="";
//		String msg="";
//		try {
//			if(methodType.equals("updatePwd")){
//				if(newPwd!=null && !"".equals(newPwd)){
//					if(userType.equals("WORKER")){
//						Worker d=commonDao.get(Worker.class,workerId);
//						d.setPassWord(newPwd);
//						commonDao.store(d);
//						msg="修改成功,重新登录";
//					} else if(userType.equals("USER")){
//						User u=commonDao.get(User.class, workerId);
//						u.setPassword(shaEncodePassword(newPwd));
//						commonDao.store(u);
//						msg="修改成功,重新登录";
//					}else{
//						status="0";
//						msg="未找到!";
//					}
//				}
//			}else if(methodType.equals("upgrade")){
//				String version=getPropertiesParm("appVersion.version");
//				String url=getPropertiesParm("appVersion.url");
//				JSONObject obj = new JSONObject();
//				obj.put("version", version);
//				obj.put("url", url);
//				data=obj.toString();
//			}
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		resultJson.put("status", status);
//		resultJson.put("msg", msg);
//		resultJson.put("data", data);
//		PrintWriter out = null;
//		try {
//			System.out.println("========="+resultJson.toString());
//			response.setContentType(resultJson.toString());
//			response.setCharacterEncoding("UTF-8");
//			out = response.getWriter();
//			out.print(resultJson.toString());
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			out.close();
//		}	
	}
	
	private String shaEncodePassword(String painPwd){
		painPwd = painPwd == null ? "" : painPwd;
		if(painPwd.length() == 40){
			return painPwd;
		}
		return DigestUtils.shaHex(painPwd);
	}
	
	public String getPropertiesParm(String obj){
		String parm = "";
		Properties prop = new Properties();   
        InputStream in = this.getClass().getResourceAsStream("/hibernate.properties");   
        try {
			prop.load(in);
			parm = prop.getProperty(obj).trim();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		return parm;
	}

}
