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/")) {
            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("uniqlo.cn/") && targetUrl.contains("#/product?pid")) {
            platformEnum = PlatformEnum.UNIQLO;
        } else if (targetUrl.contains("hm.com/m") && targetUrl.contains("productpage")) {
            platformEnum = PlatformEnum.HM;
        } else if(targetUrl.contains("adidas.com") && targetUrl.contains("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("wap.ur") && targetUrl.contains("product")) {
            platformEnum=PlatformEnum.URBANREVIVO;
        } else if(targetUrl.contains("underarmour")) {
            platformEnum=PlatformEnum.UNDERARMOUR;
        } else if(targetUrl.contains("ochirly.com") && targetUrl.contains("p/mobile/")) {
            platformEnum=PlatformEnum.OCHIRLY;
        } else if(targetUrl.contains("esprit.cn/product/") && targetUrl.contains("styleNo") && targetUrl.contains("skucode")) {
            platformEnum=PlatformEnum.ESPRIT;
        } else if(targetUrl.contains("levi.com") && targetUrl.contains("product")) {
            platformEnum=PlatformEnum.LEVI;
        } else if(targetUrl.contains("moco.com/moco/")) {
            platformEnum=PlatformEnum.MOCO;
        } else if (targetUrl.contains("massimodutti") && targetUrl.contains("colorId") && targetUrl.contains("categoryId")) {
            platformEnum = PlatformEnum.MASSIMODUTTI;
        } else if (targetUrl.contains("coach")) {
            platformEnum = PlatformEnum.COACH;
        } else if (targetUrl.contains("vans.com") && targetUrl.contains("wap/product")) {
            platformEnum = PlatformEnum.VANS;
        }
        return platformEnum;
    }
}
