package be.msyben.vinylManager.tools.swing;

import be.msyben.vinylManager.tools.Sort;
import java.awt.Point;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Combobox personnalisée permettant une suggestion.
 * N'est pas utilisé.
 * @deprecated 
 * @author Mathieu Syben
 */
@Deprecated
public class SuggestionComboBox extends JComponent {

     /** uid     */
    private static final long serialVersionUID = 7855378466L;

    /** Le champs de 'recherche'. */
    private JTextField field;
    /** La liste des suggestions. */
    private JList<String> suggestions;
    /** Le scrollpanel dans lequel se trouve la liste. */
    private JScrollPane suggestionsPane;
    /** Le panel global. */
    private JPanel panel;
    /** La largeur */
    private int width = 0;
    /** La hauteur du champs texte */
    private int textFieldHeight = 0;
    /** La hauteur de la liste. */
    private int listHeight = 0;
    /** Indique si on montre la liste */
    private boolean showSuggestions = false;
    /** Toutes les suggestions possibles */
    private List<String> allSuggestions;

    /** Constructeur privé. */
    private SuggestionComboBox() {
    }

    /**
     * Constructeur.
     * @param allSuggestions Toutes les suggestions.
     */
    public SuggestionComboBox(List<String> allSuggestions) {
        Sort.sort(allSuggestions);
        this.allSuggestions = allSuggestions;

        placeComponents();
    }

    /**
     * Place les éléments.
     */
    private void placeComponents() {
        panel = new JPanel(null);
        field = new JTextField();
        suggestions = new MyJList<>();
        //suggestions.setS
        panel.add(field);
        suggestionsPane = new JScrollPane(suggestions);
        panel.add(suggestionsPane);

        suggestions.addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent me) {
                Point p = new Point(me.getX(), me.getY());
                int index = suggestions.locationToIndex(p);
                suggestions.setSelectedIndex(index);
            }
        });

        field.addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {

                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        int sel = suggestions.getSelectedIndex();
                        if (sel > 0) {
                            suggestions.setSelectedIndex(sel - 1);
                        }

                        double position = ((sel - 1) * 1.0) / (suggestions.getModel().getSize() * 1.0);
                        int positionNeeded = (int) (suggestionsPane.getVerticalScrollBar().getMaximum() * position);
                        suggestionsPane.getVerticalScrollBar().setValue(positionNeeded);

                        break;
                    case KeyEvent.VK_DOWN:
                        sel = suggestions.getSelectedIndex();
                        if (sel < suggestions.getModel().getSize() - 1) {
                            suggestions.setSelectedIndex(sel + 1);
                        }

                        position = ((sel + 1) * 1.0) / (suggestions.getModel().getSize() * 1.0);
                        positionNeeded = (int) (suggestionsPane.getVerticalScrollBar().getMaximum() * position);
                        suggestionsPane.getVerticalScrollBar().setValue(positionNeeded);

                        break;
                    case KeyEvent.VK_ENTER:
                        hideSuggestions();
                        break;
                    default:
                        if (field.getText() != null && field.getText().length() > 0) {
                            showSuggestions();
                        } else {
                            hideSuggestions();
                        }
                }
            }
        });

        field.addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(FocusEvent e) {
                hideSuggestions();
            }
        });



        suggestions.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {

                if (suggestions.getSelectedIndex() != -1) {
                    String s = suggestions.getSelectedValue();
                    field.setText(s);
                }
            }
        });

        add(panel);
    }

    /**
     * Calcule les suggestions correspondant à la recherche et les mets dans la liste.
     */
    private void computeSuggestions() {
        String cont = field.getText().toLowerCase();
        List<String> okValues = new ArrayList<>();
        for (String s : allSuggestions) {
            if (s.toLowerCase().contains(cont)) {
                okValues.add(s);
            }
        }
        suggestions.setListData(okValues.toArray(new String[0]));
    }

    /**
     * Définit la taille.
     * @param width la largeur.
     * @param textFieldHeight La hauteur du champs texte.
     * @param listHeight La hauteur de la liste.
     */
    public void setSize(int width, int textFieldHeight, int listHeight) {

        this.width = width;
        this.textFieldHeight = textFieldHeight;
        this.listHeight = listHeight;

        if (showSuggestions) {
            panel.setSize(width, textFieldHeight + listHeight);
            setSize(width, textFieldHeight + listHeight);
        } else {
            panel.setSize(width, textFieldHeight);
            setSize(width, textFieldHeight);
        }

        field.setBounds(0, 0, width, textFieldHeight);
        suggestionsPane.setBounds(0, textFieldHeight, width, listHeight);

    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
    }

    /**
     * Montre les suggestions.
     */
    public void showSuggestions() {
        showSuggestions = true;
        computeSuggestions();
        panel.setSize(width, textFieldHeight + listHeight);
        setSize(width, textFieldHeight + listHeight);
    }

    /**
     * Cache les suggestions.
     */
    public void hideSuggestions() {
        showSuggestions = true;
        panel.setSize(width, textFieldHeight);
        setSize(width, textFieldHeight);
    }

    /**
     * Renvoie le texte.
     * @return le texte.
     */
    public String getText() {
        if (field.getText() == null) {
            return null;
        } else {
            return field.getText();
        }
    }

    /**
     * Définit le texte.
     * @param text le texte.
     */
    public void setText(String text) {
        field.setText(text);
    }

    @Override
    public void setEnabled(boolean enabled) {
        hideSuggestions();
        field.setEditable(enabled);
    }
}
