package org.jeecg;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.mapper.CtopOauthTokenMapper;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.HttpUtils2;
import cn.com.ctop.crawler.modules.appium.service.IAppiumJobService;
import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.BasicCookieStore;
import org.jeecg.modules.ctop.service.IBidWarningService;
import org.jeecg.modules.ctop.service.ICreateInternalService;
import org.jeecg.modules.mq.Sender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SampleTest {
    @Value("${jeecg.path.chrome-driver}")
    private String chromeDriver;
    @Autowired
    private IAppiumJobService jobService;
    @Autowired
    private ICreateInternalService createInternalService;
    @Autowired
    private IAppiumTaskService appiumTaskService;

    @Test
    public void testKs() throws InterruptedException {
        String url = "https://ad.e.kuaishou.com/#/welcome?redirectUrl=https%3A%2F%2Fad.e.kuaishou.com%2F%23%2Findex";
        System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
        ChromeOptions chromeOptions = new ChromeOptions();
//        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--incognito");
        chromeOptions.addArguments("--disable-gpu");
//        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("--user-agent=" + HttpUtils2.USER_AGENT);
        chromeOptions.setAcceptInsecureCerts(true);
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        try {
            Thread.sleep(3000L);
            HttpUtils2.cookieStore = new BasicCookieStore();
            webDriver.manage().deleteAllCookies();
            //获取登录页面
            webDriver.get(url);
            Thread.sleep(3000L);
            WebElement accountElement = webDriver.findElement(By.xpath("//div[@class='phone ']/input[@type='text']"));
            accountElement.sendKeys("19845004383");
            Thread.sleep(3000L);
            WebElement passwordElement = webDriver.findElement(By.xpath("//div[@class='password ']/input[@type='password']"));
            passwordElement.sendKeys("a123456");
            WebElement loginElement = webDriver.findElement(By.xpath("//div[@class='foot']"));
            Thread.sleep(3000L);
            //点击登录
            loginElement.click();
            Thread.sleep(3000L);
            //获取推广按钮
            WebElement spreadElement = webDriver.findElement(By.linkText("推广"));
            spreadElement.click();
            Thread.sleep(3000L);
            //选择广告创意
            WebElement creativeElement = webDriver.findElement(By.xpath("//div[text()='广告创意']"));
            creativeElement.click();
            Thread.sleep(3000L);
            //输入创意名称，点击搜索
            WebElement searchElement = webDriver.findElement(By.xpath("//input[@type='text']"));
            searchElement.sendKeys("2-这个是你画的吗-设计-11.19");
            Thread.sleep(3000L);
            WebElement searchButton = webDriver.findElement(By.xpath("//button[@class='ant-btn ant-input-search-button ant-btn-primary']"));
            searchButton.click();
            Thread.sleep(3000L);
            WebElement tiyanElement = webDriver.findElement(By.linkText("体验"));
            tiyanElement.click();
            Thread.sleep(2000L);
            WebElement inputKsIdElement = webDriver.findElement(By.xpath("//textarea[@placeholder='请输入快手账号…']"));
            inputKsIdElement.sendKeys("123456765");
            Thread.sleep(2000L);
            WebElement chufaElement = webDriver.findElement(By.xpath("//button[@class='creative-experience-btn-enable']"));
            chufaElement.click();


        } catch (Exception e) {
            e.printStackTrace();
            webDriver.manage().deleteAllCookies();
            webDriver.close();
        }
    }

    @Test
    public void test() {
        jobService.refreshWebPhone();
    }

    @Test
    public void testMq() {
        Sender sender = new Sender();
        sender.send();
    }

    @Autowired
    private CtopOauthTokenMapper oauthTokenMapper;

    @Test
    public void loadKuaishouDataSingle() throws Exception {
        String url = "https://a201903112120030590198943.szwego.com/static/index.html#/shop_detail/A201903112120030590198943";
        System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
        ChromeOptions chromeOptions = new ChromeOptions();
//        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--incognito");
        chromeOptions.addArguments("--disable-gpu");
//        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--window-size=1920,1080");
        chromeOptions.addArguments("--user-agent=" + HttpUtils2.USER_AGENT);
        chromeOptions.setAcceptInsecureCerts(true);
        WebDriver webDriver = new ChromeDriver(chromeOptions);
        webDriver.get(url);
        Thread.sleep(3000L);
        ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
        Thread.sleep(3000L);
        List<WebElement> elementList = webDriver.findElements(By.xpath("//div[@class='word-break ellipsis-two f14 g3']"));
        for (WebElement element : elementList) {
            System.out.println(element.getText());
        }

    }

    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;

    @Test
    public void testMail() throws Exception {
        QueryWrapper<CtopOauthToken> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id", 35675);
        CtopOauthToken token = oauthTokenMapper.selectOne(queryWrapper);

        kuaishouInterfaceService.getCreativeList(token, null, null);

        // String accessToken, Long advertiserId, Date startDate, Date endDate, Integer page
        //   kuaishouInterfaceService.getTargetingTags(35675L, "83dbda039c238c3d3484df248866815c");
    }


    @Autowired
    private IBidWarningService bidWarningService;

    @Test
    public void testMa() throws Exception {
        String url = "https://ad.e.kuaishou.com/rest/openapi/v1/dmp/population/list";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Access-Token", "4e0c7411f78d47cce7a77b2d40b283f2");
        headers.put("Content-Type", " application/json");
        JSONObject requestJson = new JSONObject();
        requestJson.put("advertiser_id", 23212L);
        String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
        JSONObject resultJson = JSONObject.parseObject(result);
        System.err.println(resultJson);
    }


}



