package egovframework.service.impl;

import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;
import egovframework.service.AdminService;
import egovframework.service.mapper.AdminMapper;
import egovframework.utill.SHA256;
import egovframework.vo.AdminVO;
import egovframework.vo.PopupVO;
import egovframework.vo.StatsVO;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequiredArgsConstructor
@Service
public class AdminServiceImpl extends EgovAbstractServiceImpl implements AdminService {

    private final AdminMapper adminMapper;
    Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);
    @Override
    public AdminVO getUser(String id) {
        return this.adminMapper.getUser(id);
    }

    @Override
    public void insertNotice(Map<String, Object> paramMap) {
        this.adminMapper.insertNotice(paramMap);
    }

    @Override
    public void deleteNotice(Map<String, Object> paramMap) {
        this.adminMapper.deleteNotice(paramMap);
    }

    @Override
    public void updateNotice(Map<String, Object> paramMap) {
        this.adminMapper.updateNotice(paramMap);
    }

    @Override
    public List<PopupVO> getPopupList(Map<String, Object> paramMap) {
        return this.adminMapper.getPopupList(paramMap);
    }

    @Override
    public void insertPopup(Map<String, Object> paramMap) {
        this.adminMapper.insertPopup(paramMap);
    }

    @Override
    public void updatePopupUseYN(Map<String, Object> paramMap) {
        this.adminMapper.updatePopupUseYN(paramMap);
    }

    @Override
    public void deletePopup(Map<String, Object> paramMap) {
        this.adminMapper.deletePopup(paramMap);
    }

    @Override
    public void updatePopup(Map<String, Object> paramMap) {
        this.adminMapper.updatePopup(paramMap);
    }

    @Override
    public Map<String, Object> checkPrePw(Map<String, Object> paramMap) {
        return this.adminMapper.checkPrePw(paramMap);
    }

    @Override
    public void updatePw(Map<String, Object> paramMap) {
        this.adminMapper.updatePw(paramMap);
    }

    @Override
    public List<StatsVO> getDDStatus(Map<String, Object> paramMap) {
        List<StatsVO> statsArr = this.adminMapper.getDDStatus(paramMap);
        for (StatsVO vo : statsArr) {
            int[] statsDdArr = new int[31];
            //0으로 초기화..
            Arrays.fill(statsDdArr, 0);
            if (vo.getConnCnt() != null) {
                String[] statsDd = vo.getDt().split(",");
                String[] connCnt = vo.getConnCnt().split(",");
                if (vo.getConnCnt() != null) {
                    for (int ii = 0; ii < statsDd.length; ii++) {
                        int dyNum = Integer.parseInt(statsDd[ii]);
                        statsDdArr[dyNum - 1] = Integer.parseInt(connCnt[ii]);
                    }
                }
                vo.setStatsInArr(statsDdArr);
            } else {
                //0으로 초기화..
                Arrays.fill(statsDdArr, 0);
                vo.setStatsInArr(statsDdArr);
            }
        }
        return statsArr;
    }

    @Override
    public List<StatsVO> getHHStatus(Map<String, Object> paramMap) {
        List<StatsVO> statsArr = this.adminMapper.getHHStatus(paramMap);
        for (StatsVO vo : statsArr) {
            if (vo.getConnCnt() != null) {
                String[] statsHh = vo.getDt().split(",");
                String[] connCnt = vo.getConnCnt().split(",");

                int[] statsHhArr = new int[24];
                //0으로 초기화..
                Arrays.fill(statsHhArr, 0);
                if (vo.getConnCnt() != null) {
                    for (int ii = 0; ii < statsHh.length; ii++) {
                        int dyNum = Integer.parseInt(statsHh[ii]);
                        statsHhArr[dyNum] = Integer.parseInt(connCnt[ii]);
                    }
                }
                vo.setStatsInArr(statsHhArr);


            } else {
                int[] statsHhArr = new int[24];
                //0으로 초기화..
                Arrays.fill(statsHhArr, 0);
                vo.setStatsInArr(statsHhArr);
            }
        }
        return statsArr;
    }

    @Override
    public List<StatsVO> getMMStatus(Map<String, Object> paramMap) {
        List<StatsVO> statsArr = this.adminMapper.getMMStatus(paramMap);
        for (StatsVO vo : statsArr) {
            int[] statsMmArr = new int[12];
            //0으로 초기화..
            Arrays.fill(statsMmArr, 0);
            if (vo.getConnCnt() != null) {
                String[] statsMm = vo.getDt().split(",");
                String[] connCnt = vo.getConnCnt().split(",");
                if (vo.getConnCnt() != null) {
                    for (int ii = 0; ii < statsMm.length; ii++) {

                        int dyNum = Integer.parseInt(statsMm[ii]);
                        statsMmArr[dyNum - 1] = Integer.parseInt(connCnt[ii]);
                    }
                }
                vo.setStatsInArr(statsMmArr);
            } else {
                //0으로 초기화..
                Arrays.fill(statsMmArr, 0);
                vo.setStatsInArr(statsMmArr);
            }
        }
        return statsArr;
    }

    @Override
    public void adminLoginMake(Map<String, Object> paramMap) {
        this.adminMapper.adminLoginMake(paramMap);
    }

    @Override
    public void adminLogoutMake(Map<String, Object> paramMap) {
        this.adminMapper.adminLogoutMake(paramMap);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public Map<String, Object> loginCheck(Map<String, Object> paramMap, HttpSession session) {
        Map<String,Object> resultMap = new HashMap<>();
        AdminVO userInfo = adminMapper.getUser(paramMap.get("email").toString());

        if (userInfo != null) {
            String pw = SHA256.encrypt(paramMap.get("password").toString()); // 원본
            if (userInfo.getIs_account_lock().equals("Y") ) {
                resultMap.put("msg", "3회 이상 로그인 실패로 계정 이용 불가\n관리자에게 문의하세요.");
            }
            else if (userInfo.getPassword().equals(pw)) {
                //로그인성공
                String id = userInfo.getEmail();
                session.setAttribute("userId", id);
                adminMapper.adminLoginMake(null);
                resultMap.put("code", "S");
                paramMap.put("count", 0);
                adminMapper.setFailCount(paramMap);
            }
            else {
                Map<String,Object> failCountMap = adminMapper.getFailCount(paramMap);
                int count;
                if (failCountMap == null) {
                    count = 0;
                }
                else {
                    count =  Integer.parseInt(String.valueOf(failCountMap.get("LOGIN_FAIL_COUNT")));
                }
                paramMap.put("count", ++count);
                adminMapper.setFailCount(paramMap);
                if (count > 2) {
                    adminMapper.setAcountLock(paramMap);
                    resultMap.put("msg", "3회 이상 로그인 실패로 계정 이용 불가\n관리자에게 문의하세요");
                }
                else {
                    //로그인 실패
                    resultMap.put("msg", "정보입력 " + count + " 회 오류");
                }
                resultMap.put("code", "F");
            }
        }
        else {
            resultMap.put("code", "F");
            resultMap.put("msg", "입력하신 정보를 다시 확인해주세요.");
        }
        return resultMap;
    }
}
