package eu.dnetlib.client.statistics;

import com.github.gwtbootstrap.client.ui.Form;
import com.github.gwtbootstrap.client.ui.ListBox;
import com.github.gwtbootstrap.client.ui.RadioButton;
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import eu.dnetlib.client.widgets.FormFieldSet;
import eu.dnetlib.goldoa.domain.stats.Browse;
import eu.dnetlib.goldoa.domain.stats.DateSeries;
import eu.dnetlib.goldoa.domain.stats.Triple;

import java.util.Date;
import java.util.List;

/**
 * Created by stefania on 11/29/15.
 */
public class DateSeriesToolbarWidget implements IsWidget {

    private FlowPanel dateSeriesToolbarPanel = new FlowPanel();

    private Form dateSeriesOptionsForm = new Form();

    private ListBox predefinedPeriodsListBox = new ListBox();

    private RadioButton dayRadio = new RadioButton("groupByTime", "Day", false);
    private RadioButton monthRadio = new RadioButton("groupByTime", "Month", false);
    private RadioButton yearRadio = new RadioButton("groupByTime", "Year", false);

    private ListBox countriesListBox = new ListBox();
    private ListBox publishersListBox = new ListBox();
    private ListBox organisationsListBox = new ListBox();
    private ListBox scientificAreasListBox = new ListBox();

    private SelectionChangedListener selectionChangedListener;

    public  DateSeriesToolbarWidget(Browse browse) {

        dateSeriesToolbarPanel.add(dateSeriesOptionsForm);

        dateSeriesOptionsForm.add(new HTML("<div class=\"formHeaderLabel\">Group requests by</div>"));

        dayRadio.setValue(true);
        dayRadio.addStyleName("inlineBlock");
        dayRadio.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
                fireSelectionChangedEvent();
            }
        });

        monthRadio.addStyleName("inlineBlock");
        monthRadio.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
                fireSelectionChangedEvent();
            }
        });

        yearRadio.addStyleName("inlineBlock");
        yearRadio.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
                fireSelectionChangedEvent();
            }
        });

        dateSeriesOptionsForm.add(new FormFieldSet(null, dayRadio, monthRadio, yearRadio));

        dateSeriesOptionsForm.add(new HTML("<div class=\"formHeaderLabel marginTop10 marginBottom10\">Show requests for the</div>"));

        predefinedPeriodsListBox.addItem("whole pilot duration", "all");
        predefinedPeriodsListBox.addItem("last 1 month", "last1Month");
        predefinedPeriodsListBox.addItem("last 3 months", "last3Months");
        predefinedPeriodsListBox.addItem("last 6 months", "last6Months");
        predefinedPeriodsListBox.addItem("last 12 months", "last12Months");
        predefinedPeriodsListBox.setAlternateSize(AlternateSize.LARGE);
        predefinedPeriodsListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
                fireSelectionChangedEvent();
            }
        });
        dateSeriesOptionsForm.add(new FormFieldSet(null, predefinedPeriodsListBox));

        dateSeriesOptionsForm.add(new HTML("<div class=\"formHeaderLabel marginTop10 marginBottom10\">Filter requests by</div>"));

        List<Triple<String, String, Integer>> countries =  browse.getData(Browse.Category.COUNTRY);
        countriesListBox.addItem("All", "all");
        for(Triple<String, String, Integer> country :  countries) {
            countriesListBox.addItem(country.getSecond(), country.getFirst());
        }
        countriesListBox.setAlternateSize(AlternateSize.LARGE);
        countriesListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
                fireSelectionChangedEvent();
            }
        });
        dateSeriesOptionsForm.add(new FormFieldSet("Country", countriesListBox));

        List<Triple<String, String, Integer>> publishers =  browse.getData(Browse.Category.PUBLISHER);
        publishersListBox.addItem("All", "all");
        for(Triple<String, String, Integer> publisher :  publishers) {
            publishersListBox.addItem(publisher.getSecond(), publisher.getFirst());
        }
        publishersListBox.setAlternateSize(AlternateSize.LARGE);
        publishersListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
                fireSelectionChangedEvent();
            }
        });
        dateSeriesOptionsForm.add(new FormFieldSet("Publisher", publishersListBox));

        List<Triple<String, String, Integer>> organisations =  browse.getData(Browse.Category.ORGANIZATION);
        organisationsListBox.addItem("All", "all");
        for(Triple<String, String, Integer> organisation :  organisations) {
            organisationsListBox.addItem(organisation.getSecond(), organisation.getFirst());
        }
        organisationsListBox.setAlternateSize(AlternateSize.LARGE);
        organisationsListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
                fireSelectionChangedEvent();
            }
        });
        dateSeriesOptionsForm.add(new FormFieldSet("Organisation", organisationsListBox));

        List<Triple<String, String, Integer>> scientificAreas =  browse.getData(Browse.Category.SCIENTIFIC_AREA);
        scientificAreasListBox.addItem("All", "all");
        for(Triple<String, String, Integer> scientificArea :  scientificAreas) {
            scientificAreasListBox.addItem(scientificArea.getSecond(), scientificArea.getFirst());
        }
        scientificAreasListBox.setAlternateSize(AlternateSize.LARGE);
        scientificAreasListBox.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent changeEvent) {
                fireSelectionChangedEvent();
            }
        });
        dateSeriesOptionsForm.add(new FormFieldSet("Scientific Area", scientificAreasListBox));

    }

    @Override
    public Widget asWidget() {
        return dateSeriesToolbarPanel;
    }

    public interface SelectionChangedListener {
        void selectionChanged(DateSeries.Over over, Date from, Date to, String[] publishers, String[] organizations, String[] countries, String[] scientificAreas);
    }

    public void setSelectionChangedListener(SelectionChangedListener selectionChangedListener) {
        this.selectionChangedListener = selectionChangedListener;
    }

    private void fireSelectionChangedEvent() {

        DateSeries.Over over = null;
        if(dayRadio.getValue())
            over = DateSeries.Over.DAY;
        else if(monthRadio.getValue())
            over = DateSeries.Over.MONTH;
        else
            over = DateSeries.Over.YEAR;

        Date dateTo = null;
        Date dateFrom = null;
        if(predefinedPeriodsListBox.getValue().equals("last1Month")) {

            dateTo = new Date();
            dateFrom = new Date();
            CalendarUtil.addMonthsToDate(dateFrom, -1);

        } else if(predefinedPeriodsListBox.getValue().equals("last3Months")) {

            dateTo = new Date();
            dateFrom = new Date();
            CalendarUtil.addMonthsToDate(dateFrom, -3);

        } else if(predefinedPeriodsListBox.getValue().equals("last6Months")) {

            dateTo = new Date();
            dateFrom = new Date();
            CalendarUtil.addMonthsToDate(dateFrom, -6);

        } else if(predefinedPeriodsListBox.getValue().equals("last12Months")) {

            dateTo = new Date();
            dateFrom = new Date();
            CalendarUtil.addMonthsToDate(dateFrom, -12);

        }

        String[] publishers = null;
        if(!publishersListBox.getValue().equals("all")) {
            publishers = new String[1];
            publishers[0] = publishersListBox.getValue();
        }

        String[] organisations = null;
        if(!organisationsListBox.getValue().equals("all")) {
            organisations = new String[1];
            organisations[0] = organisationsListBox.getValue();
        }

        String[] countries = null;
        if(!countriesListBox.getValue().equals("all")) {
            countries = new String[1];
            countries[0] = countriesListBox.getValue();
        }

        String[] scientificAreas = null;
        if(!scientificAreasListBox.getValue().equals("all")) {
            scientificAreas = new String[1];
            scientificAreas[0] = scientificAreasListBox.getValue();
        }

        if(selectionChangedListener!=null)
            selectionChangedListener.selectionChanged(over, dateFrom, dateTo, publishers, organisations, countries, scientificAreas);
    }
}
