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.ArbResults;
import object.Odds;
import object.ResultDTO;

public class BWinParser extends ParserBase {

    public static final String BOOKIE = "BWin";
    String url = "https://sports.bwin.se/sv/sports/fotboll-4/idag";
    String url2 = "https://sports.bwin.se/sv/sports/fotboll-4/imorgon";

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

        ChromeDriver driver = getSeleniumDriver();
        WebDriverWait wait = getWaitDriver(driver);

        parsePage(result, driver, wait, url);
        parsePage(result, driver, wait, url2);

        driver.close();
        return result;
    }

    private void parsePage(List<ResultDTO> result, ChromeDriver driver, WebDriverWait wait, String url) {
        driver.get(url);
        wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//div[@class='calendar-event-info']"), 0));

        List<WebElement> matchDivs = driver.findElements(By.xpath("//div[@class='calendar-event-info']"));

        for (WebElement matchDiv : matchDivs) {
            List<WebElement> participants = matchDiv.findElements(By.xpath(".//div[@class='participant']"));
            if (participants.size() != 2) {
                System.out.println("Not correct number of participants " + participants.size()
                        + (participants.size() > 0 ? " first is " + participants.get(0).getText() : ""));
                continue;
            }
            String homeTeam = participants.get(0).getText();
            String awayTeam = participants.get(1).getText();

            List<Odds> matchOdds = getMatchOdds(matchDiv);
            List<Odds> overUnderOdds = getOverUnderOdds(matchDiv);

            if (matchOdds.size() == 3 && overUnderOdds.size() == 2) {
                result.add(new ArbResults(homeTeam, awayTeam, matchOdds.get(0).getOdds(), matchOdds.get(1).getOdds(),
                        matchOdds.get(2).getOdds(),
                        overUnderOdds.get(0).getOdds(), overUnderOdds.get(1).getOdds(), BOOKIE));
                System.out.println("Added " + result.get(result.size() - 1) + " with 2.5 over under");
            } else if (matchOdds.size() == 3 && overUnderOdds.size() != 2) {
                result.add(new ArbResults(homeTeam, awayTeam, matchOdds.get(0).getOdds(), matchOdds.get(1).getOdds(),
                        matchOdds.get(2).getOdds(), BOOKIE));
                System.out.println("Added " + result.get(result.size() - 1) + " WITHOUT 2.5 over under");
            } else {
                System.out.println("Results not added for matchDiv " + matchDiv.toString());
            }
        }
    }

    private List<Odds> getMatchOdds(WebElement matchDiv) {
        List<Odds> result = new ArrayList<>();
        if (checkIfElementExists(matchDiv, ".//ms-option-group[1]")) {
            WebElement oddsContainer = matchDiv.findElement(By.xpath(".//ms-option-group[1]"));

            List<WebElement> odds = oddsContainer.findElements(By.xpath(".//ms-font-resizer"));

            if (odds.size() == 3) {
                result.add(new Odds(BOOKIE, formatFloat(odds.get(0).getText())));
                result.add(new Odds(BOOKIE, formatFloat(odds.get(1).getText())));
                result.add(new Odds(BOOKIE, formatFloat(odds.get(2).getText())));
            }
        }
        return result;
    }

    private List<Odds> getOverUnderOdds(WebElement matchDiv) {
        List<Odds> result = new ArrayList<>();
        if (checkIfElementExists(matchDiv, ".//ms-option-group[2]")) {
            WebElement oddsContainer = matchDiv.findElement(By.xpath(".//ms-option-group[2]"));

            List<WebElement> odds = oddsContainer.findElements(By.xpath(".//ms-font-resizer"));

            if (odds.size() == 3) {
                if (formatFloat(odds.get(0).getText().replace(",", ".")) == 2.5f) {
                    result.add(new Odds(BOOKIE, formatFloat(odds.get(1).getText())));
                    result.add(new Odds(BOOKIE, formatFloat(odds.get(2).getText())));
                }
            }
        }
        return result;
    }
}
