
package nlib.col.web;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import nlib.bbs.service.ArticleVO;
import nlib.cmm.NlibCommonController;
import nlib.cmm.service.CodeService;
import nlib.cmm.service.NlibProperty;
import nlib.cmm.service.PagingVO;
import nlib.col.service.RisService;
import nlib.col.service.CollectionVO;
import nlib.col.service.DeptVO;
import nlib.col.service.InterestService;
import nlib.col.service.RisVO;
import nlib.restful.service.DataApiReqVO;
import nlib.restful.service.DataApiResVO;
import nlib.user.service.NlibLoginVO;
import nlib.util.StringUtil;

/**
 * @author JSYOO
 *
 */
@Controller
public class RisController extends NlibCommonController
{
	private static final Logger log = LoggerFactory.getLogger(RisController.class);
	static final String DEFUALT_PAGE_SIZE = NlibProperty.getProperty("list.paging.page.size");
	
	@Resource(name = "codeService") 
	private CodeService codeService;
	
	@Resource(name="risService")
	private RisService risService;

	@Resource(name = "interestService")
	private InterestService interestService;
	
	/**
	 * RIS파일 다운로드
	 * @param vo
	 * @throws IOException 
	 */
	@RequestMapping(value="/ris/risDownload.do")
	public void risDownload(HttpServletRequest request,HttpServletResponse response) throws IOException {
		RisVO vo = new RisVO();
		vo.setMasterId(request.getParameter("risId"));
		
		vo = risService.selectRisInfo(vo);
		
		response.setContentType("text/plain");
		
		String fileName = vo.getTt();
		String Ty="";
		// 브라우저 별 한글 인코딩
		String header = request.getHeader("User-Agent");
		if (header.contains("Edge")){
		    fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
		    response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".ris;\"");
		} else if (header.contains("MSIE") || header.contains("Trident")) { // IE 11버전부터 Trident로 변경되었기때문에 추가해준다.
		    fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
		    response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".ris;");
		} else if (header.contains("Chrome")) {
			fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
		    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".ris\"");
		} else if (header.contains("Opera")) {
		    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
		    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".ris\"");
		} else if (header.contains("Firefox")) {
		    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
		    response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".ris");
		}
		
		
		Ty = StringUtil.getString(vo.getTy(), "");
		
		if(Ty.length() > 3)
		{
			Ty = Ty.replaceFirst("IT_", "");
		}
				
        String s = "TY - " + Ty;
        s+= "\nTI - " + StringUtil.getString(vo.getTt(), "");
        s+= "\nPY - " + StringUtil.getString(vo.getPy(), "");
        s+= "\nKW - " + StringUtil.getString(vo.getKw(), "");
        s+= "\nT2 - " + StringUtil.getString(vo.getT2(), "");
        s+= "\nA1 - " + StringUtil.getString(vo.getA1(), "");
        s+= "\nPP - " + StringUtil.getString(vo.getPp(), "");
        s+= "\nM1 - " + StringUtil.getString(vo.getM1(), "");
        s+= "\nL4 - " + StringUtil.getString(vo.getL4(), "");
        s+= "\nER - ";
        
        InputStream input = new ByteArrayInputStream(s.getBytes("UTF8"));

        int read = 0;
        byte[] bytes = new byte[1024];
        OutputStream os = response.getOutputStream();

        while ((read = input.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        os.flush();
        os.close();
	}
	
}