package com.tsiwebserver.controller;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import io.micrometer.core.ipc.http.HttpSender;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tsiwebserver.VO.AlarmConfigVO;
import com.tsiwebserver.VO.AlarmVO;
import com.tsiwebserver.VO.ApiInvokeVO;
import com.tsiwebserver.VO.CommunicationVO;
import com.tsiwebserver.VO.CpuUseVO;
import com.tsiwebserver.VO.KafkaStatusVO;
import com.tsiwebserver.VO.ServerStatusVO;
import com.tsiwebserver.VO.UserVO;
import com.tsiwebserver.service.impl.DashBoardServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequiredArgsConstructor
@Slf4j
public class DashBoardController {
    private final DashBoardServiceImpl ds;
	
	@GetMapping("/getUseCpuVo.do")
	@ResponseBody
	public List<CpuUseVO> getUseCpuVo(){
		return ds.getUseCpuVo();
	}

	@GetMapping("/getKafkaStatus.do")
	@ResponseBody
	public List<KafkaStatusVO> getKafkaStatus(){
		List<KafkaStatusVO> objs = ds.getKafkaStatus();
		if (objs.size() != 30) {
			int cnt = 30 - objs.size();
			for (int ii = 0; ii < cnt; ii++) {
				KafkaStatusVO vo = new KafkaStatusVO();
				vo.init();
				objs.add(vo);
			}
		}
		return objs;
	}

	@GetMapping("/getCommuticationVO.do")
	@ResponseBody
	public List<CommunicationVO> getCommutication(){
		List<CommunicationVO> objs = ds.getCommuticationVO();
		if (objs.size() != 30) {
			int cnt = 30 - objs.size();
			for (int ii = 0; ii < cnt; ii++) {
				CommunicationVO vo = new CommunicationVO();
				vo.init();
				objs.add(vo);
			}
		}
		return objs;
	}

	@GetMapping("/getAlarmVO.do")
	@ResponseBody
	public List<AlarmVO> getAlarmVO(){
		return ds.getAlarmVO();
	}

	@GetMapping("/getServerStat.do")
	@ResponseBody
	public List<ServerStatusVO> getServerStat(){
		List<ServerStatusVO> objs= ds.getServerStat();
		return objs;
	}

    @GetMapping("/dashBoard.do")
    public String getChart(HttpSession session,Authentication auth, Model model){
        UserVO vo = (UserVO)auth.getPrincipal();
        session.setAttribute("auth", auth);
        session.setAttribute("vo", vo);
		model.addAttribute("menu","dashboard");
    	return "dashBoard";
    }
    
    @ResponseBody
    @GetMapping("/getUpdateAlarm.do")
    public int  updateAlarm(String jsonData){
    	JSONParser parser = new JSONParser();
			JSONArray array;
			try {
				array = (JSONArray)parser.parse(jsonData);
				return ds.alarmChecker(array);
			} catch (ParseException e) {
				log.error("API : /getUpdateAlarm.do, Cause ParserException");
//				e.printStackTrace();
				return 0;
			}
    }
    
    @ResponseBody
    @GetMapping("/kafakaStatus.do")
    public List<KafkaStatusVO> getKafka(){
        return ds.getKafkaStatus();
    }

    @ResponseBody
    @GetMapping("/cpuUseStatus.do")
    public List<CpuUseVO> getCpuUseVo(){
        return ds.getUseCpuVo();
    }

    @ResponseBody
    @GetMapping("/commuticationStatus.do")
    public List<CommunicationVO> getCommuticationVO(){
        return ds.getCommuticationVO();
    }

    @ResponseBody
    @GetMapping("/oneKafakaStatus.do")
//    public List<KafkaStatusVO> getOneKafakaStatus(){
    public KafkaStatusVO getOneKafakaStatus(){
//		List<KafkaStatusVO> vo = new ArrayList<>();
//		List<KafkaStatusVO> result = ds.getOneKafkaStatus();
//    	if (result == null || result.size() == 0) {
//			KafkaStatusVO newVo = new KafkaStatusVO();
//			newVo.init();
//			vo.add(newVo);
////			vo = new ArrayList<KafkaStatusVO>().add();
////    		vo = new KafkaStatusVO();
////    		vo.init();
//    	}
//		else {
//			vo = result;
//		}
//        return vo;
		KafkaStatusVO vo = this.ds.getOneKafkaStatus();
		if (vo == null) {
			vo = new KafkaStatusVO();
			vo.init();
		}

		return vo;
    }

    @ResponseBody
    @GetMapping("/oneCommuticationStatus.do")
    public CommunicationVO getOneCommutication(){
    	CommunicationVO vo = ds.getOneCommunicationStatus();
    	if (vo == null) {
    		vo = new CommunicationVO();
    		vo.init();
    	}
        return vo;
    }

    @ResponseBody
    @GetMapping("/search.do")
    public List<AlarmVO> search(String inputVal){
        List<AlarmVO> list = ds.getSearchService(inputVal);
        return list;
    }
    
    @ResponseBody
    @GetMapping("/getAllAlarmCode.do")
    public List<AlarmConfigVO> getAllAlarmCode(){
    	return ds.getAllAlarmCode();
    }
    
    @ResponseBody
    @GetMapping("/getAlarmWarning.do")
    public List<AlarmConfigVO> getAlarmWarning(){
    	return ds.getAlarmWarning();
    }
    
    @ResponseBody
    @GetMapping("/getOneCpuUse.do")
    public List<ServerStatusVO> getOneCpuUse() {
    	return ds.getOneCpuUse();
    }

    @ResponseBody
    @GetMapping("/getOneMemUse.do")
    public List<ServerStatusVO> getOneMemUse() {
    	return ds.getOneMemUse();
    }

    @ResponseBody
    @GetMapping("/getOneDiskUse.do")
    public List<ServerStatusVO> getOneDiskUse() {
    	return ds.getOneDiskUse();
    }
    
    @ResponseBody
    @GetMapping("/getOneResourceUse.do")
    public List<ServerStatusVO> getOneResourceUse() {
    	return ds.getOneResourceUse();
    }

    @ResponseBody
    @GetMapping("/getApiInvoke.do")
    public List<ApiInvokeVO> getApiInvokeVO(){
    	return ds.getApiInvokeVO();
    }

}
