package itn.let.mjo.addr.web;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import egovframework.rte.fdl.security.userdetails.util.EgovUserDetailsHelper;
import itn.com.cmm.EgovMessageSource;
import itn.com.cmm.LoginVO;
import itn.com.utl.fcc.service.EgovStringUtil;
import itn.let.fax.admin.service.FaxStatVO;
import itn.let.mail.service.StatusResponse;
import itn.let.mjo.addr.service.AddrGroupService;
import itn.let.mjo.addr.service.AddrGroupVO;
import itn.let.mjo.addr.service.AddrService;
import itn.let.mjo.addr.service.AddrVO;

/**
 * 주소록 관한 controller 클래스를 정의한다.
 * @author ITN
 * @since 2021.04.08
 * @version 1.0
 * @see
 *
 * <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일      수정자           수정내용
 *  -------    --------    ---------------------------
 *   2021.04.08  ITN          최초 생성
 *
 * </pre>
 */
@RestController
public class AddrRestController {

	
	@Resource (name = "AddrService")
	private AddrService addrService;
	
	@Resource (name = "AddrGroupService")
	private AddrGroupService addrGroupService;
	
	/** EgovMessageSource */
	@Resource(name="egovMessageSource")
	EgovMessageSource egovMessageSource;
	
	
	
    /**
     * 주소록 대량등록 저장
     * @param searchVO
     * @param model
     * @return	"/web/mjon/addr/addrMassInsertByTempAjax.do"
     * @throws Exception
     */
	@RequestMapping(value= {"/web/mjon/addr/addrMassInsertByTempAjax_advc.do"})
	public ResponseEntity<StatusResponse> addrMassInsertByTempAjax(@RequestBody List<AddrVO> addrListVO,
			RedirectAttributes redirectAttributes, 
			ModelMap model) throws Exception{
		
		
		System.out.println("??");
		System.out.println("?? " + addrListVO.get(0).toString());
		
		return ResponseEntity.ok().body(new StatusResponse(HttpStatus.OK, "", LocalDateTime.now()));
	} 
	
	/* 바이트 자르기
	UTF-8일 경우
	subStringBytes("블라블라블라라", 10, 3);
	EUC-KR일 경우
	subStringBytes("블라블라블라라", 10, 2);
	*/
	public String subStringBytes(String str, int byteLength, int sizePerLetter) {
		  int retLength = 0;
		  int tempSize = 0;
		  int asc;
		  if (str == null || "".equals(str) || "null".equals(str)) {
		    str = "";
		  }
		 
		  int length = str.length();
		 
		  for (int i = 1; i <= length; i++) {
		    asc = (int) str.charAt(i - 1);
		    if (asc > 127) {
		      if (byteLength >= tempSize + sizePerLetter) {
		        tempSize += sizePerLetter;
		        retLength++;
		      }
		    } else {
		      if (byteLength > tempSize) {
		        tempSize++;
		        retLength++;
		      }
		    }
		  }
		 
	  return str.substring(0, retLength);
	}


	
}