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 io.micrometer.core.instrument.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaFileUtil;

/**
 * 다음 전체EP 생성
 *
 * @author gagamel
 * @since 2021. 6. 15
 */
@Component
@Slf4j
public class TsbDaumAllEp extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TsbGoodsEpService goodsEpService;

	@Autowired
	private Environment env;

	private static final int PAGE_SIZE = 100000;

	@Override
	public String read() throws Exception {
		// 다음 전체EP 생성
		goodsEpService.createDaumEp("ALL");

		return "OK";
	}

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

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

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

		String uploadPath = GagaFileUtil.getConcatenationPath(env.getProperty("ep.file.path"), "iStyle24ProductFeed_DaumProductFull_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"));

		// 총건수
		bw.append("<<<tocnt>>>" + totCnt + "\n");

		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);
			Collection<GoodsEp> goodsEpList = goodsEpService.getDaumEpPagingList(goodsEp);
			for (GoodsEp goodsEpIfno : goodsEpList) {
				bw.append(this.getEpInfo(goodsEpIfno));
			}
			log.info("{} Page data wrote successfully!", (page + 1));
		}

		bw.close();

		return "OK";
	}

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

	/**
	 * EP 정보 설정
	 * @param goodsEp - 상품EP 정보
	 * @return
	 */
	private String getEpInfo(GoodsEp goodsEp) {
		StringBuilder sb = new StringBuilder();
		sb.append("<<<begin>>>\n");
		sb.append("<<<mapid>>>").append(goodsEp.getMapid()).append("\n");
		sb.append("<<<lprice>>>").append(goodsEp.getLprice()).append("\n");
		sb.append("<<<price>>>").append(goodsEp.getPrice()).append("\n");
		sb.append("<<<mpric>>>").append(goodsEp.getMpric()).append("\n");
//		sb.append("<<<class>>>").append(goodsEp.getUpdGb()).append("\n");	// 전체EP에서는 제외
//		sb.append("<<<utime>>>").append(goodsEp.getUtime()).append("\n");	// 전체EP에서는 제외
		sb.append("<<<pname>>>").append(goodsEp.getPname()).append("\n");
		sb.append("<<<pgurl>>>").append(goodsEp.getPgurl()).append("&af=AF043").append("\n");
		sb.append("<<<igurl>>>").append(goodsEp.getIgurl()).append("\n");
//		sb.append("<<<upimg>>>").append(goodsEp.getUpimg().equals("Y") ? goodsEp.getUpimg() : "").append("\n");
		sb.append("<<<cate1>>>").append(goodsEp.getCate1()).append("\n");

		if (StringUtils.isNotBlank(goodsEp.getCate1())) {
			sb.append("<<<caid1>>>").append(goodsEp.getCaid1()).append("\n");
		} else {
			sb.append("<<<caid1>>>").append("").append("\n");
		}

		sb.append("<<<cate2>>>").append(goodsEp.getCate2()).append("\n");

		if (StringUtils.isNotBlank(goodsEp.getCate2())) {
			sb.append("<<<caid2>>>").append(goodsEp.getCaid1()).append(goodsEp.getCaid2()).append("\n");
		} else {
			sb.append("<<<caid2>>>").append("").append("\n");
		}

		sb.append("<<<cate3>>>").append(goodsEp.getCate3()).append("\n");

		if (StringUtils.isNotBlank(goodsEp.getCate3())) {
			sb.append("<<<caid3>>>").append(goodsEp.getCaid1()).append(goodsEp.getCaid2()).append(goodsEp.getCaid3()).append("\n");
		} else {
			sb.append("<<<caid3>>>").append("").append("\n");
		}

		sb.append("<<<cate4>>>").append(goodsEp.getCate4()).append("\n");

		if (StringUtils.isNotBlank(goodsEp.getCate4())) {
			sb.append("<<<caid4>>>").append(goodsEp.getCaid1()).append(goodsEp.getCaid2()).append(goodsEp.getCaid3()).append(goodsEp.getCaid4()).append("\n");
		} else {
			sb.append("<<<caid4>>>").append("").append("\n");
		}

		sb.append("<<<model>>>").append(goodsEp.getModel()).append("\n");
		sb.append("<<<brand>>>").append(goodsEp.getBrand()).append("\n");
		sb.append("<<<maker>>>").append(goodsEp.getMaker()).append("\n");
		sb.append("<<<coupo>>>").append(goodsEp.getCoupon()).append("\n");
		sb.append("<<<mcoupon>>>").append(goodsEp.getMcoupon()).append("\n");
		sb.append("<<<deliv>>>").append(goodsEp.getDeliv()).append("\n");
		sb.append("<<<delivterm>>>").append(goodsEp.getDelivterm()).append("\n");
//		sb.append("<<<dlvdt>>>").append(goodsEp.getDlvdt()).append("\n");
		sb.append("<<<rating>>>").append(goodsEp.getRating()).append("\n");
		sb.append("<<<revct>>>").append(goodsEp.getRevct()).append("\n");
		sb.append("<<<event>>>").append(goodsEp.getEvent()).append("\n");
		sb.append("<<<carddn>>>").append(goodsEp.getCarddn()).append("\n");
		sb.append("<<<cardp>>>").append(goodsEp.getCardp()).append("\n");
//		sb.append("<<<weight>>>").append(goodsEp.getWeight()).append("\n");
//		sb.append("<<<selid>>>").append(goodsEp.getSelid()).append("\n");
		sb.append("<<<adult>>>").append(goodsEp.getAdult().equals("Y") ? goodsEp.getAdult() : "").append("\n");
		sb.append("<<<insco>>>").append("").append("\n");
//		sb.append("<<<sales>>>").append(goodsEp.getSales()).append("\n");
//		sb.append("<<<likecnt>>>").append(goodsEp.getLikecnt()).append("\n");
		sb.append("<<<pubdate>>>").append(goodsEp.getPubdate()).append("\n");
//		sb.append("<<<member>>>").append(goodsEp.getMember()).append("\n");
		sb.append("<<<ftend>>>\n");
		return sb.toString();
	}

}
