package com.its.wthr.webapp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.its.app.utils.Ping;
import com.its.wthr.config.ServerConfig;
import com.its.wthr.webapp.service.FileService;
import com.its.wthr.webapp.vo.FileInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class WebAppCommonController {
	private final ServerConfig serverConfig;
	private final FileService fileService;

	public WebAppCommonController(ServerConfig serverConfig, FileService fileService) {
		this.serverConfig = serverConfig;
		this.fileService = fileService;
	}

	@RequestMapping({ "/", "/index" })
	public String index(Model model, HttpServletRequest request) {
		String result = "system";
		model.addAttribute("ServerConfig", this.serverConfig);
		model.addAttribute("ServerTime", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		return result;
	}

	@RequestMapping({ "/log" })
	public String logFiles(Model model, HttpServletRequest request) {
		String result = "log";
		List<FileInfoVo> info = this.fileService.getLogFiles();
		ObjectMapper mapper = new ObjectMapper();
		String jsonList = "";

		try {
			jsonList = mapper.writeValueAsString(info);
		} catch (IOException var8) {
			log.error("WebAppController.logFiles: Exception: {}", var8.toString());
		}

		model.addAttribute("ServerConfig", this.serverConfig);
		model.addAttribute("fileList", jsonList);
		model.addAttribute("ServerTime", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		return result;
	}

	@RequestMapping("/denied")
	public String denied() {
		return "/denied";
	}

	@RequestMapping({ "/login" })
	public String login(Model model) {
		String result = "login";
		model.addAttribute("ServerConfig", this.serverConfig);
		model.addAttribute("ServerTime", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		return result;
	}

	@GetMapping({ "/logout" })
	public void logout() {
	}

	@RequestMapping({ "/getServerDate" })
	@ResponseBody
	public Map<String, Object> getServerDate() {
		Map<String, Object> map = new HashMap<>();
		map.put("serverDate", (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		return map;
	}

	@RequestMapping(value = { "/getFileView" }, method = { RequestMethod.POST }, produces = { "application/json" })
	@ResponseBody
	public String getFileView(@RequestParam("fileName") String fileName, @RequestParam("filePath") String filePath,
			HttpServletRequest request) {
		return this.fileService.getView(request, fileName, filePath);
	}

	@RequestMapping({ "/getFileDownload" })
	public ResponseEntity<Resource> getFileDownload(@RequestParam("fileName") String fileName,
			@RequestParam("filePath") String filePath, HttpSession session, HttpServletRequest req,
			HttpServletResponse res, ModelAndView mav) throws UnsupportedEncodingException {
		return this.fileService.fileDownload(fileName, filePath);
	}

	@RequestMapping({ "/getFileDelete" })
	@ResponseBody
	public List<FileInfoVo> getFileDelete(@RequestParam("fileName") String fileName,
										  @RequestParam("filePath") String filePath) {
		this.fileService.fileDelete(fileName, filePath);
		List<FileInfoVo> fileArr = this.fileService.getLogFiles();
		return fileArr;
	}

	@RequestMapping({ "/getPing" })
	@ResponseBody
	public String getPing(@RequestParam("ipAddr") String ipAddr) {
		String result = this.ping(ipAddr);
		return result;
	}

	public String ping(String ipAddr) {
		String result = "";
		long startTm = System.nanoTime();
		long endTm = System.nanoTime();

		try {
				
			boolean isReachable = Ping.isReachable(ipAddr, 5);
			endTm = System.nanoTime();
				if (isReachable) {
					result = ipAddr + " is alive. " + Long.toString((endTm - startTm) / 1000000L) + " ms.";
				} else {
					result = ipAddr + " is not alive. time out: " + Long.toString((endTm - startTm) / 1000000L) + " ms.";
				}
		} catch (Exception var8) {
			result = ipAddr + " error. " + Long.toString((endTm - startTm) / 1000000L) + " ms. Exception: "
					+ var8.toString();
		}

		return result;
	}
}