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.persistence.domain.GoodsEp;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaFileUtil;

/**
 * 네이버 판매지수EP 생성
 *
 * @author gagamel
 * @since 2021. 6. 15
 */
@Component
@Slf4j
public class TsbNaverSellEp extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TsbGoodsEpService naverEpService;

	@Autowired
	private Environment env;

	private static final int PAGE_SIZE = 10000;

	@Override
	public String read() throws Exception {
		return "OK";
	}

	@Override
	public String process(String result) throws Exception {
		return "OK";
	}

	@Override
	public String write(String result) throws Exception {
		// 총건수
		int totCnt = naverEpService.getNaverSellEpTotalCount();
		log.info("totCnt: {}", totCnt);

		if (totCnt < 1) {
			return "OK";
		}

		String uploadPath = GagaFileUtil.getConcatenationPath(env.getProperty("ep.file.path"), "iStyle24ProductFeed_NaverEP_SellingIndex_new.txt");
		log.info("네이버 판매지수EP 파일 경로: {}", uploadPath);

		int totPage = totCnt / PAGE_SIZE;
		log.info("totalPage: {}", totPage);

		if (totCnt % PAGE_SIZE > 0) {
			totPage++;
		}

		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(uploadPath)), "UTF-8"));

		// 타이틀 Write
		bw.append(this.getEpTitle());

		for (int page = 0; page < totPage; page++) {
			int limitStartRow = (page * PAGE_SIZE + 1) - 1;
			GoodsEp goodsEp = new GoodsEp();
			goodsEp.setLimitStartRow(limitStartRow);
			goodsEp.setPageSize(PAGE_SIZE);

			// 네이버 판매지수EP 목록
			Collection<GoodsEp> goodsEpList = naverEpService.getNaverSellEpList(goodsEp);

			for (GoodsEp goodsEpInfo : goodsEpList) {
				bw.append(this.getEpInfo(goodsEpInfo));
			}
			log.info("{} Page data wrote successfully!", (page + 1));
		}

		bw.close();

		return "OK";
	}

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

	/**
	 * Get EP 타이틀
	 * @return
	 */
	private String getEpTitle() {
		StringBuilder sb = new StringBuilder();
		sb.append("id\t").append("sale_count\t").append("sale_price\t").append("order_count\t").append("dt\n");
		return sb.toString();
	}

	/**
	 * Get EP 정보
	 * @param goodsEp - 상품EP 정보
	 * @return
	 */
	private String getEpInfo(GoodsEp goodsEp) {
		StringBuilder sb = new StringBuilder();
		sb.append(goodsEp.getId()).append("\t");
		sb.append(goodsEp.getSaleCount()).append("\t");
		sb.append(goodsEp.getSalePrice()).append("\t");
		sb.append(goodsEp.getOrderCount()).append("\t");
		sb.append(goodsEp.getDt()).append("\n");
		return sb.toString();
	}

}
