package cokr.xit.fims.base;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.user.ManagedUser;
import cokr.xit.fims.cmmn.DirectoryStructureToJson;
import cokr.xit.fims.cmmn.DirectoryStructureToJson.Node;
import cokr.xit.fims.crdn.service.bean.ImportServiceBean;
import cokr.xit.interfaces.smg.service.bean.SmgServiceBean;

@Controller
public class UserController extends cokr.xit.base.user.web.UserController<ManagedUser> {
	@Resource
	SmgServiceBean smgServiceBean;

	@Resource
	ImportServiceBean importServiceBean;

	@RequestMapping(name="최고관리자 메인",value="/superUser/main.do")
	public ModelAndView superUserMain() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/superUser-main");
		return mav;
	}

	@RequestMapping(name="배치 수동 실행",value="/superUser/executeBatch.do")
	public ModelAndView executeBatch(String batch) {
		ModelAndView mav = new ModelAndView();

		switch (batch) {
		case "smgReceive" : {
			smgServiceBean.receivePetitions();
			break;
		}
		case "smgSend" : {
			smgServiceBean.sendReplies();
			break;
		}
		case "epostSend" : {

			break;
		}
		case "epostReceive" : {

			break;
		}
		default:

		}
		mav.setViewName("jsonView");
		return mav;
	}

	@RequestMapping(name="업무통보 수동 실행",value="/superUser/executeAlert.do")
	public ModelAndView executeAlert(String alertName, String interfaceKey) {
		ModelAndView mav = new ModelAndView();

		switch (alertName) {
		case "smgReceive" : {

			if(interfaceKey == null || interfaceKey.equals("")) {
				new RuntimeException("인터페이스키 없음");
			}

			List<String> interfaceKeys = new ArrayList<String>();
			interfaceKeys.add(interfaceKey);
			importServiceBean.createCrdns(interfaceKeys);

			break;
		}
		case "smgSend" : {

			break;
		}
		case "epostSend" : {

			break;
		}
		case "epostReceive" : {

			break;
		}
		default:

		}
		mav.setViewName("jsonView");
		return mav;
	}

	@RequestMapping(name="서버 파일 현황 조회",value="/superUser/getFileTree.do")
	public ModelAndView getFileTree() throws Exception {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("jsonView");

		Node tree = DirectoryStructureToJson.getNode(new File("files"));
		mav.addObject("tree", tree);
		return mav;
	}

	@RequestMapping(name="최고관리자 서버 폴더 생성", value="/superUser/createDirectory.do")
	public ModelAndView createDirectory(@RequestParam(value="directories[]") String... directories) throws Exception {

		boolean saved = false;

		String path = String.join(File.separator, directories);
		File newDir = new File(path);
		saved = newDir.mkdir();

		ModelAndView mav = new ModelAndView();
		mav.setViewName("jsonView");
		mav.addObject("saved", saved);
		return mav;
	}

	@RequestMapping(name="최고관리자 서버 파일 추가", value="/superUser/importFile.do")
	public ModelAndView importFile(MultipartFile newFile, @RequestParam(value="directories[]") String... directories) throws Exception {

		boolean saved = false;

		String path = String.join(File.separator, directories);
		path = path + File.separator + newFile.getOriginalFilename();

		File file = new File(path);
		newFile.transferTo(file);

		saved = file.exists();

		ModelAndView mav = new ModelAndView();
		mav.setViewName("jsonView");
		mav.addObject("saved", saved);
		return mav;
	}

	@RequestMapping(name="최고관리자 서버 파일 내용 보기", value="/superUser/getFileText.do")
	public ModelAndView getFileText(@RequestParam(value="directories[]") String... directories) throws Exception {
		String fileCn = "";
		StringBuilder sb = new StringBuilder();

		String path = String.join(File.separator, directories);

		int inputData = 0;
		try {

			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path),Charset.forName("EUC-KR")));

			while ((inputData = br.read()) != -1) {
				sb.append((char)inputData) ;
			}

			fileCn = sb.toString();

			br.close();
		} catch (Exception e) {

		}

		ModelAndView mav = new ModelAndView();
		mav.setViewName("jsonView");
		mav.addObject("fileCn", fileCn);
		return mav;
	}
}