package kr.gmtc.gw.eyegw.satats;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IntrfcRecvStatsManager {
    
    private Logger logger = LoggerFactory.getLogger("STATS");
    
    ObjectMapper mapper = new ObjectMapper();

    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
    DateFormat cnvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public IntrfcRecvStatsManager(){
    }

    public void writeStatsLog_KST(String strRecvDate, String strIntrfcId, String strIntrfcNm,  int iRecvCo, int iParseCo){

        IntrfcRecvStatsVO stsVO = new IntrfcRecvStatsVO();
        
        stsVO.setIntrfc_dt(strRecvDate);
		stsVO.setIntrfc_id(strIntrfcId);
        stsVO.setIntrfc_nm(strIntrfcNm);
        stsVO.setRecv_co(iRecvCo);
		stsVO.setParse_co(iParseCo);

        String cnvJsonString = "";

        try {
            cnvJsonString = mapper.writeValueAsString(stsVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        logger.info(cnvJsonString);

    }

    public void writeStatsLog(String strRecvDate, String strIntrfcId, String strIntrfcNm,  int iRecvCo, int iParseCo){

        IntrfcRecvStatsVO stsVO = new IntrfcRecvStatsVO();

        String strCnvDate = convertDate(strRecvDate);

        stsVO.setIntrfc_dt(strCnvDate);
		stsVO.setIntrfc_id(strIntrfcId);
        stsVO.setIntrfc_nm(strIntrfcNm);
        stsVO.setRecv_co(iRecvCo);
		stsVO.setParse_co(iParseCo);

        String cnvJsonString = "";

        try {
            cnvJsonString = mapper.writeValueAsString(stsVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        logger.info(cnvJsonString);

    }


    private String convertDate(String p_strDate){

        String strCnvDate = "";

        DateFormat df;

        Calendar cal = Calendar.getInstance();

        if(p_strDate == null || p_strDate.equals("")){
            strCnvDate = dateFormat.format(new Date());
        }else{

            // 1. 포멧 변경
            // 2. UTC -> KST

            if(p_strDate.length() > 18 ){
                df = cnvDateFormat;
            }else{
                df = dateFormat;
            }

            Date cnvDate = null;

            try {
                cnvDate = df.parse(p_strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            cal.setTime(cnvDate);
            cal.add(Calendar.HOUR_OF_DAY, 9);

            cnvDate = new Date(cal.getTimeInMillis());

            strCnvDate = dateFormat.format(cnvDate);
        }


        return strCnvDate;
    }


}
