package com.dbnt.faisp.fpiMgt.affairPlan.service;


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.config.FileInfo;
import com.dbnt.faisp.fpiMgt.affairPlan.mapper.PlanMapper;
import com.dbnt.faisp.fpiMgt.affairPlan.model.PlanApprv;
import com.dbnt.faisp.fpiMgt.affairPlan.model.PlanBoard;
import com.dbnt.faisp.fpiMgt.affairPlan.model.PlanFile;
import com.dbnt.faisp.fpiMgt.affairPlan.model.PlanMainInfo;
import com.dbnt.faisp.fpiMgt.affairPlan.repository.PlanApprvRepository;
import com.dbnt.faisp.fpiMgt.affairPlan.repository.PlanBoardRepository;
import com.dbnt.faisp.fpiMgt.affairPlan.repository.PlanFileRepository;
import com.dbnt.faisp.fpiMgt.affairPlan.repository.PlanMainInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class PlanService extends BaseService { // 월간계획
  private final PlanBoardRepository planBoardRepository;
  private final PlanFileRepository planFileRepository;
  private final PlanMainInfoRepository planMainInfoRepository;
  private final PlanApprvRepository planApprvRepository;
  private final PlanMapper planMapper;

  public List<PlanBoard> selectPlanBoardList(PlanBoard planBoard) {
    return planMapper.selectPlanBoardList(planBoard);
  }

  public Integer selectPlanBoardListCnt(PlanBoard planBoard) {
    return planMapper.selectPlanBoardListCnt(planBoard);
  }

  public PlanBoard selectPlanBoard(Integer planKey) {
    PlanBoard savedPlan = planBoardRepository.findById(planKey).orElse(null);
    if (savedPlan != null) {
      savedPlan.setFileList(planFileRepository.findByPlanKey(planKey));
      savedPlan.setMainInfoList(planMainInfoRepository.findByPlanKey(planKey));
      savedPlan.setApprvList(planApprvRepository.findByPlanKey(planKey));
    }
    return savedPlan;
  }

  @Transactional
  public Integer savePlanBoard(PlanBoard planBoard, List<String> planInfos, List<String> detailPlanInfos, List<Integer> deleteFileSeq) {
    Integer planKey = planBoardRepository.save(planBoard).getPlanKey();
    Integer infoSeq = savePlanMainInfos(planKey,0, "S", planInfos);//요약 summery
    savePlanMainInfos(planKey, infoSeq, "D", detailPlanInfos);//상세 detail
    if(deleteFileSeq != null && deleteFileSeq.size()>0){
      deletePlanFile(planKey, deleteFileSeq);
    }
    if(planBoard.getMultipartFileList()!=null){
      saveUploadFiles(planKey, planBoard.getMultipartFileList());
    }
    return planKey;
  }

  private Integer savePlanMainInfos(Integer planKey, Integer planSeq, String infoType, List<String> infoList){
    for(String info: infoList){
      PlanMainInfo planMainInfo = new PlanMainInfo();
      planMainInfo.setPlanKey(planKey);
      planMainInfo.setPlanSeq(++planSeq);
      planMainInfo.setPlanType(infoType);
      planMainInfo.setPlanInfo(info);
      planMainInfoRepository.save(planMainInfo);
    }
    return planSeq;
  }

  private void saveUploadFiles(Integer planKey, List<MultipartFile> multipartFileList){
    PlanFile lastFileInfo = planFileRepository.findTopByPlanKeyOrderByFileSeqDesc(planKey).orElse(null);
    int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1);
    for(MultipartFile file : multipartFileList){
      String saveName = UUID.randomUUID().toString();
      String path = locationPath+File.separator+"monthPlan"+File.separator;
      saveFile(file, new File(path+File.separator+saveName));

      String originalFilename = file.getOriginalFilename();
      int extnIdx = originalFilename.lastIndexOf(".");
      PlanFile fileInfo = new PlanFile();
      fileInfo.setPlanKey(planKey);
      fileInfo.setFileSeq(fileSeq++);
      fileInfo.setOrigNm(originalFilename.substring(0, extnIdx));
      fileInfo.setFileExtn(originalFilename.substring(extnIdx+1));
      fileInfo.setConvNm(saveName);
      fileInfo.setFileSize(calculationSize(file.getSize()));
      fileInfo.setSavePath(path);
      planFileRepository.save(fileInfo);
    }
  }

  private void deletePlanFile(Integer planKey, List<Integer> deleteFileSeq) {
    List<PlanFile> planFileList = planFileRepository.findByPlanKey(planKey);
    for(PlanFile file: planFileList){
      if(deleteFileSeq.contains(file.getFileSeq())){
        deleteStoredFile(new File(file.getSavePath(), file.getConvNm()));
        planFileRepository.delete(file);
      }
    }
  }

  public FileInfo selectPlanFile(Integer planKey, Integer fileSeq) {
    return planFileRepository.findById(new PlanFile.PlanFileId(planKey, fileSeq)).orElse(null);
  }

  @Transactional
  public Integer planStateChange(PlanApprv apprv) {
    PlanBoard savedPlan = planBoardRepository.findById(apprv.getPlanKey()).orElse(null);
    savedPlan.setPlanState(apprv.getState());
    PlanApprv lastApprv = planApprvRepository.findTopByPlanKeyOrderByApprvSeqDesc(apprv.getPlanKey()).orElse(null);
    apprv.setApprvSeq(lastApprv==null?1:lastApprv.getApprvSeq()+1);
    planApprvRepository.save(apprv);
    return apprv.getApprvSeq();
  }
}
