package com.dbnt.kcgfilemanager.service;

import com.dbnt.kcgfilemanager.model.BoardCategory;
import com.dbnt.kcgfilemanager.model.CategoryRole;
import com.dbnt.kcgfilemanager.model.ContentPosition;
import com.dbnt.kcgfilemanager.repository.BoardCategoryRepository;
import com.dbnt.kcgfilemanager.repository.CategoryRoleRepository;
import com.dbnt.kcgfilemanager.repository.ContentPositionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class BoardCategoryService {

  private final BoardCategoryRepository boardCategoryRepository;
  private final CategoryRoleRepository categoryRoleRepository;
  private final ContentPositionRepository contentPositionRepository;


  public List<BoardCategory> selectBoardCategory(Integer parentSeq, Integer depth) {
    return boardCategoryRepository.findByParentSeqAndDepth(parentSeq, depth);
  }
  public List<BoardCategory> selectBoardCategoryAll(Integer parentSeq, Integer depth){
    List<BoardCategory> categoryList = boardCategoryRepository.findByParentSeqAndDepth(parentSeq, depth);
    for(BoardCategory category: categoryList){
      category.setChildCategoryList(selectBoardCategoryAll(category.getCategorySeq(), depth+1));
    }
    return categoryList;
  }
  @Transactional
  public void insertBoardCategory(List<BoardCategory> categoryList){
    boardCategoryRepository.saveAll(categoryList);
    Integer parentSeq = categoryList.get(0).getParentSeq();
    List<CategoryRole> categoryRoleList = categoryRoleRepository.findByCategorySeq(parentSeq);
    List<CategoryRole> addRoleList = new ArrayList<>();
    for(CategoryRole role: categoryRoleList){
      for(BoardCategory category: categoryList){
        CategoryRole categoryRole = new CategoryRole();
        categoryRole.setUserSeq(role.getUserSeq());
        categoryRole.setCategorySeq(category.getCategorySeq());
        categoryRole.setCategoryRole("T");
        addRoleList.add(categoryRole);
      }
    }
    categoryRoleRepository.saveAll(addRoleList);
  }

  public void deleteBoardCategory(Integer categorySeq, Integer depth) {
    deleteChildCategory(selectBoardCategoryAll(categorySeq, depth+1));
    BoardCategory boardCategory = new BoardCategory();
    boardCategory.setCategorySeq(categorySeq);
    boardCategoryRepository.delete(boardCategory);
  }

  private void deleteChildCategory(List<BoardCategory> childList){
    for(BoardCategory category: childList){
      deleteChildCategory(category.getChildCategoryList());
    }
    boardCategoryRepository.deleteAll(childList);
  }

  public String getPageTitle(Integer categorySeq) {
    BoardCategory category = boardCategoryRepository.findById(categorySeq).orElse(null);
    if(category.getParentSeq()==null){
      return category.getCategoryName();
    }
    return getPageTitle(category.getParentSeq())+" > "+category.getCategoryName();
  }

  public String getDepth4PageTitle(Integer categorySeq) {
    ContentPosition contentPosition = contentPositionRepository.findById(categorySeq).orElse(null);
    return contentPosition.getCategoryName();
  }
  public String makeFilePath(Integer categorySeq){
    BoardCategory category = boardCategoryRepository.findById(categorySeq).orElse(null);
    if(category.getParentSeq()==null){
      return "C:\\kcgFileManager\\"+category.getCategoryName();
    }
    return makeFilePath(category.getParentSeq())+"\\"+category.getCategoryName();
  }
}
