package com.icomsys.main_vm.biz.rcp.monitoring.service;

import com.icomsys.main_vm.biz.rcp.monitoring.vo.InsertWorkHstListVo;
import com.icomsys.main_vm.biz.rcp.monitoring.vo.RetrieveWorkHstListVo;
import com.icomsys.main_vm.biz.rcp.monitoring.vo.SearchWorkHstVo;
import com.icomsys.main_vm.common.code.MenuCategoryVo;
import com.icomsys.main_vm.common.code.MenuVo;
import com.icomsys.main_vm.common.code.enumresource.ActionResource;
import com.icomsys.main_vm.common.code.enumresource.MenuResource;
import com.icomsys.main_vm.db.jpa.entity.conversation.TbIcsLog;
import com.icomsys.main_vm.db.jpa.repo.system.TbIcsLogRepo;
import com.icomsys.main_vm.db.mybatis.alias.LoginVO;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class MonitoringWorkHistoryService {

    private final MappingJackson2JsonView ajaxMainView;

    private final TbIcsLogRepo tbIcsLogRepo;

    private final HttpServletRequest request;

    //작업이력 조회
    public QueryResults<RetrieveWorkHstListVo> retrieveWorkHst(SearchWorkHstVo searchWorkHstVo) {

//        ModelAndView mv = new ModelAndView(ajaxMainView);
//
//        List<RetrieveWorkHstListVo> nList = tbIcsLogRepo.retrieveWorkHst(searchWorkHstVo);
//
//        mv.addObject("nMap", nList);

        return tbIcsLogRepo.retrieveWorkHst(searchWorkHstVo);
    }

    public List<MenuVo> workhistoryMenuList() {
        List<MenuVo> result = new ArrayList<>();
        for (MenuResource e : MenuResource.LOGLIST) {
            result.add(MenuVo.builder()
                    .authName(e.getName())
                    .menuName(MenuResource.valueOf(e.getName()).getMenuDesc())
                    .build());
        }
        return result;
    }

    public List<MenuCategoryVo> workhistoryMenuCategoryList() {
        List<MenuCategoryVo> result = new ArrayList<>();
        for (ActionResource e : ActionResource.ALL) {
            result.add(MenuCategoryVo.builder()
                            .categoryValue(e.getName())
                            .typeName(e.getTypeName())
                    .build());
        }
        return result;
    }

    public void createWorkHst(InsertWorkHstListVo insertWorkHstListVo) {
        LoginVO resultVO = (LoginVO) request.getSession().getAttribute("LoginVO");

        tbIcsLogRepo.save(TbIcsLog.builder()
                .registDate(LocalDateTime.now())
                .userId(resultVO.getUserId())
                .actionCode(insertWorkHstListVo.getActionCode())
                .menuAuth(insertWorkHstListVo.getMenuAuth())
                .ip(getIP())
                .serviceGroup(insertWorkHstListVo.getServiceGroup())
                .remark(insertWorkHstListVo.getRemark())
                .build());
    }

    // ip 가져오기
    // IPv6로 가져오기 때문에 설정 필요함
    // https://gaemi606.tistory.com/entry/IntelliJ-IPv4-%EC%84%A4%EC%A0%95
    public String getIP() {

        String ip = request.getHeader("X-FORWARDED-FOR");

        if (ip == null || ip.length() == 0) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }


}
