package digital.laboratory.platform.entrustment.controller;

import cn.hutool.core.util.StrUtil;
import digital.laboratory.platform.common.feign.RemoteWord2PDFService;
import feign.Response;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FileUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;

@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/entrustment/test" )
public class ATestController {
	private final RemoteWord2PDFService remoteWord2PDFService;


	@GetMapping("/convert")
	public String convert() throws IOException {

		File file = new File("E:\\01.鉴定委托书--陕西分中心(1).docx");
		FileInputStream fis = new FileInputStream(file);
		MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "image/jpg", fis);
		Response r = remoteWord2PDFService.word2pdf(mockMultipartFile);
		fis.close();
		File destFile = new File("E:\\", "test.pdf");
		FileUtils.copyInputStreamToFile(r.body().asInputStream(), destFile);
		return "OK";
	}


	@GetMapping("/ip")
	public String ip(HttpServletRequest request) throws IOException {
		String ret = "";

		ret += "Request Headers:\n";
		ret += "---------------------------------\n";
		Enumeration headerNames = request.getHeaderNames();
		while(headerNames.hasMoreElements()){
			String paramName = (String)headerNames.nextElement();
			String paramValue = request.getHeader(paramName);
			ret += paramName+" = "+paramValue+"\n";
		}
		ret += "---------------------------------\n";
		ret += "\nrequest.getRequestedSessionId(): "+request.getRequestedSessionId();
		ret += "\nrequest.getMethod(): "+request.getMethod();
		ret += "\nrequest.getRequestURL(): "+request.getRequestURL();
		ret += "\nrequest.getRequestURI(): "+request.getRequestURI();
		ret += "\nrequest.getPathInfo(): "+request.getPathInfo();
		ret += "\nrequest.getContextPath(): "+request.getContextPath();
		ret += "\nrequest.getPathTranslated(): "+request.getPathTranslated();
		ret += "\nrequest.getQueryString(): "+request.getQueryString();
		ret += "\nrequest.getRemoteUser(): "+request.getRemoteUser();
		ret += "\nrequest.getRemoteAddr(): "+request.getRemoteAddr();
		ret += "\nrequest.getRemoteHost(): "+request.getRemoteHost();
		ret += "\nrequest.getRemotePort(): "+request.getRemotePort();
		ret += "\nrequest.getAuthType(): "+request.getAuthType();
		ret += "\nrequest.getServletPath(): "+request.getServletPath();

		return "<pre>"+ret+"</pre>";
	}

	String localVar = "";

	@GetMapping("/var")
	public String var(@RequestParam(value = "var", required = false) String var, HttpServletRequest request) throws IOException {
		String ret = "";
		if (StrUtil.isNotBlank(var)) {
			localVar = var;
			ret += "Set localVar to ["+localVar+"]\n";
		}

		ret += "localVar 的值是 ["+localVar+"]\n";

		return "<pre>"+ret+"</pre>";
	}

}
