package kr.co.uplus.ez.view.main;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.data.ApiResponseMessage;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
public class MainViewController {
	
	// Vue일 경우 index로 전달
	@GetMapping("/")
	public String home() {
		return "index";
	}
	
	/*
	 * 엑셀 업로드 poi 4.1 사용 테스트 x
	 */
	@PostMapping("/insertFileXcel")
	@ResponseBody
	public ApiResponseMessage<?> insertFileXcel(MultipartHttpServletRequest multipartRequest) {

		MultipartFile file = multipartRequest.getFile("file");
		String colName = multipartRequest.getParameter("colName");
		ApiResponseMessage result = new ApiResponseMessage(ApiResponseCode.SUCCESS);

		try {
			int rowindex = 0;
			int columnindex = 0;

			InputStream in = file.getInputStream();
			@SuppressWarnings("resource")
			XSSFWorkbook workbook = new XSSFWorkbook(in);
			XSSFSheet sheet = workbook.getSheetAt(0);

			int rows = sheet.getPhysicalNumberOfRows();

			if (rows > 10000 || rows < 1) {
				// 엑셀 로우 건수가 10000건이 넘으면 false 반환
				result.setData(false);
			} else {
				HashMap<String, String> xcelData = new HashMap<String, String>();
				for (rowindex = 0; rowindex < rows; rowindex++) {
					XSSFRow row = sheet.getRow(rowindex);
					if (row != null) {
						int cells = row.getPhysicalNumberOfCells();
						for (columnindex = 0; columnindex <= cells - 1; columnindex++) {
							// 셀값을 읽는다
							XSSFCell cell = row.getCell(columnindex);
							String value = "";

							if (cell == null) {
								continue;
							} else {
								// 타입별로 내용 읽기
								switch (cell.getCellType()) {
								case FORMULA:
									value = cell.getCellFormula();
									break;
								case NUMERIC:
									value = cell.getNumericCellValue() + "";
									break;
								case STRING:
									value = cell.getStringCellValue() + "";
									break;
								case BLANK:
									value = cell.getBooleanCellValue() + "";
									break;
								}
							}
							if ("false".equals(value)) {
								// logger.info(rowindex+"번 행 Error :" +value);

								break;
							} else {
								// logger.info(rowindex+"번 행 : "+columnindex+"번 열 값은: "+value);
								// xcelData.put(value, value)
							}
						}
					}
					XSSFCell cell = row.getCell(columnindex);
				}
				result.setData(true);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.info("data : {}", result);
		return result;
	}

	/*
	 * 엑셀 업로드 poi 3.13 사용 rcs에서 테스트 완료
	 */

	/*
	 * @PostMapping("/insertFileXcel")
	 * 
	 * @ResponseBody public Result<?> insertFileXcel(MultipartHttpServletRequest
	 * multipartRequest){
	 * 
	 * MultipartFile file = multipartRequest.getFile("file"); String colName =
	 * multipartRequest.getParameter("colName"); Result<Map<String,Object>> result =
	 * new Result<>();
	 * 
	 * try { int rowindex=0; int columnindex=0;
	 * 
	 * InputStream in = file.getInputStream();
	 * 
	 * @SuppressWarnings("resource") XSSFWorkbook workbook = new XSSFWorkbook(in);
	 * XSSFSheet sheet=workbook.getSheetAt(0);
	 * 
	 * int rows=sheet.getPhysicalNumberOfRows(); // 엑셀 갯수 if(rows > 100 || rows < 1)
	 * { result.setSuccess(false); }else { HashMap<String, String> xcelData = new
	 * HashMap<String, String>(); for(rowindex=0;rowindex<rows;rowindex++){ XSSFRow
	 * row=sheet.getRow(rowindex); if(row !=null) { int
	 * cells=row.getPhysicalNumberOfCells(); for(columnindex=0;
	 * columnindex<=cells-1; columnindex++){ //셀값을 읽는다 XSSFCell
	 * cell=row.getCell(columnindex); String value="";
	 * 
	 * if(cell==null){ continue; }else{ //타입별로 내용 읽기 switch (cell.getCellType()){
	 * case XSSFCell.CELL_TYPE_FORMULA: value=cell.getCellFormula(); break; case
	 * XSSFCell.CELL_TYPE_NUMERIC: value=cell.getNumericCellValue()+""; break; case
	 * XSSFCell.CELL_TYPE_STRING: value=cell.getStringCellValue()+""; break; case
	 * XSSFCell.CELL_TYPE_BLANK: value=cell.getBooleanCellValue()+""; 7 break; } }
	 * if("false".equals(value)) { logger.info(rowindex+"번 행 Error :" +value);
	 * 
	 * break; }else { logger.info(rowindex+"번 행 : "+columnindex+"번 열 값은: "+value);
	 * // xcelData.put(value, value) } } } XSSFCell cell=row.getCell(columnindex); }
	 * result.setSuccess(true); } } catch (IOException e) {
	 * catch block e.printStackTrace(); } return result; }
	 */
}