package com.style24.batch.biz.job.goods;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.style24.batch.biz.job.TsbAbstractJob;
import com.style24.batch.biz.service.TsbGoodsEpService;
import com.style24.core.support.util.CryptoUtils;
import com.style24.core.support.util.MaskingUtils;
import com.style24.persistence.domain.GoodsEp;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaFileUtil;

/**
 * 다음 상품평EP 생성
 *
 * @author gagamel
 * @since 2021. 6. 18
 */
@Component
@Slf4j
public class TsbDaumReviewEp extends TsbAbstractJob<Collection<GoodsEp>, Collection<GoodsEp>, String> {

	@Autowired
	private TsbGoodsEpService goodsEpService;

	@Autowired
	private Environment env;

	@Override
	public Collection<GoodsEp> read() throws Exception {
		// 다음 상품평EP 목록
		return goodsEpService.getDaumReviewEpList();
	}

	@Override
	public Collection<GoodsEp> process(Collection<GoodsEp> goodsEpList) throws Exception {
		return goodsEpList;
	}

	@Override
	public String write(Collection<GoodsEp> goodsEpList) throws Exception {
		/*
		if (goodsEpList != null && !goodsEpList.isEmpty()) {
			StringBuilder sb = new StringBuilder();
			sb.append("<<<tocnt>>>").append(goodsEpList.size());
		
			for (GoodsEp goodsEp : goodsEpList) {
				this.setEpInfo(sb, goodsEp);
			}
		
			String uploadPath = GagaFileUtil.getConcatenationPath(env.getProperty("ep.file.path"), "iStyle24ProductFeed_DaumBest100_new.txt");
			log.info("다음 상품평EP 파일 경로: {}", uploadPath);
		
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(uploadPath)), "UTF-8"));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
		}
		*/

		if (goodsEpList != null && !goodsEpList.isEmpty()) {

			String uploadPath = GagaFileUtil.getConcatenationPath(env.getProperty("ep.file.path"), "iStyle24ProductFeed_DaumBest100_new.txt");
			log.info("다음 상품평EP 파일 경로: {}", uploadPath);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(uploadPath)), "UTF-8"));

			StringBuilder sb = new StringBuilder();
			sb.append("<<<tocnt>>>").append(goodsEpList.size());
			bw.append(sb.toString());

			for (GoodsEp goodsEp : goodsEpList) {
				sb = new StringBuilder();
				this.setEpInfo(sb, goodsEp);
				bw.append(sb.toString());
			}

			bw.write(sb.toString());
			bw.flush();
			bw.close();
		}

		return "OK";
	}

	@Override
	public void notify(String result) throws Exception {
		// Do nothing
	}

	/**
	 * EP 정보 설정
	 * @param sb - StringBuilder
	 * @param goodsEp - 상품EP 정보
	 */
	private void setEpInfo(StringBuilder sb, GoodsEp goodsEp) {
		sb.append("<<<begin>>>\n");
		sb.append("<<<mapid>>>").append(goodsEp.getMapid()).append("\n");
		sb.append("<<<reviewid>>>").append(goodsEp.getReviewid()).append("\n");
		sb.append("<<<status>>>").append(goodsEp.getStatus()).append("\n");
		sb.append("<<<title>>>").append(goodsEp.getTitle()).append("\n");
		sb.append("<<<content>>>").append(goodsEp.getContent()).append("\n");
		sb.append("<<<writer>>>").append(MaskingUtils.name(CryptoUtils.decryptAES(goodsEp.getWriter()))).append("\n");
		sb.append("<<<cdate>>>").append(goodsEp.getCdate()).append("\n");
		sb.append("<<<rating>>>").append(goodsEp.getRating()).append("\n");
		sb.append("<<<ftend>>>\n");
	}

}
