package object;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class ArbResults extends ResultDTO {

    List<Odds> odds1List = new ArrayList<>();
    List<Odds> oddsXList = new ArrayList<>();
    List<Odds> odds2List = new ArrayList<>();

    List<Odds> over25List = new ArrayList<>();
    List<Odds> under25List = new ArrayList<>();

    Odds over25;
    Odds under25;

    String bookie;

    public ArbResults(String homeTeamName, String awayTeamName, float odds1, float oddsX, float odds2, String bookie) {
        super(homeTeamName, awayTeamName, odds1, oddsX, odds2);

        odds1List.add(new Odds(bookie, odds1));
        oddsXList.add(new Odds(bookie, oddsX));
        odds2List.add(new Odds(bookie, odds2));
        this.bookie = bookie;
    }

    public ArbResults(ResultDTO m, String bookie) {
        this(m.homeTeam, m.awayTeam, m.odds1, m.oddsX, m.odds2, m.getOddsOver25goals(), m.getOddsUnder25goals(),
                bookie);
    }

    public ArbResults(String homeTeamName, String awayTeamName, float odds1, float oddsX, float odds2,
            float over25, float under25, String bookie) {
        this(homeTeamName, awayTeamName, odds1, oddsX, odds2, bookie);
        this.over25 = new Odds(bookie, over25);
        this.under25 = new Odds(bookie, under25);

        over25List.add(this.over25);
        under25List.add(this.under25);

        this.oddsOver25goals = over25;
        this.oddsUnder25goals = under25;

    }

    public void addOdds1(String bookie, float value) {
        odds1List.add(new Odds(bookie, value));
    }

    public void addOddsX(String bookie, float value) {
        oddsXList.add(new Odds(bookie, value));
    }

    public void addOdds2(String bookie, float value) {
        odds2List.add(new Odds(bookie, value));
    }

    public Odds getMaxOdds1() {
        Float val = Collections.max(odds1List.stream().map(m -> m.odds).toList());
        Optional<Odds> o = odds1List.stream().filter(p -> p.odds == val).findFirst();

        return o.isPresent() ? o.get() : null;
    }

    public Odds getMaxOddsX() {
        Float val = Collections.max(oddsXList.stream().map(m -> m.odds).toList());
        Optional<Odds> o = oddsXList.stream().filter(p -> p.odds == val).findFirst();

        return o.isPresent() ? o.get() : null;
    }

    public Odds getMaxOdds2() {
        Float val = Collections.max(odds2List.stream().map(m -> m.odds).toList());
        Optional<Odds> o = odds2List.stream().filter(p -> p.odds == val).findFirst();

        return o.isPresent() ? o.get() : null;
    }

    public boolean multipleOddsExist() {
        return ((odds1List.size() > 1 || oddsXList.size() > 1 || odds2List.size() > 1)
                && (!getMaxOdds1().bookie.equals(getMaxOdds2().bookie)
                        || !getMaxOdds1().bookie.equals(getMaxOddsX().bookie)));
    }

    public String toStringWithExtra() {
        String string = super.toString();
        string += "(" + getMaxOdds1() + "," + getMaxOddsX() + "," + getMaxOdds2() + ")";
        return string;
    }

    public float getOver25() {
        return this.oddsOver25goals;
    }

    public float getUnder25() {
        return this.oddsUnder25goals;
    }

    public void setOver25(Odds over25) {
        this.over25 = over25;
    }

    public void setUnder25(Odds under25) {
        this.under25 = under25;
    }

    public void addOver25Odds(String bookie, float odds) {
        this.over25List.add(new Odds(bookie, odds));
    }

    public void addUnder25Odds(String bookie, float odds) {
        this.under25List.add(new Odds(bookie, odds));
    }

    public Odds getMaxOver25() {
        Float val = Collections.max(over25List.stream().map(m -> m.odds).toList());
        Optional<Odds> o = over25List.stream().filter(p -> p.odds == val).findFirst();

        return o.isPresent() ? o.get() : null;
    }

    public Odds getMaxUnder25() {
        Float val = Collections.max(under25List.stream().map(m -> m.odds).toList());
        Optional<Odds> o = under25List.stream().filter(p -> p.odds == val).findFirst();

        return o.isPresent() ? o.get() : null;
    }

    public float getArbValueOverUnder() {

        Odds maxOver25 = getMaxOver25();
        Odds maxUnder25 = getMaxUnder25();

        return (1 / maxOver25.odds) + (1 / maxUnder25.odds);
    }

}
