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. 14
 */
@Component
@Slf4j
public class TsbNaverAllEp extends TsbAbstractJob<Collection<GoodsEp>, Collection<GoodsEp>, String> {

	@Autowired
	private TsbGoodsEpService naverEpService;

	@Autowired
	private Environment env;

	@Override
	public Collection<GoodsEp> read() throws Exception {
		// 네이버 전체EP 생성
		naverEpService.createNaverEp("ALL");

		// 네이버 전체EP 목록
		return naverEpService.getNaverEpList();
	}

	@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();
			this.setEpTitle(sb);

			for (GoodsEp goodsEp : goodsEpList) {
				this.setEpInfo(sb, goodsEp);
			}

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

		return "OK";
	}

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

	/**
	 * EP 타이틀 설정
	 * @param sb - StringBuilder
	 */
	private void setEpTitle(StringBuilder sb) {
		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\n").append("age_group\t").append("gender\n");
//		sb.append("class\t").append("update_time\n");	// 전체EP에서는 제외
	}

	/**
	 * EP 정보 설정
	 * @param sb - StringBuilder
	 * @param goodsEp - 상품EP 정보
	 */
	private void setEpInfo(StringBuilder sb, GoodsEp goodsEp) {
		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.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("\n");
//		sb.append(goodsEp.getUpdGb()).append("\t");
//		sb.append(goodsEp.getUpdateTime()).append("\n");
	}

}
