package com.greinet.tvtotalripper.ui;

import com.greinet.tvtotalripper.crawler.EpisodeWrapper;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;

/**
 * A panel to display the episodes of a season
 */
public class EpisodesRipperPanel {
    /** The JPanel */
    private final JPanel panel;
    /** The list for the episodes */
    private JList<EpisodeWrapper> listPanel;
    /** The list model for the episode list */
    private DefaultListModel<EpisodeWrapper> listModel;
    /** The text field for the currently selected episode */
    private JTextField textField;
    /** The button to start the episode download */
    private final JButton buttonDownload;
    /** The label for the episode count */
    private final JLabel label;
    /** The list of episodes */
    private final List<EpisodeWrapper> episodes;
    /** The button to download all episodes in the list */
    private final JButton buttonDownloadAll;
    
    /**
     * Create a panel to display episodes in a list
     * @param episodes the episodes to display
     */
    public EpisodesRipperPanel(List<EpisodeWrapper> episodes){
        this.episodes = episodes;
        
        panel = new JPanel(false);
        panel.setLayout(new GridBagLayout());
        
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 3;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.4;
        
        textField = new JTextField();
        textField.setEditable(false);
        panel.add(textField,c);
        
        buttonDownload = new JButton("Download");
        c.gridx = 3;
        c.gridwidth = 2;
        c.weightx = 0.1;
        
        panel.add(buttonDownload,c);

        listModel = new DefaultListModel<>();
        episodes.forEach(e -> listModel.addElement(e));
        
        listPanel = new JList<>(listModel);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(listPanel);
        listPanel.setLayoutOrientation(JList.VERTICAL);
        listPanel.setSelectedIndex(0);
        
        listPanel.addListSelectionListener((ListSelectionEvent e) -> {
            textField.setText(listPanel.getSelectedValue().toString());
        });
        textField.setText(listPanel.getSelectedValue().toString());
        
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 5;
        c.gridheight = 5;
        c.weightx = 0.5;
        
        panel.add(scrollPane,c);
        
        label = new JLabel(Integer.toString(episodes.size()));
        label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        
        buttonDownloadAll = new JButton("Download all episodes");
        c.gridx = 6;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 6;
        c.weightx = 0.5;
        panel.add(buttonDownloadAll,c);
        
        
        
        c.gridx = 4;
        c.gridy = 6;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        
        panel.add(label, c);
        
        
        
    }
    /**
     * Get the JPanel
     * @return the JPanel
     */
    public JPanel getJPanel(){
        return panel;
    }
    
    /**
     * Get the currently selecte episode
     * @return 
     */
    public EpisodeWrapper getCurrentSelected(){
        return listPanel.getSelectedValue();
    }
    
    /**
     * Get the download button
     * @return the download button 
     */
    public JButton getDownloadButton(){
        return buttonDownload;
    }
    
    /**
     * Get the download all button
     * @return the download all button
     */
    public JButton getDownloadAllButton(){
        return buttonDownloadAll;
    }
    
}
