package com.diaoyun.zion.chinafrica.bis.impl;

import com.diaoyun.zion.chinafrica.bis.IItemSpider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.ProductResponse;
import com.diaoyun.zion.master.util.HttpClientUtil;
import com.diaoyun.zion.master.util.TranslateHelper;
import com.diaoyun.zion.master.util.spider.SpiderUtil;
import com.diaoyun.zion.master.util.spider.VansSpiderParse;
import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * Vans（范斯） 数据爬虫
 *
 * @author 爱酱油不爱醋
 */
@Component("vansSpider")
public class VansSpider implements IItemSpider {
    private static Logger logger = LoggerFactory.getLogger(ZaraSpider.class);

    /**
     * Vans 数据爬虫
     * @see VansSpiderParse#formatProductResponse 格式化数据方法
     * @param targetUrl 接收的商品详情路径
     * @return 格式化与翻译后的 Json 数据
     */
    @Override
    public JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException, TimeoutException {
        String content = HttpClientUtil.getContentByUrl(targetUrl, PlatformEnum.VANS.getValue());
        Document document = Jsoup.parse(content);
        String pTitle = document.select("product-titles").text();
        String[] spilt = targetUrl.split("/");
        String pId = SpiderUtil.retainNumber(spilt[4]);
        targetUrl = "https://" + spilt[2] + "/wap/product-ajax_product_spec-" + pId + ".html";
        content = HttpClientUtil.getContentByUrl(targetUrl, PlatformEnum.VANS.getValue());
        ProductResponse productResponse = VansSpiderParse.formatProductResponse(content, pId, pTitle);
        JSONObject resultObj = JSONObject.fromObject(productResponse);
        TranslateHelper.translateProductResponse(resultObj);
        return resultObj;
    }
}
