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

import com.diaoyun.zion.chinafrica.bis.IItemSpider;
import com.diaoyun.zion.chinafrica.bis.impl.NetWorkSpider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.factory.ItemSpiderFactory;
import com.diaoyun.zion.chinafrica.service.SpiderService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

@Service("spiderService")
public class SpiderServiceImpl implements SpiderService {

    @Override
    public JSONObject getItemDetail(String targetUrl) throws InterruptedException, IOException, ExecutionException, URISyntaxException, TimeoutException {

        //判断链接属于哪个平台
        PlatformEnum platformEnum=judgeUrlType(targetUrl);
        IItemSpider iItemSpider=ItemSpiderFactory.getSpider(platformEnum);
        return iItemSpider.captureItem(targetUrl);
    }

    @Override
    public BigDecimal getExchangeRate(String currency) throws IOException, URISyntaxException {

        //默认人民币换美元
        if(StringUtils.isBlank(currency)) {
            currency="FOREXUSDCNY";
        }
        return NetWorkSpider.getRateFromHexun(currency);
    }

    private PlatformEnum judgeUrlType(String targetUrl) {
        PlatformEnum platformEnum = PlatformEnum.UN;
        if (targetUrl.contains("taobao.com") && (targetUrl.contains("item.htm") || targetUrl.contains("detail.htm"))) {
            platformEnum = PlatformEnum.TB;
        } else if (targetUrl.contains("tmall.com/item.htm")) {
            platformEnum = PlatformEnum.TM;
        } else if (targetUrl.contains("pullandbear.cn/cn/")) {
            platformEnum = PlatformEnum.PULLANDBEAR;
        } else if(targetUrl.contains("www.gap.cn/pdp/")) {
            platformEnum=PlatformEnum.GAP;
        } else if(targetUrl.contains("www.nike.com/cn/t/")) {
            platformEnum=PlatformEnum.NIKE;
        } else if(targetUrl.contains("www.afri-eshop.com")&&targetUrl.contains("/products/")) {
            platformEnum=PlatformEnum.AfriEshop;
        } else if (targetUrl.contains("zara.cn")) {
            platformEnum = PlatformEnum.ZARA;
        } else if (targetUrl.contains("h.uniqlo.cn/")) {
            platformEnum = PlatformEnum.UNIQLO;
        } else if (targetUrl.contains("hm.com/zh_cn/productpage")) {
            platformEnum = PlatformEnum.HM;
        } else if(targetUrl.contains("https://www.adidas.com.cn/item")) {
            platformEnum=PlatformEnum.ADIDAS;
        } else if(targetUrl.contains("http://www.lily.sh.cn/webapp/wcs/stores/servlet/lilystore")) {
            platformEnum=PlatformEnum.LILY;
        } else if(targetUrl.contains("http://wap.ur.com.cn/product/detail")) {
            platformEnum=PlatformEnum.URBANREVIVO;
        } else if(targetUrl.contains("underarmour.cn/p")) {
            platformEnum=PlatformEnum.UNDERARMOUR;
        } else if(targetUrl.contains("ochirly.com.cn/p/mobile/")) {
            platformEnum=PlatformEnum.OCHIRLY;
        } else if(targetUrl.contains("esprit.cn/product/")) {
            platformEnum=PlatformEnum.ESPRIT;
        } else if(targetUrl.contains("levi.com.cn/product/")) {
            platformEnum=PlatformEnum.LEVI;
        } else if(targetUrl.contains("moco.com/moco/zh/p/")) {
            platformEnum=PlatformEnum.MOCO;
        } else if (targetUrl.contains("massimodutti.cn") && targetUrl.contains("colorId") && targetUrl.contains("categoryId")) {
            platformEnum = PlatformEnum.MASSIMODUTTI;
        }
        return platformEnum;
    }
}
