package data.BetStrategyMysql;

import data.DTO.FibonacciDTO.FibonacciDTO;
import data.GuiMysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BetStrategyMysql extends GuiMysql {

    private BetStrategyMysql() {
        super();
    }
    private static BetStrategyMysql instance;

    public static BetStrategyMysql getInstance() {
        if (instance == null) {
            instance = new BetStrategyMysql();
        }
        return instance;
    }

    public void addFibonacciBet(FibonacciDTO fibonacciDTO) {
        String sql = "INSERT INTO FibonacciBets (betSequence, matchId, odds, status) VALUES (?, ?, ?, ?)";
        try (PreparedStatement statement = getConnection().prepareStatement(sql);) {
            int sequenceNumber = fibonacciDTO.getSequenceNumber();
            if (sequenceNumber < 0) {
                sequenceNumber = getNextSequenceNumber();
            }
            statement.setInt(1, sequenceNumber);
            statement.setInt(2, fibonacciDTO.getMatchId());
            statement.setFloat(3, fibonacciDTO.getOdds());
            statement.setString(4,"OPEN");
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int getNextSequenceNumber() {
        int sequenceNumber = 0;
        String sql = "SELECT MAX(betSequence) FROM FibonacciBets";
        try (PreparedStatement statement = getConnection().prepareStatement(sql);) {
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                sequenceNumber = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sequenceNumber + 1;
    }

    public float getTotalBetAmountFibonacci(int sequenceNumber) {
        float totalBetAmount = 0;
        String sql = "SELECT SUM(betAmount) FROM FibonacciBets WHERE betSequence = ?";
        try (PreparedStatement statement = getConnection().prepareStatement(sql);) {

            statement.setInt(1, sequenceNumber);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                totalBetAmount = resultSet.getFloat(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return totalBetAmount;
    }

    public List<FibonacciDTO> getAllOpenFibonacciSequences() {
        List<FibonacciDTO> fibonacciDTOList = new ArrayList<>();
        String sql = "SELECT fb.*, ht.name as homeTeamName, awt.name as awayTeamName, sr.gameDate as gameDate FROM FibonacciBets fb JOIN SoccerResults sr ON sr.id = fb.matchId JOIN Team ht on ht.id = sr.homeTeamId JOIN Team awt ON awt.id = sr.awayTeamId WHERE status = ?";
        try (PreparedStatement statement = getConnection().prepareStatement(sql);) {
            statement.setString(1, "OPEN");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                FibonacciDTO fibonacciDTO = new FibonacciDTO();
                fibonacciDTO.setSequenceNumber(resultSet.getInt("betSequence"));
                fibonacciDTO.setMatchId(resultSet.getInt("matchId"));
                fibonacciDTO.setOdds(resultSet.getFloat("odds"));
                fibonacciDTO.setGameDate(resultSet.getString("gameDate"));
                //fibonacciDTO.setBetAmount(resultSet.getFloat("betAmount"));
                fibonacciDTO.setStatus(resultSet.getString("status"));
                fibonacciDTO.setHomeTeamName(resultSet.getString("homeTeamName"));
                fibonacciDTO.setAwayTeamName(resultSet.getString("awayTeamName"));
                fibonacciDTOList.add(fibonacciDTO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fibonacciDTOList;
    }

    public void closeFibonacciSequence(int sequenceNumber) {
        String sql = "UPDATE FibonacciBets SET status = ? WHERE betSequence = ? ";
        try (PreparedStatement statement = getConnection().prepareStatement(sql);) {
            statement.setString(1, "CLOSED");
            statement.setInt(2, sequenceNumber);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
