package kr.gmtc.gw.dev.asderecv.asde.controller;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

import javax.annotation.Resource;

import kr.gmtc.gw.dev.asderecv.asde.rest.vo.AsdeServiceVO;
import kr.gmtc.gw.dev.asderecv.asde.rest.vo.ServiceAsdeData;
import kr.gmtc.gw.dev.asderecv.asde.rest.vo.ServiceHeader;
import kr.gmtc.gw.dev.asderecv.config.ServiceConfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@Component("AsdeServiceController")
public class AsdeServiceController{

	// 프레임 워크 구성요소 //
	/** 서비스 설정, {@code application.yml} */
	@Resource(name = "ServiceConfig")
	private ServiceConfig serviceConfig;
	
	/* 실생상태 확인 */
	private boolean running = false;
	
	/* UDP test 소켓 */
	//UDPEchoClient UDPSocket ;
	
	@Resource(name = "sendQueue")
	HashMap<Integer, Queue<ServiceAsdeData>> sendQueue;

	Queue<ServiceAsdeData> testMap = new LinkedBlockingQueue<ServiceAsdeData>();

	/* 서비스중인지 확인 */
	@Resource(name = "serviceRunnig")

	private boolean serviceRunnig = false;
	
	@Value("${asde.service.queueCount}")
	private Integer scvQcount;

	@Value("${asde.service.serviceCount}")
	private Integer scvDatacount;

	private AsdeServiceVO asdeServiceVO;
	
	@SuppressWarnings("deprecation")
	@GetMapping(value = "/getASDE/{qid}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String getASDE( @PathVariable Integer qid) {
		String sendCode, sendMsg ;
		ServiceHeader jsonHeader = new ServiceHeader();
		List<ServiceAsdeData> jsonData = new ArrayList<ServiceAsdeData>();
		
		sendCode = "200";
		sendMsg = "";
		
		asdeServiceVO = new AsdeServiceVO();
		
		int idx = qid -1;
		long qSize = sendQueue.get(idx).size();
		
		if(qSize > 0) serviceRunnig = true ;

		if(qSize > scvDatacount) qSize = scvDatacount;

		if(qid > scvQcount || qid <= 0) {
			sendCode = "Err-01";
			sendMsg = "할당되지 않은 큐를 지정했습니다.";
//			retList.add(sendMsg);
		}else {
			if(sendQueue.get(idx).peek() == null) {
				sendCode = "200";
				sendMsg = "전송할 데이터 없음.";
//				retList.add(sendMsg);
			}else{

				while (qSize > 0 ) {
					jsonData.add(sendQueue.get(idx).poll());
					qSize--;
				}
			}
			
		}
		
		jsonHeader.setResult_code(sendCode);
		jsonHeader.setResult_msg(sendMsg);
		
		asdeServiceVO.setHeader(jsonHeader);
		
		if (sendCode.equals("200")) {
			asdeServiceVO.setData(jsonData);
		}
		
		ObjectMapper mapper = new ObjectMapper();
		
		String sTmp = "";
		try {
			sTmp = mapper.writeValueAsString(asdeServiceVO);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		serviceRunnig = false ;
		
		return sTmp;
		
	}


}


