package com.dbnt.kcscbackend.admin.config.service;

import com.dbnt.kcscbackend.admin.config.entity.TbMenuRole;
import com.dbnt.kcscbackend.admin.config.entity.TcMenu;
import com.dbnt.kcscbackend.admin.config.mapper.TcMenuMapper;
import com.dbnt.kcscbackend.admin.config.repository.TbMenuRoleRepository;
import com.dbnt.kcscbackend.admin.config.repository.TcMenuRepository;
import com.dbnt.kcscbackend.commonCode.entity.TcCodeGrp;
import com.dbnt.kcscbackend.commonCode.entity.TcCodeItem;
import com.dbnt.kcscbackend.commonCode.repository.TcCodeGrpRepository;
import com.dbnt.kcscbackend.commonCode.repository.TcCodeItemRepository;
import lombok.RequiredArgsConstructor;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdminConfigService extends EgovAbstractServiceImpl {

  private final TcCodeGrpRepository codeGrpRepository;
  private final TcCodeItemRepository codeItemRepository;
  private final TcMenuRepository menuRepository;
  private final TbMenuRoleRepository menuRoleRepository;
  private final TcMenuMapper menuMapper;

  public List<TcCodeGrp> selectCodeGrpList(){
    return codeGrpRepository.findByUseYn("Y");
  }

  @Transactional
  public String addCodeGrp(TcCodeGrp codeGrp) {
    TcCodeGrp savedGrp = codeGrpRepository.findById(codeGrp.getGrpCd()).orElse(null);
    if(savedGrp!=null){
      if(savedGrp.getUseYn().equals("Y")){
        return "isSaved";
      }else{
        savedGrp.setGrpCdNm(codeGrp.getGrpCdNm());
        savedGrp.setUseYn("Y");
        codeGrpRepository.save(savedGrp);
        return savedGrp.getGrpCd();
      }
    }else{
      codeGrpRepository.save(codeGrp);
      return codeGrp.getGrpCd();
    }
  }

  @Transactional
  public String modifyCodeGrp(TcCodeGrp codeGrp) {
    TcCodeGrp savedGrp = codeGrpRepository.findById(codeGrp.getGrpCd()).orElse(null);
    if (savedGrp != null) {
      savedGrp.setGrpCdNm(codeGrp.getGrpCdNm());
      savedGrp.setLastChgId(codeGrp.getLastChgId());
      savedGrp.setLastChgDt(codeGrp.getLastChgDt());
      savedGrp.setUseYn(codeGrp.getUseYn());
      codeGrpRepository.save(savedGrp);
      return "modified";
    }else{
      return "validGrpCd";
    }
  }

  public List<TcCodeItem> selectCodeItemList(String grpCd){
    return codeItemRepository.findByGrpCdAndUseYnOrderByGrpOrder(grpCd, "Y");
  }
  @Transactional
  public String addCodeItem(TcCodeItem codeItem) {
    TcCodeItem savedItem = codeItemRepository.findById(
        new TcCodeItem.TcCodeItemId(codeItem.getItemCd(), codeItem.getGrpCd())
    ).orElse(null);
    if(savedItem!=null){
      if(savedItem.getUseYn().equals("Y")){
        return "isSaved";
      }else{
        savedItem.setItemNm(savedItem.getItemNm());
        savedItem.setUseYn("Y");
        codeItemRepository.save(savedItem);
        return savedItem.getGrpCd();
      }
    }else{
      codeItemRepository.save(codeItem);
      return codeItem.getItemCd();
    }
  }

  @Transactional
  public String modifyCodeItem(TcCodeItem codeItem) {
    TcCodeItem savedItem = codeItemRepository.findById(
        new TcCodeItem.TcCodeItemId(codeItem.getItemCd(), codeItem.getGrpCd())
    ).orElse(null);
    if (savedItem != null) {
      savedItem.setItemNm(codeItem.getItemNm());
      savedItem.setLastChgId(codeItem.getLastChgId());
      savedItem.setLastChgDt(codeItem.getLastChgDt());
      savedItem.setGrpOrder(codeItem.getGrpOrder());
      savedItem.setUseYn(codeItem.getUseYn());
      codeItemRepository.save(savedItem);
      return "modified";
    }else{
      return "validGrpCd";
    }
  }

  public List<TcMenu> selectMenuList() {
    return menuMapper.selectMenuList();
  }

  @Transactional
  public void saveMenu(TcMenu menu, String userId) {
    TcMenu savedMenu = menuRepository.findById(menu.getMenuId()).orElse(null);
    if(savedMenu==null){
      menu.setFrstCrtDt(LocalDateTime.now());
      menu.setFrstCrtId(userId);
      menu.setUseYn("Y");
      menuRepository.save(menu);
    }else{
      savedMenu.setMenuTitle(menu.getMenuTitle());
      savedMenu.setMenuGroup(menu.getMenuGroup());
      savedMenu.setMenuLevel(menu.getMenuLevel());
      savedMenu.setMenuSort(menu.getMenuSort());
      savedMenu.setMenuUrl(menu.getMenuUrl());
      savedMenu.setMenuTypeCd(menu.getMenuTypeCd());
      savedMenu.setUseYn("Y");
      savedMenu.setLastChgId(userId);
      savedMenu.setLastChgDt(LocalDateTime.now());
      menuRepository.save(savedMenu);
    }
  }

  @Transactional
  public String deleteMenu(String menuId, String userId) {
    TcMenu savedMenu = menuRepository.findById(menuId).orElse(null);
    if(savedMenu==null){
      return "notFind";
    }else{
      savedMenu.setUseYn("N");
      savedMenu.setLastChgDt(LocalDateTime.now());
      savedMenu.setLastChgId(userId);
      return null;
    }
  }

  public List<TcMenu> selectMenuAuthList(){
    return menuMapper.selectMenuAuthList();
  }

  @Transactional
  public void editMenuAuth(TcMenu menu) {
    String[] roleAry = menu.getMenuAuth().split(",");
    menuRoleRepository.deleteByMenuId(menu.getMenuId());
    List<TbMenuRole> roleList = new ArrayList<>();
    for(String role: roleAry){
      if(!role.isEmpty()){
        TbMenuRole menuRole = new TbMenuRole();
        menuRole.setMenuId(menu.getMenuId());
        menuRole.setRoleId(role);
        menuRole.setWriteYn("Y");
        roleList.add(menuRole);
      }
    }
    menuRoleRepository.saveAll(roleList);
  }
}
