package com.dbnt.sims.menuMgt.service;

import com.dbnt.sims.menuMgt.mapper.MenuMgtMapper;
import com.dbnt.sims.menuMgt.model.MenuMgt;
import com.dbnt.sims.menuMgt.repository.MenuMgtRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.persistence.Transient;
import java.util.*;

@Service
@RequiredArgsConstructor
public class MenuMgtService {

  private final MenuMgtRepository menuMgtRepository;
  private final MenuMgtMapper menuMgtMapper;

  public List<MenuMgt> selectMenuMgtList(MenuMgt menuMgt){
    List<MenuMgt> menuList = menuMgtMapper.selectMenuMgtList(menuMgt);
    return menuListRowspanSet(menuList);
  }

  public Integer selectMenuMgtListCnt(MenuMgt menuMgt){
    return menuMgtMapper.selectMenuMgtListCnt(menuMgt);
  }

  public List<MenuMgt> selectMenuMgtListToAccessAuth(MenuMgt menuMgt){
    List<MenuMgt> menuList = menuMgtMapper.selectMenuMgtListToAccessAuth(menuMgt);
    return menuListRowspanSet(menuList);
  }
  public Integer selectMenuMgtListToAccessAuthCnt(MenuMgt menuMgt){
    return menuMgtMapper.selectMenuMgtListToAccessAuthCnt(menuMgt);
  }
  @Transient
  public String saveMenuMgt(MenuMgt menuMgt) {
    if(menuMgt.getMenuKey()==null){
      MenuMgt duplMenu = menuMgtRepository.findTopByCat1CdAndCat2CdAndCat3Cd(menuMgt.getCat1Cd(), menuMgt.getCat2Cd(), menuMgt.getCat3Cd());
      if(duplMenu!=null){
        return duplMenu.getMenuUrl();
      }else{
        menuMgtRepository.save(menuMgt);
      }
    }else{
      menuMgtRepository.save(menuMgt);
    }
    return "";
  }

  @Transient
  public void deleteMenuMgt(List<MenuMgt> menuList) {
    menuMgtRepository.deleteAll(menuList);
  }

  private List<MenuMgt> menuListRowspanSet(List<MenuMgt> menuList){
    for(int i=0; i<menuList.size()-1; i++){
      int rowspanCnt = 1;
      for(int j=i+1; j<menuList.size(); j++){
        if(menuList.get(i).getCat1Cd().equals(menuList.get(j).getCat1Cd())){
          menuList.get(j).setCat1RowspanCnt(0);
          rowspanCnt++;
        }else{
          menuList.get(i).setCat1RowspanCnt(rowspanCnt);
          i=j-1;
          break;
        }
      }
      if(menuList.get(i).getCat1RowspanCnt()==null){
        menuList.get(i).setCat1RowspanCnt(rowspanCnt);
      }
    }

    for(int i=0; i<menuList.size()-1; i++){
      int rowspanCnt = 1;
      for(int j=i+1; j<menuList.size(); j++){
        if(menuList.get(i).getCat2Cd().equals(menuList.get(j).getCat2Cd())){
          menuList.get(j).setCat2RowspanCnt(0);
          rowspanCnt++;
        }else{
          menuList.get(i).setCat2RowspanCnt(rowspanCnt);
          i=j-1;
          break;
        }
      }
      if(menuList.get(i).getCat2RowspanCnt()==null){
        menuList.get(i).setCat2RowspanCnt(rowspanCnt);
      }
    }
    return menuList;
  }

  public List<MenuMgt> selectAccessMenuListWhereUserSeq(Integer userSeq) {
    List<MenuMgt> accessMenuList =menuMgtMapper.selectAccessMenuListWhereUserSeq(userSeq);
    Set<String> cat1Set = new HashSet<>();
    Set<String> cat2Set = new HashSet<>();
    Set<String> cat3Set = new HashSet<>();
    for(MenuMgt accessMenu: accessMenuList){
      cat1Set.add(accessMenu.getCat1Cd());
      cat2Set.add(accessMenu.getCat1Cd()+accessMenu.getCat2Cd());
      if(!accessMenu.getCat3Cd().isEmpty()){
        cat3Set.add(accessMenu.getCat1Cd()+accessMenu.getCat2Cd()+accessMenu.getCat3Cd());
      }
    }

    List<MenuMgt> firstMenuList = new ArrayList<>();
    Iterator<String> cat1Itr = cat1Set.iterator();
    while(cat1Itr.hasNext()){
      String cat1Cd = cat1Itr.next();
      MenuMgt firstMenu = new MenuMgt();
      List<MenuMgt> secondMenuList = new ArrayList<>();
      firstMenu.setCat1Cd(cat1Cd);
      Iterator<String> cat2Itr = cat2Set.iterator();
      while(cat2Itr.hasNext()){
        String cat2Cd = cat2Itr.next();
        if(cat2Cd.contains(cat1Cd)){
          MenuMgt secondMenu = new MenuMgt();
          List<MenuMgt> thirdMenuList = new ArrayList<>();
          secondMenu.setCat2Cd(cat2Cd.replace(cat1Cd, ""));
          Iterator<String> cat3Itr = cat3Set.iterator();
          while(cat3Itr.hasNext()){
            String cat3Cd = cat3Itr.next();
            if(cat3Cd.contains(cat2Cd)){
              MenuMgt thirdMenu = new MenuMgt();
              thirdMenu.setCat3Cd(cat3Cd.replace(cat2Cd, ""));
              for(MenuMgt accessMenu: accessMenuList){
                if(accessMenu.getCat1Cd().equals(cat1Cd)
                    && accessMenu.getCat2Cd().equals(secondMenu.getCat2Cd())
                    && accessMenu.getCat3Cd().equals(thirdMenu.getCat3Cd())){
                  thirdMenu.setMenuUrl(accessMenu.getMenuUrl());
                  break;
                }
              }
              thirdMenuList.add(thirdMenu);
            }
          }
          if(thirdMenuList.size()>0){
            thirdMenuList.sort((o1, o2) -> {
              String cat3_1 = o1.getCat3Cd();
              String cat3_2 = o2.getCat3Cd();
              return cat3_1.compareTo(cat3_2);
            });
            secondMenu.setChildList(thirdMenuList);
          }else{
            for(MenuMgt accessMenu: accessMenuList){
              if(accessMenu.getCat1Cd().equals(cat1Cd)
                  && accessMenu.getCat2Cd().equals(secondMenu.getCat2Cd())){
                secondMenu.setMenuUrl(accessMenu.getMenuUrl());
                break;
              }
            }
          }
          secondMenuList.add(secondMenu);
        }
      }
      secondMenuList.sort((o1, o2) -> {
        String cat2_1 = o1.getCat2Cd();
        String cat2_2 = o2.getCat2Cd();
        return cat2_1.compareTo(cat2_2);
      });
      firstMenu.setChildList(secondMenuList);
      firstMenuList.add(firstMenu);
    }
    firstMenuList.sort((o1, o2) -> {
      String cat1_1 = o1.getCat1Cd();
      String cat1_2 = o2.getCat1Cd();
      return cat1_1.compareTo(cat1_2);
    });
    return firstMenuList;
  }
}
