package parser;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import object.ResultDTO;

public class BetHard extends ParserBase {

    private boolean tomorrowParsed;

    public BetHard() {
    }

    public List<ResultDTO> seleniumParseMatches() {
        List<ResultDTO> result = new ArrayList<>();

        String url = "https://se-sports.bethard.com/sports/?langid=722&stoken=logout";
        ChromeDriver driver = getSeleniumDriver();

        WebDriverWait wait = getWaitDriver(driver);

        driver.get(url);

        wait.until(
                ExpectedConditions.numberOfElementsToBeMoreThan(
                        By.xpath("//div[contains(@class,'sports-list-item-text')]"),
                        0));

        WebElement fotballSideMenu = driver
                .findElement(By.xpath("//div[@data-uat='sports-list-item-text-name' and text()='Fotboll']/.."));

        wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("//div[@data-uat='sports-list-item-text-name' and text()='Fotboll']/..")));

        fotballSideMenu.click();

        wait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("//span[@data-uat='tab-switch-btn-text' and text()='Daglig matchlista']/../..")));
        WebElement dagligLista = driver
                .findElement(By.xpath("//span[@data-uat='tab-switch-btn-text' and text()='Daglig matchlista']/../.."));

        dagligLista.click();

        wait.until(
                ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//div[@class='rj-instant-collapsible']"), 0));

        parseMatches(driver, wait, result);

        driver.close();
        return result;
    }

    private void parseMatches(ChromeDriver driver, WebDriverWait wait, List<ResultDTO> result) {
        List<WebElement> expandableDivs = driver
                .findElements(By.xpath("//div[@class='rj-instant-collapsible']"));

        List<WebElement> handledDiv = new ArrayList<>();
        while (true) {
            WebElement webElement = expandableDivs.get(0);
            scrollElementIntoViewCenter(driver, webElement);
            if (webElement.getAttribute("data-collapsed").equals(Boolean.TRUE.toString())) {
                int matchDivsBefore = driver.findElements(By.xpath("//div[@class='rj-ev-list__ev-card__inner']"))
                        .size();
                webElement.click();
                wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(
                        By.xpath("//div[@class='rj-ev-list__ev-card__inner']"), matchDivsBefore - 1));
            }
            List<WebElement> matchDiv = webElement
                    .findElements(By.xpath(".//div[@class='rj-ev-list__ev-card__inner']"));
            for (WebElement match : matchDiv) {
                parseMatch(match, result);
            }

            handledDiv.add(webElement);
            expandableDivs.remove(webElement);
            expandableDivs = driver
                    .findElements(By.xpath("//div[@class='rj-instant-collapsible']"));

            expandableDivs.removeAll(handledDiv);

            if (expandableDivs.size() <= 0) {
                break;
            }
        }
        if (!tomorrowParsed) {
            tomorrowParsed = true;
            try {
                scrollToTopOfPage(driver);
                Thread.sleep(200);
                WebElement tomorrowHeader = driver.findElement(By.xpath("//div[text()='Imorgon']//../.."));

                tomorrowHeader.click();
                wait.until(
                        ExpectedConditions.numberOfElementsToBeMoreThan(
                                By.xpath("//div[@class='rj-instant-collapsible']"),
                                0));
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            parseMatches(driver, wait, result);
        }
    }

    private void parseMatch(WebElement match, List<ResultDTO> result) {
        WebElement participants = match.findElement(By.xpath(".//a/div[contains(@class,'name')]"));

        WebElement homeTeamSpan = participants.findElement(By.xpath(".//div[1]/span"));
        WebElement awayTeamSpan = participants.findElement(By.xpath(".//div[2]/span"));

        String homeTeamName = homeTeamSpan.getText();
        String awayTeamName = awayTeamSpan.getText();

        if (!checkIfElementExists(match, ".//button[1]//div[2]/span") || // no odds for this game
                !checkIfElementExists(match, ".//button[2]//div[2]/span") ||
                !checkIfElementExists(match, ".//button[3]//div[2]/span")) {
            return;
        }
        WebElement odds1Span = match.findElement(By.xpath(".//button[1]//div[2]/span"));
        WebElement oddsXSpan = match.findElement(By.xpath(".//button[2]//div[2]/span"));
        WebElement odds2Span = match.findElement(By.xpath(".//button[3]//div[2]/span"));

        float odds1 = formatFloat(odds1Span.getText());
        float oddsX = formatFloat(oddsXSpan.getText());
        float odds2 = formatFloat(odds2Span.getText());

        result.add(new ResultDTO(homeTeamName, awayTeamName, odds1, oddsX, odds2));
    }
}
