package com.style24.front.biz.service;

import java.io.File;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.style24.core.support.env.TscConstants;
import com.style24.front.biz.dao.TsfCounselDao;
import com.style24.front.support.security.session.TsfSession;
import com.style24.persistence.domain.Counsel;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaDateUtil;
import com.gagaframework.web.util.GagaFileUtil;

/**
 * 상담(1:1문의) Service
 *
 * @author gagamel
 * @since 2020. 12. 28
 */
@Service
@Slf4j
public class TsfCounselService {

	@Autowired
	private TsfCounselDao counselDao;

	@Autowired
	private Environment env;

	/**
	 * 1:1문의 생성
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@Transactional("shopTxnManager")
	public void createOneToOneQna(Counsel counsel) {
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setCellPhnno(TsfSession.getInfo().getCellPhnno());
		counsel.setEmail(TsfSession.getInfo().getEmail());
		counsel.setRegNo(TsfSession.getInfo().getCustNo());
		counsel.setUpdNo(TsfSession.getInfo().getCustNo());
		counselDao.createOneToOneQna(counsel);

		if (counsel.getOrgFileNmArr() != null) {
			int ind = 1;
			String[] orgItem = counsel.getOrgFileNmArr();
			String[] sysItem = counsel.getSysFileNmArr();

			for (int i = 0; i < orgItem.length; i++) {
				if (orgItem[i] != "") {
					String imgInd = String.valueOf(ind);
					if (ind < 10) {
						imgInd = "0" + imgInd;
					}
					String planUploadPath = env.getProperty("upload.default.target.path");
					String yearMonth = GagaDateUtil.getToday().substring(0, 6);
					planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, "/counsel");
					File newFile = new File(GagaFileUtil.getConcatenationPath(planUploadPath, sysItem[i]));
					planUploadPath = GagaFileUtil.getConcatenationPath(planUploadPath, yearMonth);
					String newFilename = "COUNSEL_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + org.springframework.util.StringUtils.getFilenameExtension(sysItem[i]);
					File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(planUploadPath, newFilename)));
					// Rename a file
					File path = new File(planUploadPath);
					if (!path.exists()) {
						path.mkdir();
					}
					newFile.renameTo(uniqueFile);

					if (i == 0) {
						counsel.setOrgFileNm1(orgItem[i]);
						counsel.setSysFileNm1("/counsel/" + yearMonth + "/" + newFilename);
					} else {
						counsel.setOrgFileNm2(orgItem[i]);
						counsel.setSysFileNm2("/counsel/" + yearMonth + "/" + newFilename);
					}
					ind++;
				}
			}
			counselDao.updateOneToOneQna(counsel);
		}
	}

	/**
	 * 1:1문의(총,답변완료,처리중) 건수
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	public Counsel getOneToOneQnaCountInfo() {
		Counsel counsel = new Counsel();
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());
		return counselDao.getOneToOneQnaCountInfo(counsel);
	}

	/**
	 * 1:1문의 총건수
	 * @param counsel - 상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	public int getOneToOneQnaTotalCount(Counsel counsel) {
		return counselDao.getOneToOneQnaTotalCount(counsel);
	}

	/**
	 * 1:1문의 목록
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	public Collection<Counsel> getOneToOneQnaList(Counsel counsel) {
		return counselDao.getOneToOneQnaList(counsel);
	}

	/**
	 * 1:1문의 삭제
	 * 		진행중 상태일 때 삭제하는 것이 아니라 답변완료 상태일 때만 삭제하는 것으로 함 (기획서)
	 * @param counsel - 상담정보
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@Transactional("shopTxnManager")
	public void deleteOneToOneQna(Counsel counsel) {
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setUpdNo(TsfSession.getInfo().getCustNo());
		int result = counselDao.deleteOneToOneQna(counsel);
		if (result == 0) {
//			throw new IllegalStateException("답변이 완료되어 삭제할 수 없습니다.");
			throw new IllegalStateException("답변이 완료되지 않아 삭제할 수 없습니다.");
		}
	}

	/**
	 * 1:1문의 상세
	 * @param counselSq - 상담일련번호
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	public Counsel getOneToOneQna(Integer counselSq) {
		Counsel counsel = new Counsel();
		counsel.setCounselSq(counselSq);
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());
		return counselDao.getOneToOneQna(counsel);
	}

	/**
	 * 1:1문의 수정
	 * @param counsel - 상담정보
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@Transactional("shopTxnManager")
	public void updateOneToOneQna(Counsel counsel) {
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setUpdNo(TsfSession.getInfo().getCustNo());
		int result = counselDao.updateOneToOneQna(counsel);
		if (result == 0) {
			throw new IllegalStateException("답변이 완료되어 수정할 수 없습니다.");
		}
	}

	/**
	 * 1:1문의 이미지 삭제
	 * @param counsel - 상담정보
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@Transactional("shopTxnManager")
	public void deleteOneToOneQnaImage(Counsel counsel) {
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setUpdNo(TsfSession.getInfo().getCustNo());
		int result = counselDao.deleteOneToOneQnaImage(counsel);
		if (result == 0) {
//			throw new IllegalStateException("답변이 완료되어 삭제할 수 없습니다.");
			throw new IllegalStateException("답변이 완료되지 않아 삭제할 수 없습니다.");
		}
	}

	/**
	 * 1:1문의 사진 리스트
	 * @param counsel -상담정보
	 * @return
	 * @author sowon
	 * @since 2021. 06. 214
	 */
	public Collection<Counsel> getOneToOneImgList(Counsel counsel) {
		return counselDao.getOneToOneImgList(counsel);
	}

	/**
	 * 1:1문의용 답변템플릿 조회
	 * @param counselClsf - 문의유형
	 * @return
	 * @author gagamel
	 * @since 2021. 9. 28
	 */
	public String getOnetooneQnaAnswerTemplate(String counselClsf) {
		return counselDao.getOnetooneQnaAnswerTemplate(counselClsf);
	}

	/**
	 * 상품문의 생성
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@Transactional("shopTxnManager")
	public void createGoodsQna(Counsel counsel) {
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setCellPhnno(TsfSession.getInfo().getCellPhnno());
		counsel.setEmail(TsfSession.getInfo().getEmail());
		counsel.setRegNo(TsfSession.getInfo().getCustNo());
		counsel.setUpdNo(TsfSession.getInfo().getCustNo());
		counselDao.createGoodsQna(counsel);
	}

	/**
	 * 상품문의(총,답변완료,처리중) 건수
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	public Counsel getGoodsQnaCountInfo() {
		Counsel counsel = new Counsel();
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setSiteCd(TscConstants.Site.STYLE24.value());
		return counselDao.getGoodsQnaCountInfo(counsel);
	}

	/**
	 * 상품문의 총건수
	 * @param counsel - 상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	public int getGoodsQnaTotalCount(Counsel counsel) {
		return counselDao.getGoodsQnaTotalCount(counsel);
	}

	/**
	 * 상품문의 목록
	 * @param counsel -상담정보
	 * @return
	 * @author gagamel
	 * @since 2020. 12. 24
	 */
	public Collection<Counsel> getGoodsQnaList(Counsel counsel) {
		return counselDao.getGoodsQnaList(counsel);
	}

	/**
	 * 상품문의 목록 - 상품상세용
	 * @param counsel -상담정보
	 * @return
	 * @author eskim
	 * @since 2021. 03. 05
	 */
	public Collection<Counsel> getGoodsQnaListForGoods(Counsel counsel) {
		return counselDao.getGoodsQnaListForGoods(counsel);
	}

	/**
	 * 상품문의 삭제
	 * @param counsel - 상담정보
	 * @author gagamel
	 * @since 2020. 12. 28
	 */
	@Transactional("shopTxnManager")
	public void deleteGoodsQna(Counsel counsel) {
		counsel.setCustNo(TsfSession.getInfo().getCustNo());
		counsel.setUpdNo(TsfSession.getInfo().getCustNo());
		int result = counselDao.deleteGoodsQna(counsel);
		if (result == 0) {
//			throw new IllegalStateException("답변이 완료되어 삭제할 수 없습니다.");
			throw new IllegalStateException("답변이 완료되지 않아 삭제할 수 없습니다.");
		}
	}

}
