package com.style24.core.biz.service;

import java.io.File;
import java.io.IOException;
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.TscPopupDao;
import com.style24.persistence.domain.Popup;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 팝업 Service
 *
 * @author bin2107
 * @since 2020. 03. 03
 */
@Service
@Slf4j
public class TscPopupService {
    @Autowired
    private TscPopupDao popupDao;

    @Autowired
    private Environment env;

    /**
     * 팝업 리스트 카운트
     * @return
     * @author bin2107
     * @since 2021. 3. 3
     */
    public int getPopupListCount(Popup popup) {
        return popupDao.getPopupListCount(popup);
    }

    /**
     * 팝업 리스트
     * @return
     * @author bin2107
     * @since 2021. 3. 3
     */
    public Collection<Popup> getPopupList(Popup popup){
        return popupDao.getPopupList(popup);
    }

    /**
     * 팝업관리 저장
     * @return
     * @author bin2107
     * @since 2021. 3. 3
     */
    @Transactional("shopTxnManager")
    public void savePopup(Popup popup) {
        if(!StringUtils.isEmpty(popup.getNewSysFileNm())){
            String displayUploadPath = env.getProperty("upload.default.target.path");
            displayUploadPath = GagaFileUtil.getConcatenationPath(displayUploadPath, "popup");
            File file = new File(GagaFileUtil.getConcatenationPath(displayUploadPath, popup.getNewSysFileNm()));

            String yearMonth = GagaDateUtil.getToday().substring(0, 6);
            displayUploadPath = GagaFileUtil.getConcatenationPath(displayUploadPath, yearMonth); 
            log.info("displayUploadPath {}", displayUploadPath);
            
            File popupPath = new File(displayUploadPath);
			if (!popupPath.exists()) {
				popupPath.mkdir();
			}
			
            String newFilename = "POPUP_" + GagaDateUtil.getTodayDateTime() + "." + StringUtils.getFilenameExtension(popup.getNewSysFileNm());
            File uniqueFile = GagaFileUtil.getUniqueFile(new File(GagaFileUtil.getConcatenationPath(displayUploadPath, newFilename)));
            

            // 기존이미지 삭제
            try{
                GagaFileUtil.deleteFile(GagaFileUtil.getConcatenationPath(displayUploadPath, popup.getOrgFileNm()));
            }catch (IOException e){
                //  nothing
                log.info("[savePopup POPUP 기존 이미지 삭제중 error]");
                //e.printStackTrace();
            }
            
           
           
            // Rename a file
            file.renameTo(uniqueFile);

            popup.setOrgFileNm("/popup/"+ yearMonth +'/'+ newFilename);
            popup.setSysFileNm("/popup/"+ yearMonth +'/'+ newFilename);
        }
        popupDao.savePopup(popup);
        popupDao.deletePopupFront(popup);
    }

    /**
     * 팝업관리 삭제
     * @return
     * @author bin2107
     * @since 2021. 3. 4
     */
    @Transactional("shopTxnManager")
    public void deletePopup(Popup popup){
        for(String popupSq : popup.getSelectDataArr()){
            popup.setPopupSq(Integer.parseInt(popupSq));
            popupDao.deletePopup(popup);
            popupDao.deletePopupFront(popup);
        }
    }

    /**
     * 팝업관리 프론트 적용
     * @return
     * @author bin2107
     * @since 2021. 3. 4
     */
    @Transactional("shopTxnManager")
    public void savePopupFront(Popup popup) {
        for(String popupSq : popup.getSelectDataArr()){
            popup.setPopupSq(Integer.parseInt(popupSq));
            popupDao.savePopupFront(popup);
        }
    }
}
