package components.newBetForm;

import data.BetStrategyMysql.BetStrategyMysql;
import data.DTO.FibonacciDTO.FibonacciDTO;
import events.MatchSelectedEvent;
import objects.SoccerMatchAnalysis;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import java.util.List;
import java.util.stream.Collectors;

public class NewBetForm extends Panel {
    TextField<Integer> matchIdText;
    TextField<Float> betAmount;
    TextField<Float> odds;
    DropDownChoice<String> sequenceSelect;
    Label teamText;
    Label oddsText;

    public NewBetForm(String id) {
        super(id);

        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        add(feedbackPanel);

        Form<?> form = new Form<Void>("form") {
            @Override
            protected void onSubmit() {
                FibonacciDTO fibonacciDTO = new FibonacciDTO();
                fibonacciDTO.setMatchId(matchIdText.getModelObject());
                try {
                    fibonacciDTO.setSequenceNumber(Integer.parseInt(sequenceSelect.getConvertedInput()));
                } catch (NumberFormatException e) {
                    fibonacciDTO.setSequenceNumber(BetStrategyMysql.getInstance().getNextSequenceNumber());
                }
                fibonacciDTO.setOdds(odds.getModelObject());
                fibonacciDTO.setBetAmount(betAmount.getModelObject());

                BetStrategyMysql.getInstance().addFibonacciBet(fibonacciDTO);
            }

            @Override
            protected void onError() {
                System.out.println("ERROR Form error");
                super.onError();
            }
        };

        //Label betSequencesLabel = new Label("betSequencesLabel", "Bet sequences");
        List<FibonacciDTO> allOpenFibonacciSequences = BetStrategyMysql.getInstance().getAllOpenFibonacciSequences();
        List<String> sequenceNumbers = allOpenFibonacciSequences.stream().map(FibonacciDTO::getSequenceNumber).map(Object::toString).distinct().collect(Collectors.toList());
        if (sequenceNumbers.size() < 4) {
            sequenceNumbers.add("New bet sequence");
        }

        sequenceSelect = new DropDownChoice<>("betSequences", Model.of("Bet Sequence"), sequenceNumbers);

        // Label betOddsLabel = new Label("betOddsLabel", "Odds");
        odds = new TextField<>("betOdds", Model.of(), Float.class);
        odds.setRequired(true);

        // Label betAmountLabel = new Label("betAmountLabel", "Bet Amount");
        betAmount = new TextField<>("betAmount", Model.of(), Float.class);
        betAmount.setRequired(true);
        Button submit = new Button("submit", Model.of("Submit"));

        teamText = new Label("teams");
        teamText.setOutputMarkupId(true);
        oddsText = new Label("calculatedOdds");
        oddsText.setOutputMarkupId(true);
        matchIdText = new TextField<>("matchId", Model.of(), Integer.class);
        matchIdText.setOutputMarkupId(true);

        form.add(matchIdText, sequenceSelect, odds, betAmount, submit);
        add(teamText);
        add(oddsText);
        add(form);
    }

    @Override
    public void onEvent(IEvent<?> event) {
        if (event.getPayload() instanceof MatchSelectedEvent) {
            SoccerMatchAnalysis match = ((MatchSelectedEvent) event.getPayload()).getSelectedMatch();

            teamText.setDefaultModel(Model.of(match.getHomeTeamName() + " vs " + match.getAwayTeamName()));
            oddsText.setDefaultModel(Model.of(match.getCalculatedPercentages().getDrawPercentage()));

            matchIdText.setDefaultModel(Model.of(match.getMatchId()));

            getPage().renderPage();
        }

        event.stop();
    }
}
