package components.matchtable;

import events.MatchSelectedEvent;
import objects.SoccerMatch;
import objects.SoccerMatchAnalysis;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.*;
import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.PackageResourceReference;

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

public class MatchTable extends Panel {

    List<IColumn<SoccerMatchAnalysis, String>> tableColumns = new ArrayList<>();

    public MatchTable(String id, List<SoccerMatch> matches) {
        super(id);

        List<SoccerMatchAnalysis> matchAnalysis = matches.stream().map(SoccerMatchAnalysis::new).collect(Collectors.toList());
        matchAnalysis.forEach(m -> m.calculateWinPercentages());

        buildMatchesTable();

        add(new Label("matchesCount", "Matches count: " + matchAnalysis.size()));

        populateMatchesTable(matchAnalysis);
    }

    @Override
    public void renderHead(IHeaderResponse response) {
        super.renderHead(response);
        PackageResourceReference cssFile = new PackageResourceReference(this.getClass(), "MatchTable.css");
        CssHeaderItem cssItem = CssHeaderItem.forReference(cssFile);

        response.render(cssItem);
    }

    private void populateMatchesTable(List<SoccerMatchAnalysis> todaysMatchAnalysis) {

        todaysMatchAnalysis.sort((m1, m2) ->
                Float.compare(m2.getCalculatedPercentages().getDrawPercentage(),
                        m1.getCalculatedPercentages().getDrawPercentage()));
        SortableSoccerMatchAnalysisDataProvider dataProvider = new SortableSoccerMatchAnalysisDataProvider(todaysMatchAnalysis);

        DataTable<SoccerMatchAnalysis, String> dataTable = new DataTable<>("matchesTable", tableColumns, dataProvider, 10) {
            @Override
            protected Item<SoccerMatchAnalysis> newRowItem(String id, int index, IModel<SoccerMatchAnalysis> model) {
                Item<SoccerMatchAnalysis> item = super.newRowItem(id, index, model);
                item.add(new AjaxEventBehavior("click") {
                    @Override
                    protected void onEvent(AjaxRequestTarget target) {
                        send(getPage(), Broadcast.BREADTH, new MatchSelectedEvent(item.getModelObject()));
                    }
                });
                return item;
            }
        };

        dataTable.addTopToolbar(new HeadersToolbar<>(dataTable, dataProvider));

        add(dataTable);
    }

    private void buildMatchesTable() {
        tableColumns.add(new LambdaColumn<>(Model.of("Match"), "toString",  s -> "(" +s.getMatchId() + ") " +  s.toString()));
        tableColumns.add(new PropertyColumn<>(Model.of("Odds 1"), "odds1", "odds1"));
        tableColumns.add(new PropertyColumn<>(Model.of("Odds X"), "oddsX", "oddsX"));
        tableColumns.add(new PropertyColumn<>(Model.of("Odds 2"), "odds2", "odds2"));

        tableColumns.add(new AbstractColumn<>(Model.of("Calculated Percent 1"), "calculatedPercentages.homeWinPercentage") {

            @Override
            public void populateItem(Item<ICellPopulator<SoccerMatchAnalysis>> cellItem, String componentId,
                    IModel<SoccerMatchAnalysis> model) {
                cellItem.add(new Label(componentId, model.getObject().getCalculatedPercentages().getHomeWinPercentage()));
            }
        });
        tableColumns.add(new AbstractColumn<>(Model.of("Calculated Percent X"), "calculatedPercentages.drawPercentage") {

            @Override
            public void populateItem(Item<ICellPopulator<SoccerMatchAnalysis>> cellItem, String componentId,
                    IModel<SoccerMatchAnalysis> model) {
                cellItem.add(new Label(componentId, model.getObject().getCalculatedPercentages().getDrawPercentage()));
            }
        });
        tableColumns.add(new AbstractColumn<>(Model.of("Calculated Percent 2"), "calculatedPercentages.awayWinPercentage") {

            @Override
            public void populateItem(Item<ICellPopulator<SoccerMatchAnalysis>> cellItem, String componentId,
                    IModel<SoccerMatchAnalysis> model) {
                cellItem.add(new Label(componentId, model.getObject().getCalculatedPercentages().getAwayWinPercentage()));
            }


        });
    }

}
