package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import service.HouseService;
import service.HouseServiceImpl;
import util.PagenationSupport;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entity.District;
import entity.House;
import entity.HouseType;
import entity.Street;
import entity.User;

public class HouseAction extends ActionSupport {
	HouseService houseService = new HouseServiceImpl();
	private PagenationSupport pagenaton;
	private int pagenow;
	private int id;
	private House house;
	private List<Street> streets;
	private List<HouseType> types;
	private List<District> districts;
	
	private File images;
	private String imagesContentType;
	private String imagesFileName;
	private String savePath="/images";
	
	
	public String upload() throws Exception {
		System.out.println("getImagesContentType()="+getImagesContentType());
		System.out.println("images.getName()=="+getImagesFileName());
		System.out.println("images!=null=="+images!=null);
		InputStream is=new FileInputStream(getImages());
		OutputStream os=new FileOutputStream(getSavePath()+"\\"+getImagesFileName());
		byte[] buffer = new byte[1024]; 
        while(is.read(buffer)>0){
        		os.write(buffer);
        }
		return "updloadSUCC";

	}

	// 分页显示所有房屋
	@SuppressWarnings("unchecked")
	public String getHousesByPage() {
		Map<String, Object> factor = houseService.getHouseFactor();
		streets = (List<Street>) factor.get("streets");
		types = (List<HouseType>) factor.get("housetypes");
		districts = (List<District>) factor.get("districts");
		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put("streets", streets);
		session.put("types", types);
		session.put("districts", districts);
		pagenaton = houseService.getHousesByPage(pagenow, pagenaton.PageSize);
		return "houselist";
	}
	public static Logger log=Logger.getLogger(HouseAction.class);
	
	// 新增房屋
	public String addHouse()throws IOException {
		
		Map<String,Object> session=ActionContext.getContext().getSession();
			User u= (User)session.get("user");
			house.setUser(u);
			System.out.println("u.getName()==="+u.getName());
			System.out.println("house.getHousetype().getId()==="+house.getHousetype().getId());
			System.out.println("house.getStreet().getId()==="+house.getStreet().getId());	
			System.out.println("images.getName()=="+imagesFileName);
			System.out.println("images!=null=="+images!=null);
		if(images!=null){
			InputStream is=new FileInputStream(images);
			//上传图片目录;设置图片名称:上传时间+用户名+image.getName().
			Date date=new Date(System.currentTimeMillis());
			String strDate=new SimpleDateFormat("yyyyMMddhhmmss").format(date);
			String fileName=strDate+u.getName()+imagesFileName;
			System.out.println("上传的图片："+imagesFileName+"存储到:"+getSavePath()+fileName);
			log.info("上传的图片："+images.getName()+"存储到:"+getSavePath()+fileName);
			//设置存储目标文件
			File toFile=new File(getSavePath(),fileName);
			OutputStream os=new FileOutputStream(toFile);
			 //设置缓存  
	        byte[] buffer = new byte[1024]; 
	        while(is.read(buffer)>0){
	        	os.write(buffer);
	        }
	        is.close();
	        os.close();
	        house.setPic(fileName);
		}
			houseService.save(house);
		return "houseAddSuccess";
	}

	// 查询district下的所有street
	public String getStreetByDistrict() throws IOException {
		streets = houseService.getStreetByDistrictId(id);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.print("<streets>");
		for (Street s : streets) {
			out.print("<street>" + "<streetid>" + s.getId() + "</streetid>"
					+ "<streetname>" + s.getStreet_name() + "</streetname>"
					+ "</street>");
		}
		out.println("</streets>");
		System.out.println(out);
		return null;
	}

	// 根据ID查询房屋明细
	public String getHouseDetail() {
		house = houseService.getHouseById(id);
		return "houseDetail";
	}

	public PagenationSupport getPagenaton() {
		return pagenaton;
	}

	public void setPagenaton(PagenationSupport pagenaton) {
		this.pagenaton = pagenaton;
	}

	public int getPagenow() {
		return pagenow;
	}

	public void setPagenow(int pagenow) {
		this.pagenow = pagenow;
	}

	public List<Street> getStreets() {
		return streets;
	}

	public void setStreets(List<Street> streets) {
		this.streets = streets;
	}

	public List<HouseType> getTypes() {
		return types;
	}

	public void setTypes(List<HouseType> types) {
		this.types = types;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public House getHouse() {
		return house;
	}

	public void setHouse(House house) {
		this.house = house;
	}

	public String getImagesContentType() {
		return imagesContentType;
	}

	public void setImagesContentType(String imagesContentType) {
		this.imagesContentType = imagesContentType;
	}

	public String getImagesFileName() {
		return imagesFileName;
	}

	public void setImagesFileName(String imagesFileName) {
		this.imagesFileName = imagesFileName;
	}
	//指定上传图片的保存位置
	@SuppressWarnings("deprecation")
	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getImages() {
		return images;
	}

	public void setImages(File images) {
		this.images = images;
	}

}
