package com.dbnt.faisp.fipTarget.service;



import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.config.FileInfo;
import com.dbnt.faisp.equip.model.Equip;
import com.dbnt.faisp.equip.model.Equip.EquipId;
import com.dbnt.faisp.fipTarget.mapper.FipTargetMapper;
import com.dbnt.faisp.fipTarget.model.PartInfo;
import com.dbnt.faisp.fipTarget.model.PartInfo.PartInfoId;
import com.dbnt.faisp.fipTarget.model.PartInfoFile;
import com.dbnt.faisp.fipTarget.model.PartInfoFile.PartInfoFileId;
import com.dbnt.faisp.fipTarget.model.PartWork;
import com.dbnt.faisp.fipTarget.model.PartWork.PartWorkId;
import com.dbnt.faisp.fipTarget.model.PartWorkFile;
import com.dbnt.faisp.fipTarget.model.PartWorkFile.PartWorkFileId;
import com.dbnt.faisp.fipTarget.repository.PartInfoFileRepository;
import com.dbnt.faisp.fipTarget.repository.PartInfoRepository;
import com.dbnt.faisp.fipTarget.repository.PartWorkFileRepository;
import com.dbnt.faisp.fipTarget.repository.PartWorkRepository;
import com.dbnt.faisp.publicBoard.model.PublicFile;
import com.dbnt.faisp.util.ParamMap;

import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.*;

@Service
@RequiredArgsConstructor
public class FipTargetService extends BaseService {
	@Value("${spring.servlet.multipart.location}")
	  protected String locationPath;

	private final PartInfoRepository partInfoRepository;
	private final PartInfoFileRepository partInfoFileRepository;
	private final PartWorkRepository partWorkRepository;
	private final PartWorkFileRepository partWorkFileRepository;
	private final FipTargetMapper fipTargetMapper;
	
	public List<ParamMap> selectPartInfoManagerList(ParamMap param) {
		return fipTargetMapper.selectPartInfoManagerList(param);
	}
	
	@Transactional
	public void savePartInfo(PartInfo partInfo) {
		PartInfo dbPart = partInfoRepository.findFirstByOrderByPiSeqDesc();
			if(dbPart == null) {
				partInfo.setPiSeq(1);
				partInfo.setVersionNo(1);
				partInfoRepository.save(partInfo);
			} else {
				partInfo.setPiSeq(dbPart.getPiSeq()+1);
				partInfo.setVersionNo(1);
				partInfoRepository.save(partInfo);
			}
		saveUploadFiles(partInfo.getPiSeq(),partInfo.getVersionNo(), partInfo.getMultipartFileList());
	}
	
	private void saveUploadFiles(Integer piSeq,Integer versionNo, List<MultipartFile> multipartFileList) {
		PartInfoFile lastFileInfo = partInfoFileRepository.findTopByPiSeqAndVersionNoOrderByFileSeqDesc(piSeq,versionNo);
	    int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1);
		    if(multipartFileList == null || multipartFileList.isEmpty()) {
		    	if(lastFileInfo == null || lastFileInfo.getOrigNm() == null ) {
		    		PartInfoFile fileInfo = new PartInfoFile();
		    		fileInfo.setFileSeq(fileSeq);
		    		fileInfo.setPiSeq(piSeq);	 
		    		fileInfo.setVersionNo(versionNo);
		    		partInfoFileRepository.save(fileInfo);
		    	}
			      
		    } else {
		    	for(MultipartFile file : multipartFileList){
				      String saveName = UUID.randomUUID().toString();
				      String path = locationPath+File.separator+"publicFile"+File.separator;
				      saveFile(file, new File(path+File.separator+saveName));
			
				      String originalFilename = file.getOriginalFilename();
				      int extnIdx = originalFilename.lastIndexOf(".");
				      PartInfoFile fileInfo = new PartInfoFile();
				      fileInfo.setFileSeq(fileSeq++);
				      fileInfo.setPiSeq(piSeq);	 
				      fileInfo.setVersionNo(versionNo);
				      fileInfo.setOrigNm(originalFilename.substring(0, extnIdx));
				      fileInfo.setFileExtn(originalFilename.substring(extnIdx+1));
				      fileInfo.setConvNm(saveName);
				      fileInfo.setFileSize(calculationSize(file.getSize()));
				      fileInfo.setFilePath(path);
				      partInfoFileRepository.save(fileInfo);
				    }
		    }
		
	  }

	public List<PartInfo> selectPartInfoList(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoList(partInfo);
	}

	public Integer selectPartInfoListCnt(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoListCnt(partInfo);
	}

	public ParamMap selectWorkTypeTotal(PartInfo partInfo) {
		return fipTargetMapper.selectWorkTypeTotal(partInfo);
	}

	public PartInfo selectPartInfo(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfo(partInfo);
	}

	public List<PartInfoFile> selectPartInfoFile(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoFile(partInfo);
	}
	
	@Transactional
	public void updatePartInfo(PartInfo partInfo, List<Integer> deleteFileSeq) {
		PartInfo dbPart = partInfoRepository.findById(new PartInfoId(partInfo.getPiSeq(), partInfo.getVersionNo())).orElse(null);
		PartInfo partTmp = new PartInfo();
		partTmp.setPiSeq(dbPart.getPiSeq());
		partTmp.setVersionNo(dbPart.getVersionNo()+1);
		partTmp.setMgtOrgan(partInfo.getMgtOrgan());
		partTmp.setLandPolice(partInfo.getLandPolice());
		partTmp.setPiUserSeq(partInfo.getPiUserSeq());
		partTmp.setRentType(partInfo.getRentType());
		partTmp.setRentPrice(partInfo.getRentPrice());
		partTmp.setUtilityType(partInfo.getUtilityType());
		partTmp.setUtilityPrice(partInfo.getUtilityPrice());
		partTmp.setTerminalNm(partInfo.getTerminalNm());
		partTmp.setMpWorkType(partInfo.getMpWorkType());
		partTmp.setMpPeopleCnt(partInfo.getMpPeopleCnt());
		partTmp.setMpDescription(partInfo.getMpDescription());
		partTmp.setPlWorkType(partInfo.getPlWorkType());
		partTmp.setPlPeopleCnt(partInfo.getPlPeopleCnt());
		partTmp.setPlDescription(partInfo.getPlDescription());
		partTmp.setWrtUserSeq(partInfo.getWrtUserSeq());
		partTmp.setWrtNm(partInfo.getWrtNm());
		partTmp.setWrtPart(partInfo.getWrtPart());
		partTmp.setWrtOrgan(partInfo.getWrtOrgan());
		partTmp.setWrtDt(partInfo.getWrtDt());
		partInfoRepository.save(partTmp);
		//업데이트하는 버전 파일조회
		List<PartInfoFile> fileList = partInfoFileRepository.findByPiSeqAndVersionNoOrderByFileSeqAsc(partInfo.getPiSeq(),partInfo.getVersionNo());
		if(fileList.get(0).getOrigNm() != null) {
			for(PartInfoFile fileInfo : fileList){
				PartInfoFile tmpFile = new PartInfoFile();
				tmpFile.setFileSeq(fileInfo.getFileSeq());
				tmpFile.setPiSeq(fileInfo.getPiSeq());
				tmpFile.setVersionNo(partTmp.getVersionNo());
				tmpFile.setOrigNm(fileInfo.getOrigNm());
				tmpFile.setConvNm(fileInfo.getConvNm());
				tmpFile.setFileExtn(fileInfo.getFileExtn());
				tmpFile.setFileSize(fileInfo.getFileSize());
				tmpFile.setFilePath(fileInfo.getFilePath());
				partInfoFileRepository.save(tmpFile);
			}
		}
		//수정시 삭제한 파일삭제
		if(deleteFileSeq!=null && deleteFileSeq.size()>0){
		      deletePartInfoFile(partTmp.getPiSeq(),partTmp.getVersionNo(), deleteFileSeq);
		}
		//파일업로드
			saveUploadFiles(partTmp.getPiSeq(),partTmp.getVersionNo(), partInfo.getMultipartFileList());
	}
	
	private void deletePartInfoFile(Integer piSeq,Integer versionNo, List<Integer> deleteFileSeq) {
	    List<PartInfoFile> partInfoFileList = partInfoFileRepository.findByPiSeqAndVersionNoOrderByFileSeqAsc(piSeq,versionNo);
	    for(PartInfoFile file: partInfoFileList){
	      if(deleteFileSeq.contains(file.getFileSeq())){
	        deleteStoredFile(new File(file.getFilePath(), file.getConvNm()));
	        partInfoFileRepository.deleteById(new PartInfoFileId(file.getFileSeq(),piSeq,versionNo));
	      }
	    }
	  }

	public List<PartInfo> selectPartInfoSeq(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoSeq(partInfo);
	}

	public PartInfo selectPartInfoHistoryView(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoHistoryView(partInfo);
	}

	public List<PartInfoFile> selectPartInfoFileHistoryView(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoFileHistoryView(partInfo);
	}
	
	@Transactional
	public void deletePartInfo(PartInfo partInfo) {
		//파일삭제
		List<PartInfoFile> partInfoFileList= selectPartInfoFile(partInfo);
		for(PartInfoFile file: partInfoFileList){
		      if(file.getOrigNm() != null){
		        deleteStoredFile(new File(file.getFilePath(), file.getConvNm()));
		      }
		}
		partInfoFileRepository.deleteByPiSeq(partInfo.getPiSeq());
		partInfoRepository.deleteByPiSeq(partInfo.getPiSeq());
		
	}

	public PartInfoFile selectPartInfoFileDown(Integer fileSeq, Integer piSeq, Integer versionNo) {
		return partInfoFileRepository.findById(new PartInfoFileId(fileSeq, piSeq,versionNo)).orElse(null);
	}

	public String selectPartInfoFirstId(PartInfo partInfo) {
		return fipTargetMapper.selectPartInfoFirstId(partInfo.getPiSeq());
	}

	public List<ParamMap> selectTerminalName(PartWork partWork) {
		return fipTargetMapper.selectTerminalName(partWork);
	}
	
	@Transactional
	public void savePartWork(PartWork partWork) {
		PartWork dbPart = partWorkRepository.findTopByPiSeqOrderByPwSeqDesc(partWork.getPiSeq());
		if(dbPart == null) {
			partWork.setPwSeq(1);
			partWorkRepository.save(partWork);
		} else {
			partWork.setPwSeq(dbPart.getPwSeq()+1);
			partWorkRepository.save(partWork);
		}
		if(partWork.getMultipartFileList()!= null){
			saveFilesUpload(partWork.getPwSeq(),partWork.getPiSeq(), partWork.getMultipartFileList());			
		}
	}
	
	private void saveFilesUpload(Integer pwSeq,Integer piSeq, List<MultipartFile> multipartFileList) {
		PartWorkFile lastFileInfo = partWorkFileRepository.findTopByPwSeqAndPiSeqOrderByFileSeqDesc(pwSeq,piSeq);
	    int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1);
	    for(MultipartFile file : multipartFileList){
	        String saveName = UUID.randomUUID().toString();
	        String path = locationPath+File.separator+"publicFile"+File.separator;
	        saveFile(file, new File(path+File.separator+saveName));

	        String originalFilename = file.getOriginalFilename();
	        int extnIdx = originalFilename.lastIndexOf(".");
	        PartWorkFile fileInfo = new PartWorkFile();	        
	        fileInfo.setFileSeq(fileSeq++);
	        fileInfo.setPwSeq(pwSeq);
	        fileInfo.setPiSeq(piSeq);
	        fileInfo.setOrigNm(originalFilename.substring(0, extnIdx));
	        fileInfo.setFileExtn(originalFilename.substring(extnIdx+1));
	        fileInfo.setConvNm(saveName);
	        fileInfo.setFileSize(calculationSize(file.getSize()));
	        fileInfo.setFilePath(path);
	        partWorkFileRepository.save(fileInfo);
	      }
		
	  }

	public List<PartWork> selectPartWorkList(PartWork partWork) {
		return fipTargetMapper.selectPartWorkList(partWork);
	}

	public Integer selectPartWorkListCnt(PartWork partWork) {
		return fipTargetMapper.selectPartWorkListCnt(partWork);
	}

	public PartWork selectPartWorkInfo(PartWork partWork) {
		return fipTargetMapper.selectPartWorkInfo(partWork);
	}

	public List<PartWorkFile> selectPartWorkFile(PartWork partWork) {
		return partWorkFileRepository.findByPwSeqAndPiSeqOrderByFileSeqAsc(partWork.getPwSeq(),partWork.getPiSeq());
	}
	
	@Transactional
	public void updatePartWork(PartWork partWork, List<Integer> deleteFileSeq) {
		partWorkRepository.save(partWork);
		if(deleteFileSeq!=null && deleteFileSeq.size()>0){
		      deletePartWorkFile(partWork, deleteFileSeq);
		}
		if(partWork.getMultipartFileList()!= null){
			saveFilesUpload(partWork.getPwSeq(),partWork.getPiSeq(), partWork.getMultipartFileList());			
		}		
	}
	
    private void deletePartWorkFile(PartWork partWork, List<Integer> deleteFileSeq) {
	    List<PartWorkFile> partWorkFileList = partWorkFileRepository.findByPwSeqAndPiSeq(partWork.getPwSeq(),partWork.getPiSeq());
	    for(PartWorkFile file: partWorkFileList ){
	      if(deleteFileSeq.contains(file.getFileSeq())){
	        deleteStoredFile(new File(file.getFilePath(), file.getConvNm()));
	        partWorkFileRepository.delete(file);
	      }
	    }
	}
		
	
	
	
	
	
}
