package cn.com.ctop.toutiao.modules.report.service.impl;

import cn.com.ctop.common.module.utils.HttpUtils2;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceReportDailyAgentService;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
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.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Service
public class BytedanceReportDailyAgentServiceImpl implements IBytedanceReportDailyAgentService {
    @Value("${jeecg.path.chrome-driver}")
    private String chromeDriver;

    public boolean loginAgent() {
        boolean loginStatus = false;
        System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
        String url = "https://agent.oceanengine.com/login";
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--no-sandbox");
        chromeOptions.addArguments("--disable-dev-shm-usage");
        chromeOptions.addArguments("--incognito");
        chromeOptions.addArguments("--disable-gpu");
        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("//input[@name='account']"));
            accountElement.sendKeys("guoyanling@c-top.com.cn");
            Thread.sleep(3000L);
            WebElement passwordElement = webDriver.findElement(By.xpath("//input[@name='password']"));
            passwordElement.sendKeys("HCST-hcst888");
            WebElement captchaElement = webDriver.findElement(By.xpath("//div[contains(@class,'custom-toutiao-captcha')]"));
            String bg = captchaElement.getCssValue("background-image");
//            String result = ChaojiyingUtils.process(bg.split(",")[1],1004);
            String result = "";
            WebElement captchaInputElement = webDriver.findElement(By.xpath("//input[@name='captcha']"));
            WebElement loginElement = webDriver.findElement(By.xpath("//div[contains(text(),'登录')]"));
            Thread.sleep(3000L);
            //点击登录
            loginElement.click();
            Thread.sleep(3000L);

            Set<Cookie> cookies = webDriver.manage().getCookies();
            Iterator iterator = cookies.iterator();
            while (iterator.hasNext()) {
                String cookieStr = iterator.next().toString();
                String[] cookieArray = cookieStr.split(";");
                Map<String, String> cookieMap = new HashMap<>();
                for (String cookie : cookieArray) {
                    String[] kv = cookie.split("=");
                    if (kv.length > 1) {
                        cookieMap.put(kv[0].trim(), kv[1].trim());
                    }
                }
                BasicClientCookie clientCookie = new BasicClientCookie(cookieArray[0].split("=")[0],
                        cookieArray[0].split("=")[1]);
                clientCookie.setDomain("agent.oceanengine.com");
                clientCookie.setPath(cookieMap.get("path"));
                HttpUtils2.cookieStore.addCookie(clientCookie);
            }
            loginStatus = true;
        } catch (Exception e) {
            e.printStackTrace();
            loginStatus = false;
        } finally {
            webDriver.manage().deleteAllCookies();
            webDriver.quit();
        }
        return loginStatus;
    }
}
