package com.dbnt.faisp.main.translator;

import com.dbnt.faisp.main.authMgt.service.AuthMgtService;
import com.dbnt.faisp.main.translator.model.Translator;
import com.dbnt.faisp.main.translator.model.TranslatorCrr;
import com.dbnt.faisp.main.translator.service.TranslatorService;
import com.dbnt.faisp.main.userInfo.model.UserInfo;
import com.dbnt.faisp.util.ParamMap;
import com.dbnt.faisp.util.Utils;

import lombok.RequiredArgsConstructor;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequiredArgsConstructor
@RequestMapping("/translator")
public class TranslatorController {

  private final AuthMgtService authMgtService;
  private final TranslatorService translatorSevice;
 
  
  @GetMapping("/info")
  public ModelAndView translatorInfo(@AuthenticationPrincipal UserInfo loginUser,Translator translator,HttpServletResponse response) {
    ModelAndView mav;
    if (translator.getDashboardFlag()){
      mav = new ModelAndView("translator/translatorDashboard");
      translator.setRowCnt(5);
    }else{
      mav = new ModelAndView("translator/translator");
    }
    translator.setDownOrganCdList(loginUser.getDownOrganCdList());
    //엑셀다운
    if(translator.getExcel() != null && translator.getExcel().equals("Y")){
    	String[] headers = { "translator_key", "ogdp1", "tr_lang", "tr_career", "tr_name", "tr_age", "tr_nny", "tr_edu", "tr_cft", "dml_yn", "apt_dt", "tr_phone"};
		String[] headerNames = { "연번", "관서명", "언어", "경력", "성명", "나이", "국적", "학력", "자격증", "해촉", "위촉일", "연락처" };
		String[] columnType = { "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String"};
		String sheetName = "민간 통역인 현황";
		String excelFileName = "민간 통역인 현황";
		List<ParamMap> translatorInfoList= translatorSevice.selectTranslatorListEx(translator);

		try { 
			Utils.listToExcel(translatorInfoList, response, headers, headerNames, columnType, sheetName, excelFileName);
		} catch (IOException e) {
			
		}
		return null;
    }
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/translator/info").get(0).getAccessAuth();
    
    mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    mav.addObject("userOrgan", loginUser.getOgCd());
    mav.addObject("accessAuth", accessAuth);
    translator.setQueryInfo();
    mav.addObject("translatorList", translatorSevice.selectTranslatorList(translator));
    translator.setContentCnt(translatorSevice.selectTranslatorListCnt(translator));
    translator.setPaginationInfo();
    mav.addObject("searchParams", translator);
    return mav;
  }
  
  @PostMapping("/insertTranslatorInfo")
  public String insertTranslatorInfo(@AuthenticationPrincipal UserInfo loginUser,Translator translator) {
	  translator.setWrtNm(loginUser.getUserNm());
	  translator.setWrtOrgan(loginUser.getOgCd());
	  translator.setWrtPart(loginUser.getOfcCd());
	  translator.setWrtTitle(loginUser.getTitleCd());
	  translator.setWrtDt(LocalDateTime.now());
    return translatorSevice.insertTranslatorInfo(translator);
  }
 
  @GetMapping("/translatorEditModal")
  public ModelAndView translatorEditModal(@AuthenticationPrincipal UserInfo loginUser,Translator translator){
    ModelAndView mav = new ModelAndView("translator/translatorEditModal");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/translator/info").get(0).getAccessAuth();
    mav.addObject("wrtUserSeq", translatorSevice.selectTrFristUserSeq(translator.getTranslatorKey()));
    mav.addObject("userSeq", loginUser.getUserSeq());
    mav.addObject("trInfo", translatorSevice.selectTranslatorView(translator));
    mav.addObject("mgtOrganList", loginUser.getDownOrganCdList());
    mav.addObject("accessAuth", accessAuth);
    return mav;
  }
  
  @PostMapping("/updateTranslatorInfo")
  public Translator updatetranslatorInfo(@AuthenticationPrincipal UserInfo loginUser,Translator translator) {
	  translator.setWrtNm(loginUser.getUserNm());
	  translator.setWrtOrgan(loginUser.getOgCd());
	  translator.setWrtPart(loginUser.getOfcCd());
	  translator.setWrtTitle(loginUser.getTitleCd());
	  translator.setWrtDt(LocalDateTime.now());
	  Translator result =  translatorSevice.updatetranslatorInfo(translator);
	  return result;
  }
  
  @GetMapping("/revisionHistory")
  public ModelAndView revisionHistory(Translator translator){
    ModelAndView mav = new ModelAndView("translator/translatorHistory");
    mav.addObject("historyList", translatorSevice.selectHistoryList(translator));
    return mav;
  }
  
  @GetMapping("/HistoryView")
  @ResponseBody
  public Translator HistoryView(Translator translator){

    return translatorSevice.HistoryView(translator);
  }
  
  @GetMapping("/careerView")
  public ModelAndView careerView(@AuthenticationPrincipal UserInfo loginUser,TranslatorCrr translatorCrr){
    ModelAndView mav = new ModelAndView("translator/translatorCareerModal");
    //메뉴권한 확인
    String accessAuth = authMgtService.selectAccessConfigList(loginUser.getUserSeq(), "/translator/info").get(0).getAccessAuth();
    mav.addObject("policeCrr", translatorSevice.selectMaritimePoliceCareer(translatorCrr));
    mav.addObject("anotherCrr", translatorSevice.selectAnotherOrganizationCareer(translatorCrr));
    mav.addObject("trKey", translatorCrr.getTranslatorKey());
    mav.addObject("wrtUserSeq", translatorSevice.selectTrFristUserSeq(translatorCrr.getTranslatorKey()));
    mav.addObject("userSeq", loginUser.getUserSeq());
    mav.addObject("trName", translatorSevice.selectTrLastName(translatorCrr));
    mav.addObject("accessAuth", accessAuth);
    return mav;
  }
  
  @GetMapping("/careerInsertPage")
  public ModelAndView careerInsertPage(TranslatorCrr translatorCrr){
    ModelAndView mav = new ModelAndView("translator/translatorCareerInsert");
    mav.addObject("trKey", translatorCrr.getTranslatorKey());
    return mav;
  }
  
  @PostMapping("/insertTranslatorCareer")
  @ResponseBody
  public int insertTranslatorCareer(@RequestBody List<TranslatorCrr> translatorCrr){
	  int trKey = translatorSevice.insertTranslatorCareer(translatorCrr);
    return trKey;
  }
  
  @PostMapping("/deleteCareer")
  @ResponseBody
  public int deleteCareer(@RequestBody TranslatorCrr translatorCrr) {
	  int trKey = translatorSevice.deleteCareer(translatorCrr);
    return trKey;
  }
  
  @PostMapping("/deleteTranslatorInfo")
  @ResponseBody
  public void deleteTranslatorInfo(@RequestBody int trKey) {
	  translatorSevice.deleteTranslatorInfo(trKey);
  }
  
  @GetMapping("/statisticsLang")
  public ModelAndView statisticsLang() {
    ModelAndView mav = new ModelAndView("translator/translatorStatisticsLang");
    mav.addObject("cntList", translatorSevice.selectStatisticsLangCnt());
    mav.addObject("total", translatorSevice.selectStatisticsLangTotal());
    return mav;
  }
  
  @GetMapping("/statisticsExcelDown")
  public void statisticsExcelDown(HttpServletResponse response) {
	    ParamMap total =translatorSevice.selectStatisticsLangTotal();
	    String[] headers = { "rownum", "lang", "cnt_total", "cnt_center", "cnt_west", "cnt_south", "cnt_east", "cnt_jeju"};
		String[] headerNames = { "어권별 구분", "", "총계", "중부청", "서해청", "남해청", "동해청", "제주청"};
		String[] headerNames2 = { "연번", "계", ""+total.getInt("total_total")+"", ""+total.getInt("total_center")+"", ""+total.getInt("total_west")+"", ""+total.getInt("total_south")+"", ""+total.getInt("total_east")+"", ""+total.getInt("total_jeju")+""};
		String[] columnType = { "int", "String", "int", "int", "int", "int", "int", "int"};
		String sheetName = "어권별 현황 통계";
		String excelFileName = "어권별 현황 통계";
		List<ParamMap> translatorInfoList= translatorSevice.selectStatisticsLangCnt();

		try { 
			Utils.downExcel(translatorInfoList, response, headers, headerNames,headerNames2, columnType, sheetName, excelFileName);
		} catch (IOException e) {
			
		}
  }
  
  
  

}
