package com.tsiwebserver.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import com.tsiwebserver.VO.*;
import com.tsiwebserver.pageService.PageClass;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.data.repository.query.Param;
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.service.impl.SystemManagementServiceImpl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Controller
@RequiredArgsConstructor
public class SystemManagementController {

	private final SystemManagementServiceImpl systemManagemetServiceImp;

	// 시스템관리 페이지 이동
	@GetMapping("/systemManagement.do")
	public String ShowSystemManagement(Model model) {
		model.addAttribute("menu","system-manager");
		return "systemManagement";
	}

	@GetMapping("/node/getSearchWithCityNodeData.do")
	@ResponseBody
	public List<NodeVO> getSearchWithCityNodeData(String city) {
		return systemManagemetServiceImp.getSearchWithCity(city);
	}

	// 노드 데이터(시,구) 조회하기
	@GetMapping("/node/getSearchWithCityGuNodeData.do")
	@ResponseBody
	public List<NodeVO> getSearchNodeData(String city, String gu) {
		return systemManagemetServiceImp.getSearchWithCityAndGu(city, gu);
	}

	// 노드 데이터(시,구) 조회하기
	@GetMapping("/node/getSearchWithCityGuIsNodeData.do")
	@ResponseBody
	public List<NodeVO> getSearchWithCityGuIsNodeData(String city, String gu, String intersection) {
		return systemManagemetServiceImp.getSearchWithCityAndGuAndIntersection(city, gu, intersection);
	}

	// 노드 전체 조회
	@GetMapping("/node/getSearchNodeAllData.do")
	@ResponseBody
	public List<NodeVO> getSearchNodeData() {
		return systemManagemetServiceImp.getSearchAll();
	}

	// 노드 전체 조회
	@GetMapping("/node/getNodeData.do")
	@ResponseBody
	public List<NodeVO> getNodeData(String city, String gu, String intersection) {
		return systemManagemetServiceImp.getNodeData(city, gu, intersection);
	}
	
	//지역(시) 옵션 조회
	@GetMapping("/base/setCityOption.do")
	@ResponseBody
	public List<String> getCityOption() {
		return systemManagemetServiceImp.getCityName();
	}
	
	// 군/구 옵션 조회
	@GetMapping("/node/setGuOption.do")
	@ResponseBody
	public List<String> getGuOption(String city) {
		return systemManagemetServiceImp.getGuName(city);
	}
	
	//교차로 옵션 조회
	@GetMapping("/node/setIntersectionOption.do")
	@ResponseBody
	public List<String> getInterserctionOption(String city, String gu) {
		return systemManagemetServiceImp.getIntersection(city, gu);
	}
	
	//노드 추가
	@GetMapping("/node/addNode.do")
	@ResponseBody
	public int addNode(String jsonData) {
		JSONParser parser =new JSONParser();
		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
			return systemManagemetServiceImp.insertNode(array);
		} catch (ParseException e) {
			log.error("API : /base/addNode.do, Cause ParserException : {}", jsonData);
//			e.printStackTrace();
			return 0;
		}
	}
	
	//전체 노드
	@GetMapping("/node/getAllNodeId.do")
	@ResponseBody
	public List<String> getAllNodeId(){
		return systemManagemetServiceImp.getSearchAllNodeId();
	}
	
	//사용중인 권역 아이디
	@GetMapping("/api/getAllRegion.do")
	@ResponseBody
	public List<Map<String, Object>> getAllRegion(){
		return systemManagemetServiceImp.getAllRegion();
	}
	
	//노드 수정
	@GetMapping("/node/updateNode.do")
	@ResponseBody
	public int updateNode(String jsonData) {
		JSONParser parser =new JSONParser();
		try {
			JSONObject ob = (JSONObject)parser.parse(jsonData);
//			log.info(ob.toJSONString());
			return systemManagemetServiceImp.nodeUpdate(ob);
		} catch (ParseException e) {
			log.error("API : /base/updateNode.do, Cause ParserException : {}", jsonData);
//			e.printStackTrace();
			return 0;
		}
	}

	@GetMapping({"/node/updateMultiNode.do"})
	@ResponseBody
	public int updateMultiNode(String jsonData) {
		JSONParser parser = new JSONParser();

		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
			return this.systemManagemetServiceImp.nodeMultiUpdate(array);
		} catch (ParseException var4) {
			log.error("API : /base/updateMultiNode.do, Cause ParserException : {}", jsonData);
//			var4.printStackTrace();
			return 0;
		}
	}
	
	//노드 삭제
	@GetMapping("/node/deleteNode.do")
	@ResponseBody
	public int deleteNode(String nodeid) {
		return systemManagemetServiceImp.nodeDelete(nodeid);
	}
	
	//컨슈머 전체 데이터
	@GetMapping("/consumer/getSearchConsumerAllData.do")
	@ResponseBody
	public List<TscApiClientVO> getSearchConsumerAllData() {
		return systemManagemetServiceImp.getSearchConsumerAllData();
	}
	
	//업체명으로 검색
	@GetMapping("/consumer/getSearchCompanyData.do")
	@ResponseBody
	public List<TscApiClientVO> getSearchConsumerData(String company) {
		return systemManagemetServiceImp.getSearchCompanyData(company);
	}
	
	//전체 토큰 검색
	@GetMapping("/consumer/getAllConsumerToken.do")
	@ResponseBody
	public List<String> getAllConsumerToken(){
		return systemManagemetServiceImp.getAllConsumerToken();
	}
	
	//컨슈머 추가
	@GetMapping("/consumer/addConsumer.do")
	@ResponseBody
	public int addConsumer(String jsonData) throws UnsupportedEncodingException {
		JSONParser parser =new JSONParser();
		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
//			log.info(array.toJSONString());
			return systemManagemetServiceImp.addConsumer(array);
		} catch (NoSuchAlgorithmException e) {
			log.error("API : /consumer/addConsumer.do, Cause NoSuchAlgorithmException : {}", jsonData);
//			e.printStackTrace();
			return 0;
		} catch (ParseException e) {
			log.error("API : /consumer/addConsumer.do, Cause ParserException : {}", jsonData);
//			e.printStackTrace();
			return 0;
		}
	}
	
	//컨슈머 수정
	@GetMapping("/consumer/updateConsumer.do")
	@ResponseBody
	public int updateConsumer(String jsonData) {
		JSONParser parser =new JSONParser();
		try {
			JSONObject ob = (JSONObject)parser.parse(jsonData);
//			log.info("ob1 : {}",ob);
			return systemManagemetServiceImp.updateConsumer(ob);
		} catch (ParseException e) {
//			e.printStackTrace();
			log.error("API : /consumer/updateConsumer.do, Cause ParseException : {}", jsonData);
			return 0;
		} catch (Exception e) {
//			e.printStackTrace();
			log.error("API : /consumer/updateConsumer.do, Cause Exception : {}", jsonData);
			return 0;
		}
	}
	
	//컨슈머 삭제
	@GetMapping("/consumer/deleteConsumer.do")
	@ResponseBody
	public int deleteConsumer(Model model,String apitoken) {
		return systemManagemetServiceImp.consumerDelete(apitoken);
	}
	
	
	//토큰 설정 팝업
	@GetMapping("/consumer/setToken")
	public String showTokenPopup(){
		return "tokenPopup"; 
	}

	//노드 엑셀 업로드
	@GetMapping({"/node/nodeExcelUpload"})
	public String showNodeUploadPopup() {
		return "nodeExcelPopup";
	}
	
	//토큰 설정 확인 팝업
	@GetMapping("/consumer/checkToken")
	public String checkTokenPopup(){
		return "tokenCheck"; 
	}
	
	//알람 정책 데이터
	@GetMapping("/alarm/getSearchAlarm.do")
	@ResponseBody
	public List<AlarmConfigVO> getSearchAlarm(){
		return systemManagemetServiceImp.getSearchAlarm();
	}
	
	//알람 정책 수정
	@GetMapping("/alarm/updateAlarm.do")
	@ResponseBody
	public int updateAlarm(String jsonData) {
		JSONParser parser =new JSONParser();
			JSONArray array;
			try {
				array = (JSONArray)parser.parse(jsonData);
//				log.info(array.toJSONString());
				return systemManagemetServiceImp.updateAlarm(array);
			} catch (ParseException e) {
//				e.printStackTrace();
				log.error("API : /consumer/updateAlarm.do, Cause ParseException : {}", jsonData);
				return 0;
			}
	}
	
	@GetMapping("/alarm/getApiAcl.do")
	@ResponseBody
	public List<Map<String, Object>> getApiAcl(String apiToken){
//		log.info(apiToken);
		return systemManagemetServiceImp.getApiAcl(apiToken);
	}

	@GetMapping({"/base/baseExcelUpload"})
	public String showBaseUploadPopup() {
		return "baseExcelPopup";
	}

	@GetMapping({"/base/getBaseAllData.do"})
	@ResponseBody
	public List<BaseVO> getBaseData() {
		return this.systemManagemetServiceImp.getAllBase();
	}

	@GetMapping({"/base/getSearchBaseAllData.do"})
	@ResponseBody
	public List<BaseVO> getSearchBaseData(String city, String gu, String intersection, String nodeid) {
		return this.systemManagemetServiceImp.getSearchAllBase(city, gu, intersection, nodeid);
	}

	@GetMapping({"/base/setBaseCityOption.do"})
	@ResponseBody
	public List<String> getBaseCityOption() {
		return this.systemManagemetServiceImp.getBaseCityName();
	}

	@GetMapping({"/base/setBaseGuOption.do"})
	@ResponseBody
	public List<String> getBaseGuOption(String city) {
		return this.systemManagemetServiceImp.getBaseGuName(city);
	}

	@GetMapping({"/base/setBaseIntersectionOption.do"})
	@ResponseBody
	public List<String> getBaseInterserctionOption(String city, String gu) {
		return this.systemManagemetServiceImp.getBaseIntersection(city, gu);
	}

	@GetMapping({"/base/setBaseNodeIdOption.do"})
	@ResponseBody
	public List<String> getBaseNodeIdOption() {
		return this.systemManagemetServiceImp.getBaseNodeId();
	}

	@GetMapping({"/base/addBase.do"})
	@ResponseBody
	public int addBase(String jsonData) {
		JSONParser parser = new JSONParser();

		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
			return this.systemManagemetServiceImp.insertBase(array);
		} catch (ParseException var4) {
//			var4.printStackTrace();
			log.error("API : /base/addBase.do, Cause ParserException : {}", jsonData);
			return 0;
		}
	}

	@GetMapping({"/base/updateBase.do"})
	@ResponseBody
	public int updateBase(String jsonData) {
		JSONParser parser = new JSONParser();

		try {
			JSONObject ob = (JSONObject)parser.parse(jsonData);
			return this.systemManagemetServiceImp.baseUpdate(ob);
		} catch (ParseException var4) {
//			var4.printStackTrace();
			log.error("API : /base/updateBase.do, Cause ParserException : {}", jsonData);
			return 0;
		}
	}

	@GetMapping({"/base/updateMultiBase.do"})
	@ResponseBody
	public int updateMultiBase(String jsonData) {
		JSONParser parser = new JSONParser();

		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
			return this.systemManagemetServiceImp.baseMultiUpdate(array);
		} catch (ParseException var4) {
//			var4.printStackTrace();
			log.error("API : /base/updateMultiBase.do, Cause ParserException : {}", jsonData);
			return 0;
		}
	}

	@GetMapping({"/base/deleteBase.do"})
	@ResponseBody
	public int deleteBase(String nodeid) {
		return this.systemManagemetServiceImp.baseDelete(nodeid);
	}

	@GetMapping({"/base/getSearchLinkAllData.do"})
	@ResponseBody
	public List<LinkVO> getSearchLinkData() {
		return this.systemManagemetServiceImp.getSearchAllLink();
	}

	@GetMapping({"/base/getAllLinkId.do"})
	@ResponseBody
	public List<String> getAllLinkId() {
		return this.systemManagemetServiceImp.getSearchAllLinkId();
	}

	@GetMapping({"/base/addLink.do"})
	@ResponseBody
	public int addLink(String jsonData) {
		JSONParser parser = new JSONParser();

		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
			return this.systemManagemetServiceImp.insertLink(array);
		} catch (ParseException var4) {
//			var4.printStackTrace();
			log.error("API : /base/addLink.do, Cause ParserException : {}", jsonData);
			return 0;
		}
	}

	@GetMapping({"/base/deleteLink.do"})
	@ResponseBody
	public int deleteLink(String nodeid) {
		return this.systemManagemetServiceImp.linkDelete(nodeid);
	}

	@GetMapping({"/user/getSearchUser.do"})
	@ResponseBody
	public List<UserVO> getSearchUser() {
		return this.systemManagemetServiceImp.getSearchUser();
	}

	@GetMapping({"/user/allUserId.do"})
	@ResponseBody
	public List<String> getAllUserId() {
		return this.systemManagemetServiceImp.getAllUserId();
	}

	@GetMapping({"/user/addUser.do"})
	@ResponseBody
	public int addUser(String jsonData) {
		JSONParser parser = new JSONParser();
		try {
			JSONArray array = (JSONArray)parser.parse(jsonData);
			return this.systemManagemetServiceImp.addUser(array);
		} catch (ParseException var4) {
//			var4.printStackTrace();
			log.error("API : /base/addUser.do, Cause ParserException : {}", jsonData);
			return 0;
		}
	}

	@GetMapping({"/user/updateUser.do"})
	@ResponseBody
	public int updateUser(String jsonData) {
		JSONParser parser = new JSONParser();

		try {
			JSONObject obj = (JSONObject)parser.parse(jsonData);
			return this.systemManagemetServiceImp.updateUser(obj);
		} catch (ParseException var4) {
//			var4.printStackTrace();
			log.error("API : /base/updateUser.do, Cause ParserException : {}", jsonData);
			return 0;
		}
	}

	@GetMapping({"/user/deleteUser.do"})
	@ResponseBody
	public int deleteUser(String userId) {
		return this.systemManagemetServiceImp.deleteUser(userId);
	}
}
