package org.helioviewer.jhv.timelines.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.HashSet;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.helioviewer.jhv.gui.components.calendar.JHVCalendarDatePicker;
import org.helioviewer.jhv.layers.Movie;
import org.helioviewer.jhv.time.TimeUtils;
import org.helioviewer.jhv.timelines.TimelineLayer;
import org.helioviewer.jhv.timelines.TimelineLayers;
import org.helioviewer.jhv.timelines.Timelines;
import org.helioviewer.jhv.timelines.band.Band;
import org.helioviewer.jhv.timelines.band.BandType;
import org.helioviewer.jhv.timelines.draw.DrawController;

@SuppressWarnings("serial")
public class TimelineDataPanel extends JPanel {

    private final JHVCalendarDatePicker calendarStartDate = new JHVCalendarDatePicker();
    private final JComboBox<String> comboBoxGroup = new JComboBox<>();
    private final JComboBox<BandType> comboBoxData = new JComboBox<>();

    private boolean userSet;

    public TimelineDataPanel() {
        setLayout(new GridBagLayout());

        comboBoxGroup.addActionListener(e -> updateGroupValues());
        calendarStartDate.addJHVCalendarListener(e -> userSet = true);

        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;

        c.gridy = 0;
        c.gridx = 0;
        add(new JLabel("Start date", JLabel.RIGHT), c);
        c.gridx = 1;
        add(calendarStartDate, c);

        c.gridy = 1;
        c.gridx = 0;
        add(new JLabel("Group", JLabel.RIGHT), c);
        c.gridx = 1;
        add(comboBoxGroup, c);

        c.gridy = 2;
        c.gridx = 0;
        add(new JLabel("Dataset", JLabel.RIGHT), c);
        c.gridx = 1;
        add(comboBoxData, c);
    }

    public void setupDatasets() {
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(BandType.getGroups());
        if (model.getSize() > 0) {
            comboBoxGroup.setModel(model);
            comboBoxGroup.setSelectedIndex(0);
        }
    }

    void updateGroupValues() {
        if (!userSet)
            calendarStartDate.setTime(Movie.getStartTime());

        String selectedGroup = (String) comboBoxGroup.getSelectedItem();
        if (selectedGroup == null)
            return;

        HashSet<BandType> bandTypesInSelectorModel = new HashSet<>();
        for (TimelineLayer tl : TimelineLayers.get()) {
            if (tl instanceof Band)
                bandTypesInSelectorModel.add(((Band) tl).getBandType());
        }

        DefaultComboBoxModel<BandType> model = new DefaultComboBoxModel<>();
        for (BandType value : BandType.getBandTypes(selectedGroup)) {
            if (!bandTypesInSelectorModel.contains(value))
                model.addElement(value);
        }

        if (model.getSize() > 0) {
            comboBoxData.setModel(model);
            comboBoxData.setSelectedIndex(0);
        }
    }

    void loadButtonPressed() {
        BandType bandType = (BandType) comboBoxData.getSelectedItem();
        if (bandType == null)
            return;

        Timelines.getLayers().add(new Band(bandType));

        long time = calendarStartDate.getTime();
        long movieStart = Movie.getStartTime();
        long movieEnd = Movie.getEndTime();
        if (time >= movieStart && time <= movieEnd) {
            DrawController.setSelectedInterval(movieStart, movieEnd);
        } else {
            long now = System.currentTimeMillis();
            DrawController.setSelectedInterval(Math.min(time, now), Math.min(time + 2 * TimeUtils.DAY_IN_MILLIS, now));
        }
    }

}

@@ -262,166 +262,8 @@
 l;%0A%0A
-import org.helioviewer.jhv.gui.components.calendar.JHVCalendarDatePicker;%0Aimport org.helioviewer.jhv.layers.Movie;%0Aimport org.helioviewer.jhv.time.TimeUtils;%0A
 impo
@@ -310,16 +310,16 @@
 eLayer;%0A
+
 import o
@@ -514,66 +514,8 @@
 ype;
-%0Aimport org.helioviewer.jhv.timelines.draw.DrawController;
 %0A%0A@S
@@ -593,97 +593,8 @@
  %7B%0A%0A
-    private final JHVCalendarDatePicker calendarStartDate = new JHVCalendarDatePicker();%0A
     
@@ -737,38 +737,8 @@
 );%0A%0A
-    private boolean userSet;%0A%0A
     
@@ -802,25 +802,24 @@
 gLayout());%0A
-%0A
         comb
@@ -876,79 +876,8 @@
 ());
-%0A        calendarStartDate.addJHVCalendarListener(e -%3E userSet = true);
 %0A%0A  
@@ -1093,163 +1093,8 @@
  0;%0A
-        add(new JLabel(%22Start date%22, JLabel.RIGHT), c);%0A        c.gridx = 1;%0A        add(calendarStartDate, c);%0A%0A        c.gridy = 1;%0A        c.gridx = 0;%0A
     
@@ -1211,17 +1211,17 @@
 gridy = 
-2
+1
 ;%0A      
@@ -1652,92 +1652,8 @@
 ) %7B%0A
-        if (!userSet)%0A            calendarStartDate.setTime(Movie.getStartTime());%0A%0A
     
@@ -2565,33 +2565,32 @@
         return;%0A
-%0A
         Timeline
@@ -2631,472 +2631,8 @@
 e));
-%0A%0A        long time = calendarStartDate.getTime();%0A        long movieStart = Movie.getStartTime();%0A        long movieEnd = Movie.getEndTime();%0A        if (time %3E= movieStart && time %3C= movieEnd) %7B%0A            DrawController.setSelectedInterval(movieStart, movieEnd);%0A        %7D else %7B%0A            long now = System.currentTimeMillis();%0A            DrawController.setSelectedInterval(Math.min(time, now), Math.min(time + 2 * TimeUtils.DAY_IN_MILLIS, now));%0A        %7D
 %0A   

Remove date selection from timelines dialog