package com.dbnt.kcgfilemanager.service;

import com.dbnt.kcgfilemanager.mapper.ModifyRequestMapper;
import com.dbnt.kcgfilemanager.model.ModifyRequest;
import com.dbnt.kcgfilemanager.model.UserInfo;
import com.dbnt.kcgfilemanager.repository.ModifyRequestRepository;
import com.dbnt.kcgfilemanager.repository.UserInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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


@Service
@RequiredArgsConstructor
public class ModifyRequestService {

  private final ModifyRequestRepository modifyRequestRepository;
  private final ModifyRequestMapper modifyRequestMapper;
  private final UserInfoRepository userInfoRepository;
  private final CategoryRoleService categoryRoleService;

  public Integer saveModifyRequest(ModifyRequest modifyRequest) {
    return modifyRequestRepository.save(modifyRequest).getRequestSeq();
  }

  public Integer updateModifyRequest(ModifyRequest modifyRequest) {
    ModifyRequest dbData = selectModifyRequestByRequestSeq(modifyRequest.getRequestSeq());

    if(modifyRequest.getStatus()!=null && !modifyRequest.getStatus().equals("")){
      dbData.setStatus(modifyRequest.getStatus());
    }
    if(modifyRequest.getRequestType()!=null && !modifyRequest.getRequestType().equals("")){
      dbData.setRequestType(modifyRequest.getRequestType());
    }
    if(modifyRequest.getDescription()!=null && !modifyRequest.getDescription().equals("")){
      dbData.setDescription(modifyRequest.getDescription());
    }
    if(modifyRequest.getRequestName()!=null && !modifyRequest.getRequestName().equals("")){
      dbData.setRequestName(modifyRequest.getRequestName());
    }
    if(modifyRequest.getRequestDepartment()!=null && !modifyRequest.getRequestDepartment().equals(0)){
      dbData.setRequestDepartment(modifyRequest.getRequestDepartment());
    }
    if(modifyRequest.getRequestPosition()!=null && !modifyRequest.getRequestPosition().equals(0)){
      dbData.setRequestPosition(modifyRequest.getRequestPosition());
    }
    if(modifyRequest.getRequestCategoryAry()!=null && !modifyRequest.getRequestCategoryAry().equals("")){
      dbData.setRequestCategoryAry(modifyRequest.getRequestCategoryAry());
    }

    return modifyRequestRepository.save(dbData).getRequestSeq();
  }
  public Integer deleteModifyRequest(ModifyRequest modifyRequest) {
    modifyRequest = selectModifyRequestByRequestSeq(modifyRequest.getRequestSeq());
    modifyRequest.setTitle("삭제된 요청.");
    modifyRequest.setStatus("delete");
    modifyRequest.setDescription(null);
    modifyRequest.setRequestName(null);
    modifyRequest.setRequestDepartment(null);
    modifyRequest.setRequestPosition(null);
    modifyRequest.setRequestCategoryAry(null);
    return modifyRequestRepository.save(modifyRequest).getRequestSeq();
  }

  public List<ModifyRequest> selectModifyRequestList(ModifyRequest modifyRequest) {
    return modifyRequestMapper.selectModifyRequestList(modifyRequest);
  }
  public Integer selectModifyRequestListCnt(ModifyRequest modifyRequest) {
    return modifyRequestMapper.selectModifyRequestListCnt(modifyRequest);
  }

  public ModifyRequest selectModifyRequestByRequestSeq(Integer requestSeq) {
    return modifyRequestRepository.findById(requestSeq).orElse(null);
  }

  public Integer modifyRequestStateChange(ModifyRequest modifyRequest) {
    switch (modifyRequest.getStatus()){
      case "approval":
        ModifyRequest request = modifyRequestRepository.findById(modifyRequest.getRequestSeq()).orElse(null);
        UserInfo userInfo = userInfoRepository.findByUserId(request.getCreateId()).orElse(null);
        switch (request.getRequestType()) {
          case "userInfo":
            userInfo.setName(request.getRequestName());
            userInfo.setPosition(request.getRequestPosition());
            userInfo.setDepartment(request.getRequestDepartment());
            userInfoRepository.save(userInfo);
            break;
          case "categoryRole":
            String[] categoryAry = request.getRequestCategoryAry().split(",");
            List<Integer> requestCategoryList = new ArrayList<>();
            for(String categorySeq : categoryAry){
              requestCategoryList.add(Integer.parseInt(categorySeq.replaceAll(" ", "")));
            }
            categoryRoleService.modifyCategoryRole(userInfo.getUserSeq(), requestCategoryList);
            break;
        }
        return updateModifyRequest(modifyRequest);
      case "delete":
        return deleteModifyRequest(modifyRequest);
      default:
        return updateModifyRequest(modifyRequest);
    }
  }
}
