import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class ResponsesTab extends application{
    
    public JPanel ResponsesTabPanel;
    public Browser DirectoryChooser;
    private JPanel SortingPanel;
    private JPanel ExportPanel;
    
    private JLabel SortingLabel;
    private JRadioButton[] SortingOptions;
    private ButtonGroup SortingOptionsGroup;
    private JLabel DestinationLabel;
    public JTextField DestinationField;
    private JButton ExportButton, BrowseButton;
    
    public ResponsesTab(){
        
        ResponsesTabPanel = new JPanel();
        GridLayout layout = new GridLayout(2, 1);
        ResponsesTabPanel.setLayout(new GridBagLayout());
        
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        
        
        setupSortingPanel();
        ResponsesTabPanel.add(SortingPanel, c);
        
        c.gridy = 1;
        
        setupExportPanel();
        ResponsesTabPanel.add(ExportPanel, c);
    }
    
    public void setupSortingPanel(){
        
        SortingPanel = new JPanel();
        SortingPanel.setPreferredSize(new Dimension(600, 150));
        SortingPanel.setLayout(new GridLayout(4, 1));
        SortingPanel.setBorder(new TitledBorder("Sorting"));
        
        SortingLabel = new JLabel("Sort responses by");
        SortingPanel.add(SortingLabel);
        
        SortingOptions = new JRadioButton[3];
        SortingOptionsGroup = new ButtonGroup();
        
        SortingOptions[0] = new JRadioButton("Name");
        SortingOptions[1] = new JRadioButton("Lastname");
        SortingOptions[2] = new JRadioButton("Date");
        
        for (int r= 0; r<SortingOptions.length; r++){
            SortingPanel.add(SortingOptions[r]);
            SortingOptionsGroup.add(SortingOptions[r]);
        }
        
        SortingOptions[2].setSelected(true);
        
    }
    
    public void setupExportPanel(){
        
        ExportPanel = new JPanel();
        GridLayout layout = new GridLayout(2, 3);
        layout.setVgap(5);
        ExportPanel.setLayout(layout);
        ExportPanel.setPreferredSize(new Dimension(600, 75));
        ExportPanel.setBorder(new TitledBorder("Export responses to CSV"));
        
        DestinationLabel = new JLabel("Destination");
        ExportPanel.add(DestinationLabel);
        
        DestinationField = new JTextField(40);
        DestinationField.setToolTipText("Enter the absolute path to the directory where you want to export the responses");
        ExportPanel.add(DestinationField);
        
        BrowseButton = new JButton("Browse...");
        BrowseButton.setToolTipText("Opens default file browser");
        BrowseButton.addActionListener(new BrowseButtonHandler());
        ExportPanel.add(BrowseButton);
        
        ExportPanel.add(new JLabel(""));
        
        ExportButton = new JButton("Export");
        ExportButton.addActionListener(new ExportButtonHandler());
        ExportPanel.add(ExportButton);
        
        ExportPanel.add(new JLabel(""));
    }
    
    private int getSortType(){
        int r;
        for (r = 0; r < SortingOptions.length; r++)
            if (SortingOptions[r].isSelected())
                break;
        return r;
    }
    
    public void DirectoryChooserHandler(String DirectoryPath){
        DestinationField.setText(DirectoryPath);
    }
    
    private class BrowseButtonHandler implements ActionListener{
        
        public void actionPerformed(ActionEvent event){
            DirectoryChooser = new Browser(true);
        }
    }
    
    private class ExportButtonHandler implements ActionListener{
        
        public void actionPerformed(ActionEvent event){
            JOptionPane AlertMessage = new JOptionPane();
            responses rsp = new responses();
            if (DestinationField.getText().compareTo("") == 0)
                AlertMessage.showMessageDialog(null, "Please enter the path where to export!");
            else if (rsp.areResponsesClear())
                AlertMessage.showMessageDialog(null, "Responses file is empty, impossible to export!");
            else{
                try{
                    rsp.exportAnswers(DestinationField.getText(), getSortType());
                    AlertMessage.showMessageDialog(null, "Successfully exported to "+DestinationField.getText()+"/responses.csv");
                }
                catch (Exception e){
                    System.out.println(e.toString());
                }
            }
            
        }
    }
    
}
