package com.dbnt.kcscbackend.standardCode.service;

import com.dbnt.kcscbackend.standardCode.entity.*;
import com.dbnt.kcscbackend.standardCode.mapper.StandardCodeMapper;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentContentRepository;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentFavoritesRepository;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentGroupRepository;
import com.dbnt.kcscbackend.standardCode.repository.TnDocumentInfoRepository;
import lombok.RequiredArgsConstructor;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class StandardCodeService extends EgovAbstractServiceImpl {

  private final TnDocumentGroupRepository tnDocumentGroupRepository;
  private final TnDocumentContentRepository tnDocumentContentRepository;
  private final TnDocumentInfoRepository tnDocumentInfoRepository;
  private final TnDocumentFavoritesRepository favoritesRepository;
  private final StandardCodeMapper standardCodeMapper;

  @Transactional
  public void saveErrorCd(TnDocumentContent content) {
    TnDocumentContent saveContent = tnDocumentContentRepository.findByDocContSeq(content.getDocContSeq()).orElse(null);
    if(saveContent!=null){
      saveContent.setErrorCd(content.getErrorCd());
    }
  }

  public List<StandardCodeTreeInterface> selectStandardCodeTree(){
    return tnDocumentGroupRepository.spGetTnDocumentCodeByTree();
  }

  public List<StandardCodeContentInterface> selectStandardCodeDocument(StandardCodeVO param) {
    return tnDocumentContentRepository.spGetRecentFullContextByContent(param.getDocCode(), param.getDocPart(), param.getYmd());
  }

  public List<TnDocumentInfoInterface> selectStandardCodeDocInfo(StandardCodeVO param) {
    return tnDocumentInfoRepository.spGetTnDocumentInfoByGroupCd(param.getDocCode());
  }

  public List<TnDocumentCodeList> selectStandardCodeList(TnDocumentInfo tnDocumentInfo){
    List<TnDocumentCodeList> codeList = standardCodeMapper.selectStandardCodeList(tnDocumentInfo);
    List<TnDocumentInfo> historyList = standardCodeMapper.selectStandardCodeHistoryList(tnDocumentInfo);
    for(TnDocumentCodeList code: codeList){
      List<TnDocumentInfo> tempList = new ArrayList<>();
      for(TnDocumentInfo history: historyList){
        if(code.getKcscCd().equals(history.getKcscCd())){
          tempList.add(history);
        }
      }
      code.setHistoryList(tempList);
    }
    return codeList;
  }

  public TnDocumentCodeList selectStandardCodeListCnt(TnDocumentInfo tnDocumentInfo) {
    return standardCodeMapper.selectStandardCodeListCnt(tnDocumentInfo);
  }

  public List<TnDocumentInfo> selectStandardCodeRevisionhistoryList(Integer groupseq) {
    return standardCodeMapper.selectStandardCodeRevisionhistoryList(groupseq);
  }

  public Integer selectStandardCodeGroupSeq(String groupFullCd) { return standardCodeMapper.selectStandardCodeGroupSeq(groupFullCd); }


  public List<TnDocumentGroup> selectTnDocumentGroupToGroupFullCdLike(String groupFullCd) {
    return tnDocumentGroupRepository.findByGroupFullCdLike(groupFullCd);
  }

  public void saveFavorites(Integer userSeq, Integer groupSeq){
    TnDocumentFavorites favorites = new TnDocumentFavorites();
    favorites.setUserSeq(userSeq);
    favorites.setGroupSeq(groupSeq);
    favoritesRepository.save(favorites);
  }

  public void deleteFavorites(Integer userSeq, Integer groupSeq){
    favoritesRepository.deleteById(new TnDocumentFavorites.TnDocumentFavoritesId(userSeq, groupSeq));
  }
}
