package com.style24.core.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 org.springframework.util.StringUtils;

import com.style24.core.biz.dao.TscLookbookDao;
import com.style24.persistence.domain.Lookbook;
import com.style24.persistence.domain.LookbookBanner;
import com.style24.persistence.domain.LookbookGoods;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 룩북 Service
 *
 * @author bin2107
 * @since 2020. 03. 05
 */
@Service
@Slf4j
public class TscLookbookService {
	@Autowired
	private TscLookbookDao lookbookDao;

	@Autowired
	private Environment env;

	/**
	 * 팝업관리 저장
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 3
	 */
	@Transactional("shopTxnManager")
	public void saveLookbook(Lookbook lookbook) {
		if("U".equals(lookbook.getMode())){
			lookbookDao.deleteLookbookBanner(lookbook.getLookbookSq());
		}

		if (!StringUtils.isEmpty(lookbook.getNewSysTnfileNm())) {
			String bannerUploadPath = env.getProperty("upload.default.target.path");
			String yearMonth = GagaDateUtil.getToday().substring(0, 6);
			bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, "/lookbook/thumbnail");
			File newFile = new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, lookbook.getNewSysTnfileNm()));
			bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, yearMonth);
			String newFilename = "LOOKBOOK_" + GagaDateUtil.getTodayDateTime() + "." + StringUtils.getFilenameExtension(lookbook.getNewSysTnfileNm());
			File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, newFilename)));
//          Rename a file
			File path = new File(bannerUploadPath);
			if (!path.exists()) {
				path.mkdir();
			}
			newFile.renameTo(uniqueFile);

			lookbook.setOrgTnfileNm("/lookbook/thumbnail/" + yearMonth + "/" + newFilename);
			lookbook.setSysTnfileNm("/lookbook/thumbnail/" + yearMonth + "/" + newFilename);
		}else{
			lookbook.setOrgTnfileNm(lookbook.getOrgTnfileNm());
			lookbook.setSysTnfileNm(lookbook.getOrgTnfileNm());
		}

		lookbookDao.saveLookbook(lookbook);

		int ind = 1;
		for(LookbookBanner bannerInfo : lookbook.getBannerList()){
			if(bannerInfo.getSysFileNm() != null && !"".equals(bannerInfo.getSysFileNm())){
				String imgInd = String.valueOf(ind);
				if (ind < 10) {
					imgInd = "0" + imgInd;
				}
				String bannerUploadPath = env.getProperty("upload.default.target.path");
				String yearMonth = GagaDateUtil.getToday().substring(0, 6);
				bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, "/lookbook/banner");
				File newFile = new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, bannerInfo.getSysFileNm()));
				bannerUploadPath = GagaFileUtil.getConcatenationPath(bannerUploadPath, yearMonth);
				String newFilename = "BANNER_" + GagaDateUtil.getTodayDateTime() + "_" + imgInd + "." + StringUtils.getFilenameExtension(bannerInfo.getSysFileNm());
				File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(bannerUploadPath, newFilename)));
				// Rename a file
				File path = new File(bannerUploadPath);
				if (!path.exists()) {
					path.mkdir();
				}
				newFile.renameTo(uniqueFile);
				bannerInfo.setOrgFileNm("/lookbook/banner/" + yearMonth + "/" + newFilename);
				bannerInfo.setSysFileNm("/lookbook/banner/" + yearMonth + "/" + newFilename);
			}else{
				bannerInfo.setOrgFileNm(bannerInfo.getOrgFileNm());
				bannerInfo.setSysFileNm(bannerInfo.getOrgFileNm());
			}
			ind++;
			bannerInfo.setLookbookSq(lookbook.getLookbookSq());
			bannerInfo.setRegNo(lookbook.getRegNo());
			bannerInfo.setUpdNo(lookbook.getUpdNo());
			lookbookDao.saveLookbookBanner(bannerInfo);
		}
	}

	/**
	 * 룩북 리스트 카운트
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 5
	 */
	public int getLookbookListCount(Lookbook lookbook) {
		return lookbookDao.getLookbookListCount(lookbook);
	}

	/**
	 * 룩북 리스트
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 5
	 */
	public Collection<Lookbook> getLookbookList(Lookbook lookbook) {
		return lookbookDao.getLookbookList(lookbook);
	}

	/**
	 * 룩북 리스트 (브랜드)
	 * @return
	 * @author bin2107
	 * @since 2021. 5. 15
	 */
	public Collection<Lookbook> lookbookBrandList(Lookbook lookbook) {
		return lookbookDao.lookbookBrandList(lookbook);
	}

	/**
	 * 룩북 삭제
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 5
	 */
	@Transactional("shopTxnManager")
	public void deleteLookbook(Lookbook lookbook) {
		for (String lookbookSq : lookbook.getSelectDataArr()) {
			lookbook.setLookbookSq(Integer.parseInt(lookbookSq));
			lookbookDao.deleteLookbook(lookbook);
		}
	}

	/**
	 * 룩북 마스터정보 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 8
	 */
	public Lookbook getLookbookMstInfo(Integer lookbookSq){
		return lookbookDao.getLookbookMstInfo(lookbookSq);
	}

	/**
	 * 룩북 배너 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 8
	 */
	public Collection<LookbookBanner> getLookbookBannerDetailList(Integer lookbookSq){
		return lookbookDao.getLookbookBannerDetailList(lookbookSq);
	}

	/**
	 * 룩북 배너 피커 등록/수정
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 9
	 */
	@Transactional("shopTxnManager")
	public void saveLookbookPicker(LookbookGoods lookbookGoods) {
		if("U".equals(lookbookGoods.getMode())){
			lookbookDao.deleteLookbookPicker(lookbookGoods.getLookbookbSq());
		}

		for(LookbookGoods pickerInfo : lookbookGoods.getPickerList()){
			pickerInfo.setLookbookbSq(lookbookGoods.getLookbookbSq());
			pickerInfo.setDelYn(lookbookGoods.getDelYn());
			pickerInfo.setRegNo(lookbookGoods.getRegNo());
			pickerInfo.setUpdNo(lookbookGoods.getUpdNo());
			lookbookDao.saveLookbookPicker(pickerInfo);
		}
	}

	/**
	 * 룩북 메인룩북 등록
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 15
	 */
	@Transactional("shopTxnManager")
	public void saveMainLookbook(Lookbook lookbook) {
		lookbookDao.deleteLookbookMainDispYn(lookbook);
		lookbookDao.updateLookbookMainDispYn(lookbook);
	}


	/**
	 * 룩북 배너 피커 조회
	 * @return
	 * @author bin2107
	 * @since 2021. 3. 9
	 */
	public Collection<LookbookGoods> getLookbookBannerPickerList(Integer lookbookbSq){
		return lookbookDao.getLookbookBannerPickerList(lookbookbSq);
	}

	/**
	 * front 영역
	 */

	/**
	 * 룩북
	 *
	 * @param lookbook
	 * @return
	 * @author eskim
	 * @since 2021. 2. 18
	 */
	public Collection<Lookbook> getLookbookListForGoods(Lookbook lookbook) {
		Collection<Lookbook> lookbookBannerList = lookbookDao.getLookbookBannerList(lookbook);

		for (Lookbook tempLookbook : lookbookBannerList) {
			tempLookbook.setCustNo(lookbook.getCustNo());
			tempLookbook.setLookbookGoodsList(lookbookDao.getLookbookBannerGoodsList(tempLookbook));
		}

		return lookbookBannerList;
	}

	/**
	 * 다른 룩북 조회
	 * @param lookbook
	 * @return
	 * @author bin2107
	 * @since 2021. 4. 6
	 */
	public Collection<Lookbook> getOtherLookbookList(Lookbook lookbook){
		return lookbookDao.getOtherLookbookList(lookbook);
	}
}
