package kr.gmtc.gw.asderecv.asde.parser;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

import kr.gmtc.gw.asderecv.kafka.producer.KafkaProducer;
import kr.gmtc.gw.asderecv.rest.vo.ServiceAsdeData;

public class AsterixParserThread {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private AsterixParser asterixParser;

	private boolean isRunning = true;
	private Thread thread;
	
	private Integer scvQcount = 3;
	private String debugLogMode = "none";
	
	List<ServiceAsdeData> serviceMap;
	
	ObjectMapper mapper;
	
	DateTimeFormatter dfPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
	
	KafkaProducer kafkaProducer;

	@SuppressWarnings("unused")
	public AsterixParserThread(Queue<String> packetQ, HashMap<Integer, Queue<ServiceAsdeData>> serviceQueue, KafkaProducer kafkaProducer) {
		thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while (isRunning) {
					String packetMsg = "";
					try {
						packetMsg = packetQ.poll();
						
						if (packetMsg != null) {
							 
							// 테스트용
							//String sBynary = "0A 05 BB E3 81 F1 91 03 8A 06 04 98 3C 00 C1 D9 F5 28 0E 72 45 54 48 33 36 36 37 20 04 01 68 45 54 41 57 45 20 20 20 02 8D 36 34 39 20 00 33 34 52 0B 8F 08 EB 81 71 01 03 02 06 04 98 41 00 D3 95 20 50 08 06 E8 41 46 4C 36 20 20 20 20 71 D1 48 53 49 41 31 34 38 20 20 08 20 EB 81 01 01 03 02 06 04 98 3D C1 CF 75 18 D7 01 00 E0 02 E0 EB 81 71 01 03 02 06 04 98 40 C1 EA EC E5 80 00 00 E8 46 49 52 45 35 20 20 20 71 D1 08 53 49 41 31 30 38 20 20 08 20 EB 81 01 01 03 02 06 04 98 3C 45 E9 6B EC FB 00 00 E0 02 E0 E3 81 81 81 03 02 06 04 98 3F C5 A3 C8 F1 38 06 03 02 AC 00 20 E3 81 81 81 03 02 06 04 98 41 05 46 90 F1 38 06 02 02 6A 00 20 EB 81 F1 91 03 8A 06 04 98 3E 46 E3 86 26 F7 F9 FB F2 07 E3 54 57 42 32 39 32 20 20 71 C3 24 48 4C 38 33 32 34 20 20 3F FE 31 32 39 20 02 33 33 52 04 A7 08 EB 81 F1 11 03 0A 06 04 98 3F 86 D5 73 10 DC FF 00 F8 04 00 4B 41 53 38 20 20 20 20 71 C2 12 48 4C 38 32 31 32 20 20 35 34 36 20 02 04 9A 08 EB 81 01 01 03 02 06 04 98 40 07 CB 78 28 01 00 00 E0 02 E0 EB 81 01 01 03 02 06 04 98 3C CB D6 2D 0F 8A 00 00 E0 02 E0 EB 81 01 01 03 02 06 04 98 3B 8E D3 69 14 D5 00 00 E0 02 E0 EB 81 01 01 03 02 06 04 98 30 51 E8 81 14 CC 00 00 E0 00 E0 EB 81 F1 91 03 8A 06 04 98 3E 12 DA FA 58 54 A1 07 F5 20 08 CF 4B 41 4C 35 35 31 20 20 71 BE 01 48 4C 37 36 30 31 20 20 00 6C 36 30 39 20 00 33 34 52 06 A5 08 E3 81 F1 91 03 8A 06 04 98 3C 14 E4 E6 F5 28 0E 73 41 49 48 32 32 31 20 20 71 C5 03 48 4C 38 35 30 33 20 20 02 73 36 31 36 20 00 33 34 52 05 B4 08 EB 81 F1 91 03 8A 06 04 98 3E 14 D1 26 0E 46 03 02 F2 09 1B 43 53 4E 33 30 36 31 20 78 1D AC 42 33 32 39 53 20 20 20 3F FE 31 32 34 20 02 33 34 4C 04 A6 08 EB 81 01 01 03 02 06 04 98 42 D8 CC 92 18 B2 00 00 E0 00 E0 EB 81 01 01 03 02 06 04 98 35 98 DE 2A 14 0B 00 00 E0 02 E0 EB 81 01 01 03 02 06 04 98 3E DA 0B 2E 16 DE FF 00 E0 00 E0 EB 81 01 01 03 02 06 04 98 40 9A DE FC 21 72 00 00 E0 00 E0 EB 81 01 01 03 02 06 04 98 27 5C D3 D8 32 98 00 00 E0 00 E0 EB 81 F1 91 03 8A 06 04 98 3E 9C D4 72 FA 1A E4 27 F2 07 A8 4B 41 4C 37 35 36 20 20 71 C2 76 48 4C 38 32 37 36 20 20 3F FD 32 34 36 20 02 33 34 4C 04 A8 08 EB 81 01 01 03 02 06 04 98 3F A0 EC 22 01 A7 00 00 E0 02 E0 EB 81 F1 91 83 8A 06 04 98 3E A1 2A 7C CF 65 D6 3B F2 06 BD 4A 4E 41 32 38 32 20 20 71 BF 57 48 4C 37 37 35 37 20 20 00 1D 32 30 20 20 80 71 33 33 52 00 33 33 52 04 A9 08 EB 81 F1 91 03 8A 06 04 98 40 22 05 38 14 50 FC FD F4 0E 70 47 45 43 38 33 39 31 20 3C 70 C9 44 41 4C 46 49 20 20 20 3F FF 36 33 33 20 02 33 34 52 04 23 08 EB 81 01 01 03 02 06 04 98 3F E4 EF 9C 04 0B 00 00 E0 00 E0 EB 81 71 01 03 02 06 04 98 3C E5 D9 E1 09 F9 FD 09 E8 53 41 46 45 54 59 31 20 71 D1 13 53 49 41 31 31 33 20 20 0A 20 EB 81 01 01 03 02 06 04 98 3F E5 EE CB FA B5 00 00 E0 00 E0 EB 81 F1 91 03 8A 06 04 98 3D 66 FE 13 13 E4 08 F5 F2 08 F2 43 59 5A 32 33 31 41 20 78 05 CA 42 32 38 38 31 20 20 20 3F FE 36 30 34 20 02 33 33 52 04 A4 08 EB 81 F1 91 03 8A 06 04 98 3F 66 F8 39 07 39 FB FD F4 0E 63 46 44 58 35 33 39 31 20 AC 25 C4 4E 38 38 32 46 44 20 20 3F FE 36 35 32 20 02 33 34 52 08 97 08 EB 81 E1 81 03 02 06 04 98 3B 28 10 A1 72 50 C4 44 F1 10 00 C1 48 4C 35 32 33 39 20 20 71 AA 39 01 6C 00 20 E3 81 F1 91 03 8A 06 04 98 3E 68 86 EA F5 20 0E 5D 44 4C 48 37 31 39 20 20 3C 67 0E 44 41 49 58 4E 20 20 20 00 6B 33 32 20 20 00 33 34 52 04 24 08 EB 81 01 01 03 02 06 04 98 41 29 CA 2D 1A CC 00 00 E0 02 E0 EB 81 01 01 03 02 06 04 98 39 2A 07 DB 16 5D 00 00 E0 00 E0 EB 81 01 01 03 02 06 04 98 3D ED 0D E0 18 E4 00 00 E0 00 E0 EB 81 01 01 03 02 06 04 98 2A AF CB D2 26 53 00 00 E0 02 E0 EB 81 F1 11 03 02 06 04 98 3A EF 0D 76 04 91 00 00 F2 08 E4 42 33 32 37 37 20 20 20 78 11 D5 42 33 32 37 37 20 20 20 38 34 32 20 02 E0 EB 81 01 01 03 02 06 04 98 3C 2F EA 89 ED 0F 00 00 E0 02 E0 EB 81 01 01 03 02 06 04 98 3B F2 CA 19 24 BF 03 03 E0 02 E0 EB 81 01 01 03 02 06 04 98 41 34 0F 9F 1B C5 00 00 E0 00 E0 EB 81 71 01 03 02 06 04 98 3D B7 01 C9 F6 FB FF FF E8 46 49 52 45 33 20 20 20 71 D1 09 53 49 41 31 30 39 20 20 08 20 EB 81 01 01 03 02 06 04 98 3D F8 D4 C1 12 10 00 00 E0 02 E0 E3 81 81 81 03 02 06 04 98 40 39 4E BC F1 38 06 01 02 44 00 20 EB 81 F1 91 03 8A 06 04 98 3D BA DA EC 01 57 05 F8 F2 06 A6 41 41 52 31 31 31 20 20 71 BF 92 48 4C 37 37 39 32 20 20 3F FE 32 31 20 20 02 33 34 4C 04 A2 08 EB 81 E1 81 03 02 06 04 98 3D BC 13 53 7B 69 C7 58 F1 30 0F 14 4A 4E 41 33 38 30 20 20 71 C0 14 02 29 00 20 EB 81 01 01 03 02 06 04 98 3D 7F 11 2A 1B 1E 00 00 E0 00 E0 ";
							//sBynary = sBynary.replaceAll("\\p{Z}", "");
							
							// UDP 수신데이터
							String sBynary = packetMsg.replaceAll("\\p{Z}", "");
							String sTime = null;
							
							// 수신시각
							if(sBynary.indexOf("*") > 0) {
								sTime = packetMsg.substring(packetMsg.indexOf("*") +1);
								sBynary = sBynary.substring(0, sBynary.indexOf("*"));
							}else {
								sTime = LocalDateTime.now().format(dfPattern);
							} 
					           
							// 수신 메시지(Hex) byte로 변환
						    byte[] data = new java.math.BigInteger(sBynary, 16).toByteArray();
						    
						    // byte문자열 확인
							// int recvCnt ++;
//						    logger.writeLevelLog("[AsterixParserThread] ByteArray(" + recvCnt + "):" + packetMsg, LogLevelType.LOG_DEBUG, "AllLog");
							
						    // 수신메시지 파싱
						    List<LinkedHashMap<String,String>> result = asterixParser.parse(data, sTime);

							// logger.info("[ASDE분석결과:"+LocalDateTime.now().format(dfPattern)+"] result = "+ result);
							//logger.info("[ASDE분석결과(service):"+LocalDateTime.now().format(dfPattern)+"] serviceMap = "+ Arrays.deepToString(serviceMap.toArray()));
						    
						    if(!serviceMap.isEmpty()) {
						    	
								List<ServiceAsdeData> srcList = new ArrayList<ServiceAsdeData>(); 
								srcList.addAll(serviceMap);

								serviceMap.clear();

								//kafkaProducer.sendASDEMessage(srcList);

						    	for(int idx=0; idx<srcList.size(); idx++) {
						    		for(int qi=0; qi<scvQcount; qi++) {
								    	serviceQueue.get(qi).offer(srcList.get(idx));
									}
						    	}

						    	
						    }

						}
					} catch (Exception e) {
						if (e.getMessage() != null) {
							logger.error("Parsing Error Message : " + e);
							logger.error("=> Packet Message : " + packetMsg);
						} else {
							logger.error("Parsing Error Packet Message :" + packetMsg);
						}

					}

				}
			}
		});
		
	}

	public void start() {

		serviceMap = new ArrayList<ServiceAsdeData>();
		
		// Asterix Parser 객체 생성 (category별로 상이한 yaml파일의 spec내용을 로드)
		asterixParser = new AsterixParser(serviceMap);
		asterixParser.setDebugLogMode(debugLogMode);
		
		mapper = new ObjectMapper();
		 
		isRunning = true;
		thread.start();
	}

	public void stop(){
		isRunning = false;		
	}

	public void setScvQcount(Integer scvQcount) {
		this.scvQcount = scvQcount;
	}

	public void setDebugLogMode(String debugLogMode) {
		this.debugLogMode = debugLogMode;
	}

	


}
