package parser;

import data.GuiMysql;
import io.github.bonigarcia.wdm.WebDriverManager;
import objects.SoccerMatch;
import objects.SoccerMatchAnalysis;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.*;

public class Svenskaspel {

    String matchesXPATH = "//div[@class = 'pg_coupon_static__event_container']";
    String date;

    Map<String, String> nameReplace = Map.ofEntries(
            new AbstractMap.SimpleEntry<String, String>("manchester united", "Manchester Utd"),
            new AbstractMap.SimpleEntry<String, String>("queens park rangers", "QPR"),
            new AbstractMap.SimpleEntry<String, String>("west bromwich", "West Brom"),
            new AbstractMap.SimpleEntry<String, String>("blackburn rovers", "Blackburn"),
            new AbstractMap.SimpleEntry<String, String>("nottingham forest", "Nottingham"),
            new AbstractMap.SimpleEntry<String, String>("wolverhampton", "Wolves"));

    public List<SoccerMatchAnalysis> GetStryktipsetSelenium(String date) {
        this.date = date;
        String url = "https://spela.svenskaspel.se/stryktipset/resultat/" + date + "/statistik";

        //RemoteWebDriver seleniumDriver = getSeleniumDriver();
        //seleniumDriver.get(url);

        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver(getChromeOptions());
        driver.get(url);

        CloseCookiePopup(driver);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        List<SoccerMatchAnalysis> matches = FindMatches(driver);

        driver.close();
        driver.quit();
        return matches;
    }

    private List<SoccerMatchAnalysis> FindMatches(WebDriver driver) {
        List<SoccerMatch> soccerMatches = new ArrayList<>();
        List<SoccerMatchAnalysis> matchAnalyses = new ArrayList<>();

        List<WebElement> matches = driver.findElements(By.xpath(matchesXPATH));
        for (WebElement match : matches) {
            List<String> teamNames = GetNames(match);
            String homeTeamName = teamNames.get(0);
            String awayTeamName = teamNames.get(1);
            System.out.println("Match: " + homeTeamName + " " + awayTeamName);
            if (nameReplace.containsKey(homeTeamName.toLowerCase())) {
                homeTeamName = nameReplace.get(homeTeamName.toLowerCase());
            }
            if (nameReplace.containsKey(awayTeamName.toLowerCase())) {
                awayTeamName = nameReplace.get(awayTeamName.toLowerCase());
            }
            soccerMatches.add(GuiMysql.getInstance().getMatch(date,
                    homeTeamName.trim().replace(" ", "%") + "%",
                    awayTeamName.trim().replace(" ", "%") + "%"));
        }

        for (SoccerMatch match : soccerMatches) {
            try {
                matchAnalyses.add(new SoccerMatchAnalysis(match));
            } catch (NullPointerException e) {
                System.out.println("Failed to get team info between " + match.getHomeTeamName() + "-" + match.getAwayTeamName());
            }
        }

        return matchAnalyses;
    }

    private List<String> GetNames(WebElement match) {
        List<String> result = new ArrayList<>();

        WebElement hometeamWebElement = match.findElement(By.xpath("./a/div[2]/span[1]"));
        WebElement awayteamWebElement = match.findElement(By.xpath("./a/div[2]/span[3]"));

        if (hometeamWebElement.findElements(By.xpath("./abbr")).size() == 1) {
            String teamName = hometeamWebElement.findElement(By.xpath("./abbr")).getAttribute("title");
            result.add(teamName);
        } else {
            result.add(hometeamWebElement.findElement(By.xpath("./span")).getText());
        }

        if (awayteamWebElement.findElements(By.xpath("./abbr")).size() == 1) {
            String teamName = awayteamWebElement.findElement(By.xpath("./abbr")).getAttribute("title");
            result.add(teamName);
        } else {
            result.add(awayteamWebElement.findElement(By.xpath("./span")).getText());
        }

        return result;
    }

    private void CloseCookiePopup(WebDriver driver) {
        WebElement acceptButton = driver.findElement(By.xpath("//button[@id='onetrust-accept-btn-handler']"));
        acceptButton.click();
    }

    private ChromeOptions getChromeOptions() {
        ChromeOptions options = new ChromeOptions();
        // Fixing 255 Error crashes
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");

        // Options to trick bot detection
        // Removing webdriver property
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        options.setExperimentalOption("useAutomationExtension", null);

        // Changing the user agent / browser fingerprint
        options.addArguments("window-size=1920,1080");
        options.addArguments(
                "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");

        // Other
        options.addArguments("disable-infobars");

        return options;
    }
}
