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.HMSpiderParse;
import net.sf.json.JSONObject;
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;

/**
 * H&M 数据爬虫
 *
 * TODO 数据被处理，尚未方法爬取
 *
 * @author 爱酱油不爱醋
 */
@Component("hmSpider")
public class HmSpider implements IItemSpider {
    private static Logger logger = LoggerFactory.getLogger(UniqloSpider.class);

    /**
     * H&M 详情商品页url
     */
    private static final String H_M_URL = "https://www2.hm.com/zh_cn/productpage";

    /**
     * H&M 数据格式化
     * @param targetUrl
     * @return
     */
    @Override
    public JSONObject captureItem(String targetUrl) throws URISyntaxException, IOException, ExecutionException, InterruptedException, TimeoutException {
        String[] spilt = targetUrl.split("productpage.");
        targetUrl = "https://www2.hm.com/zh_cn/productpage." + spilt[1];
        String content = HttpClientUtil.getContentByUrl(targetUrl, PlatformEnum.HM.getValue());
        ProductResponse productResponse = HMSpiderParse.formatProductResponse(content);
        JSONObject resultObj = JSONObject.fromObject(productResponse);
        TranslateHelper.translateProductResponse(resultObj);
        return resultObj;
    }

//    public static void main(String[] args) throws Exception {
//        String targetUrl = "https://m2.hm.com/m/zh_cn/productpage.0806412004.html";
//        String[] spilt = targetUrl.split("productpage.");
//        targetUrl = "https://www2.hm.com/zh_cn/productpage." + spilt[1];
//        String content = HttpClientUtil.getContentByUrl(targetUrl, PlatformEnum.HM.getValue());
//        // 获取主要数据并将转换 Json 数据及 Document 对象
//        String detailStr = JsoupUtil.getScriptContent(content, "productArticleDetails");
//        int firstBrackets = detailStr.indexOf("{");
//        int lastbrackets = detailStr.lastIndexOf("}");
//        String resultStr = detailStr.substring(firstBrackets,lastbrackets+1);
//        resultStr = resultStr.replaceAll("\'", "\"")
//                .replaceAll("\"image\": isDesktop [?] ", "")
//                .replaceAll("\"fullscreen\": isDesktop [?] ", "")
//                .replaceAll("\"zoom\": isDesktop [?] ", "");
//        JSONObject dataMap = JSONObject.fromObject(resultStr);
//        Document document = Jsoup.parse(content);
//    }

}
