package com.greinet.tvtotalripper.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
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;

/**
 * The base panel for show and episode visualization
 */
public class BaseRipperPanel {
    
    /** The JPanel */
    private final JPanel panel;
    /** The Jlist for the elements */
    private JList<String> listPanel;
    /** The text field for the currently selected element */
    private JTextField textField;
    /** The button to select the currently selected element */
    private final JButton button;
    /** The label for the element count */
    private final JLabel label;
    /** The map for the elements */
    private final Map<String, String> elements;
    
    /**
     * Create a base panel the display elements and select a specific one
     * @param elements the elements
     */
    public BaseRipperPanel(Map<String, String> elements){
        this.elements = elements;
        
        if(elements.isEmpty()){
            elements.put("Dummy-Key", "Dummy-URL");
        }
        
        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.75;
        
        textField = new JTextField();
        textField.setEditable(false);
        panel.add(textField,c);
        
        button = new JButton("Select");
        c.gridx = 3;
        c.gridwidth = 2;
        c.weightx = 0.25;
        
        panel.add(button,c);

        
        List<String> names = new ArrayList<>(elements.keySet());
        Collections.sort(names);
        
        listPanel = new JList<>(names.toArray(new String[elements.keySet().size()]));
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(listPanel);
        listPanel.setLayoutOrientation(JList.VERTICAL);
        listPanel.setSelectedIndex(0);
        
        listPanel.addListSelectionListener((ListSelectionEvent e) -> {
            textField.setText(listPanel.getSelectedValue());
        });
        textField.setText(listPanel.getSelectedValue());
        
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 5;
        c.gridheight = 5;
        c.weightx = 0;
        
        panel.add(scrollPane,c);
        
        label = new JLabel(Integer.toString(elements.keySet().size()));
        label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        
        
        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 selection JButton
     * @return the JButton
     */
    public JButton getJButton(){
        return button;
    }
    
    /**
     * Get the String representing the currently selected element
     * @return the selected element
     */
    public String getCurrentSelected(){
        return elements.get(listPanel.getSelectedValue());
    }
    
    /**
     * Get the Jlist of the elements
     * @return the JList
     */
    public JList getJList(){
        return listPanel;
    }
    
}
