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 TsbNaverSummaryEp extends TsbAbstractJob<String, String, String> {

	@Autowired
	private TsbGoodsEpService naverEpService;

	@Autowired
	private Environment env;

	private static final int PAGE_SIZE = 100000;

	@Override
	public String read() throws Exception {
		// 네이버 요약EP 생성
		naverEpService.createNaverEp("SUMMARY");

		return "OK";
	}

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

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

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

		String uploadPath = GagaFileUtil.getConcatenationPath(env.getProperty("ep.file.path"), "iStyle24ProductFeed_NaverEP_Summary_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);
			Collection<GoodsEp> goodsEpList = naverEpService.getNaverEpPagingList(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("title\t");
		sb.append("price_pc\t").append("price_mobile\t").append("normal_price\t");
		sb.append("link\t").append("mobile_link\t").append("image_link\t").append("add_image_link\t");
		sb.append("category_name1\t").append("category_name2\t").append("category_name3\t").append("category_name4\t").append("naver_category\t");
		sb.append("condition\t").append("import_flag\t").append("parallel_import\t").append("order_made\t").append("product_flag\t");
		sb.append("adult\t").append("manufacture_define_number\t");
		sb.append("brand\t").append("maker\t").append("origin\t");
		sb.append("card_event\t").append("event_words\t").append("coupon\t").append("partner_coupon_download\t");
		sb.append("interest_free_event\t").append("point\t").append("installation_costs\t").append("search_tag\t").append("minimum_purchase_quantity\t");
		sb.append("review_count\t").append("shipping\t").append("delivery_grade\t").append("delivery_detail\t").append("age_group\t").append("gender\t");
		sb.append("class\t").append("update_time\n");
		return sb.toString();
	}

	/**
	 * EP 정보 설정
	 * @param goodsEp - 상품EP 정보
	 * @return
	 */
	private String getEpInfo(GoodsEp goodsEp) {
		StringBuilder sb = new StringBuilder();
		sb.append(goodsEp.getId()).append("\t");
		sb.append(goodsEp.getTitle()).append("\t");
		sb.append(goodsEp.getPricePc()).append("\t");
		sb.append(goodsEp.getPriceMobile()).append("\t");
		sb.append(goodsEp.getNormalPrice()).append("\t");
		sb.append(goodsEp.getLink()).append("&af=AF033").append("\t");
		sb.append(goodsEp.getMobileLink()).append("&af=AF028").append("\t");
		sb.append(goodsEp.getImageLink()).append("\t");
		sb.append(goodsEp.getAddImageLink()).append("\t");
		sb.append(goodsEp.getCategoryName1()).append("\t");
		sb.append(goodsEp.getCategoryName2()).append("\t");
		sb.append(goodsEp.getCategoryName3()).append("\t");
		sb.append(goodsEp.getCategoryName4()).append("\t");
		sb.append(goodsEp.getNaverCategory()).append("\t");
		sb.append(goodsEp.getGoodsGbNm()).append("\t");
		sb.append(goodsEp.getImportFlag()).append("\t");
		sb.append(goodsEp.getParallelImport()).append("\t");
		sb.append(goodsEp.getOrderMade()).append("\t");
		sb.append(goodsEp.getProductFlag()).append("\t");
		sb.append(goodsEp.getAdult()).append("\t");
		sb.append(goodsEp.getManufactureDefineNumber()).append("\t");
		sb.append(goodsEp.getBrand()).append("\t");
		sb.append(goodsEp.getMaker()).append("\t");
		sb.append(goodsEp.getOrigin()).append("\t");
		sb.append(goodsEp.getCardEvent()).append("\t");
		sb.append(goodsEp.getEventWords()).append("\t");
		sb.append(goodsEp.getCoupon()).append("\t");
		sb.append(goodsEp.getPartnerCouponDownload()).append("\t");
		sb.append(goodsEp.getInterestFreeEvent()).append("\t");
		sb.append(goodsEp.getPoint()).append("\t");
		sb.append("").append("\t");
		sb.append(goodsEp.getSearchTag()).append("\t");
		sb.append(goodsEp.getMinimumPurchaseQuantity()).append("\t");
		sb.append(goodsEp.getReviewCount()).append("\t");
		sb.append(goodsEp.getShipping()).append("\t");
		sb.append("").append("\t");
		sb.append("").append("\t");
		sb.append(goodsEp.getAgeGroup()).append("\t");
		sb.append(goodsEp.getGender()).append("\t");
		sb.append(goodsEp.getUpdGb()).append("\t");
		sb.append(goodsEp.getUpdateTime()).append("\n");
		return sb.toString();
	}

}
