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.springframework.stereotype.Service;

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

/**
 * 爬虫服务类
 *
 * @author G
 */
@Service("spiderService")
public class SpiderServiceImpl implements SpiderService {

    /**
     * 判断链接属于哪个平台
     * @param targetUrl 在商品详情页截取到的路径
     * @return 对应的爬虫
     */
    @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);
    }

    /**
     * 获取汇率
     * @param currency FOREXUSDCNY 人民币换美元
     * @return 汇率
     */
    @Override
    public BigDecimal getExchangeRate(String currency) throws IOException, URISyntaxException {
        //默认人民币换美元
        if(StringUtils.isBlank(currency)) {
            currency="FOREXUSDCNY";
        }
        return NetWorkSpider.getRateFromHexun(currency);
    }

    /**
     * 判断链接属于哪个平台
     * @param targetUrl 在商品详情页截取到的路径
     * @return 对应的爬虫
     */
    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")) {
            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")) {
            platformEnum = PlatformEnum.ZARA;
        } else if (targetUrl.contains("uniqlo") && 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("lily")) {
            platformEnum=PlatformEnum.LILY;
        } else if(targetUrl.contains("eifini")) {
            platformEnum=PlatformEnum.EIFINI;
        } else if(targetUrl.contains("wap")) {
            platformEnum=PlatformEnum.URBANREVIVO;
        } else if(targetUrl.contains("abercrombie")) {
            platformEnum=PlatformEnum.ABERCROMBIEFITCH;
        } else if(targetUrl.contains("ochirly.com")) {
            platformEnum=PlatformEnum.OCHIRLY;
        } else if(targetUrl.contains("esprit")) {
            platformEnum=PlatformEnum.ESPRIT;
        } else if(targetUrl.contains("levi.com")) {
            platformEnum=PlatformEnum.LEVI;
        } else if(targetUrl.contains("moco.com/moco/")) {
            platformEnum=PlatformEnum.MOCO;
        } else if (targetUrl.contains("massimodutti")) {
            platformEnum = PlatformEnum.MASSIMODUTTI;
        } else if (targetUrl.contains("coach")) {
            platformEnum = PlatformEnum.COACH;
        } else if (targetUrl.contains("revolve")) {
            platformEnum = PlatformEnum.REVOLVE;
        } else if (targetUrl.contains("vans.com")) {
            platformEnum = PlatformEnum.VANS;
        } else if (targetUrl.contains("oysho")) {
            platformEnum = PlatformEnum.OYSHO;
        } else if (targetUrl.contains("stradivarius")) {
            platformEnum = PlatformEnum.STRADIVARIUS;
        } else if (targetUrl.contains("maje")) {
            platformEnum = PlatformEnum.MAJE;
        } else if (targetUrl.contains("gucci")) {
            platformEnum = PlatformEnum.GUCCI;
        } else if (targetUrl.contains("burberry.com")) {
            platformEnum = PlatformEnum.BURBERRY;
        } else if (targetUrl.contains("prada.com")) {
            platformEnum = PlatformEnum.PRADA;
        } else if (targetUrl.contains("fendi")) {
            platformEnum = PlatformEnum.FENDI;
        } else if (targetUrl.contains("apple")) {
            platformEnum = PlatformEnum.APPLE;
        } else if (targetUrl.contains("louisvuitton")) {
            platformEnum = PlatformEnum.LOUISVUITTON;
        }
        return platformEnum;
    }
}
