package com.zjty.autotest;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zjty.autotest.pojo.report.Measure;
import com.zjty.autotest.pojo.report.Report;
import com.zjty.autotest.pojo.test.Input;
import com.zjty.autotest.pojo.test.Project;
import com.zjty.autotest.service.impl.SeleniumExecutor;
import com.zjty.autotest.util.WebDriverUtil;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.boot.jaxb.SourceType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@SpringBootTest
class AutotestApplicationTests {

    @Autowired
    SeleniumExecutor seleniumExecutor;

    @Test
    public void execute() throws InterruptedException {
        List<Input> inputs = Lists.newArrayList();
        inputs.add(new Input(null, null, null, "caozuoyuan123"));
        inputs.add(new Input(null, null, null, "caozuoyuan"));

        String url = "http://192.168.1.244:8080/person/#/login";
        Project project = new Project(
                null,
                null,
                "firefox",
                url,
                inputs
        );
//        String url2 = "file:///C:/code/wps/index.html";
//        WebDriver driver = WebDriverUtil.getWebDriver("firefox");
//        driver.manage().timeouts().pageLoadTimeout(4L, TimeUnit.SECONDS);
//        driver.get(url);
//        Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofSeconds(2));
//        String source1 = driver.getPageSource();
//        driver.get(url2);
//        driver.get(url);
//        Sleeper.SYSTEM_SLEEPER.sleep(Duration.ofSeconds(2));
//        String source2 = driver.getPageSource();
//        System.out.println(Objects.equals(source1,source2));


        Report report = seleniumExecutor.execute(project);
        for (Measure measure : report.getMeasures()) {
            System.out.println(measure);
        }


//        Document document = Jsoup.parse(driver.getPageSource());
//        List<Element> elements = document.getAllElements().stream()
//                .filter(element -> element.childrenSize() == 0)
//                .collect(Collectors.toList());
//        for (Element element : elements) {
//            System.out.println("==============================================");
//            System.out.println(String.format("tag:%s   text:%s  attrs:%s",element.tagName(),element.text(),element.attributes()));
//        }
    }

    private Boolean elementMatch(WebElement webElement, Attributes Attributes) {
        return Attributes.asList().stream().allMatch(attribute -> Objects.equals(attribute.getValue(), webElement.getAttribute(attribute.getKey())));
    }

}
