package be.msyben.vinylManager.gui.list.leftList;

import be.msyben.vinylManager.data.Data;
import be.msyben.vinylManager.entities.Album;
import be.msyben.vinylManager.properties.Language;
import be.msyben.vinylManager.properties.Preferences;
import be.msyben.vinylManager.tools.Sort;
import be.msyben.vinylManager.tools.swing.MyJList;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * Affichage sous forme de liste d'artistes.
 *
 * @author Mathieu Syben
 */
public class ArtistList extends AbstractLeftList {

    /**
     * uid
     */
    private static final long serialVersionUID = 2784785478L;
    /**
     * La sélection courante
     */
    private DisplayElem currentSelection = null;
    /**
     * La liste
     */
    private final JList<DisplayElem> list = new MyJList<>();
    /**
     * Le scrollpanel
     */
    private JScrollPane panel;

    /**
     * Constructeur.
     */
    public ArtistList() {
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (list.isSelectionEmpty()) {
                    selection(new ArrayList<>());
                } else {
                    currentSelection = (DisplayElem) list.getSelectedValue();
                    selection(getAlbums());
                }
            }
        });
        panel = new JScrollPane(list);
    }

    @Override
    public void updateList() {
        updateList(null);
    }

    @Override
    public void updateList(Album selectedAlbum) {
        list.setListData(getAllArtists().toArray(new DisplayElem[0]));
        double position;
        if (selectedAlbum == null) {
            currentSelection = null;
            list.clearSelection();
            position = 0.0;
        } else {
            int idx = -1;
            for (int i = 0; i < list.getModel().getSize(); i++) {

                if (((DisplayElem) list.getModel().getElementAt(i)).getForSort().toLowerCase().equals(selectedAlbum.getArtisteForSorting().toLowerCase())) {
                    idx = i;
                    break;
                }
            }
            if (idx >= 0) {
                currentSelection = new DisplayElem(selectedAlbum.getArtisteForList(), selectedAlbum.getArtisteForSorting());
                list.setSelectedIndex(idx);
                position = (idx * 1.0) / (list.getModel().getSize() * 1.0);
            } else {
                currentSelection = null;
                list.clearSelection();
                position = 0.0;
            }
        }
        int positionNeeded = (int) (panel.getVerticalScrollBar().getMaximum() * position);
        panel.getVerticalScrollBar().setValue(positionNeeded);

    }

    /**
     * Renvoie la liste de tous les artistes.
     *
     * @return la liste de tous les artistes
     */
    private List<DisplayElem> getAllArtists() {
        if (Data.getInstance().getCollection() == null) {
            return new ArrayList<>();
        }

        java.util.List<Album> albs = Data.getInstance().getCollection().getAlbums();
        Sort.sort(albs, Preferences.getInstance().get(Preferences.SORT_ARTIST_DESC, "N").equals("Y"));

        java.util.List<DisplayElem> all = new ArrayList<>();
        String lastSort = "";
        for (Album al : albs) {
            String sort = al.getArtisteForSorting();
            if (!sort.equalsIgnoreCase(lastSort)) {
                if (Preferences.getInstance().get(Preferences.STANDARD_NAME_UPPER_CASE, "Y").equals("Y")) {
                    //all.add(last.toUpperCase());
                    all.add(new DisplayElem(al.getArtisteForList().toUpperCase(), sort));
                } else {
                    //all.add(last);
                    all.add(new DisplayElem(al.getArtisteForList(), sort));
                }
                lastSort = sort;
            }
        }

        return all;
    }

    /**
     * Réupère la liste des albums d'un artiste.
     *
     * @return la liste des albums
     */
    private List<Album> getAlbums() {
        if (Data.getInstance().getCollection() == null) {
            return new ArrayList<>();
        }
        java.util.List<Album> albs = Data.getInstance().getCollection().getAlbums();
        java.util.List<Album> albsNeeded = new ArrayList<>();
        //String artList = Album.convertForSorting(currentSelection).toLowerCase();
        String artList = currentSelection.getForSort().toLowerCase();

        for (Album al : albs) {
            if (al.getArtisteForSorting().toLowerCase().equals(artList)) {
                albsNeeded.add(al);
            }
        }
        return albsNeeded;
    }

    @Override
    public void updateListAfterDeleteAlbum(Album deletedAlbum) {
        updateList(deletedAlbum);
    }

    @Override
    public Component getComponent() {
        return panel;
    }

    @Override
    public void setEnabled(boolean enabled) {
        panel.setEnabled(enabled);
        list.setEnabled(enabled);
    }

    @Override
    public String getTitle() {
        return Language.getInstance().get("name.artistes");
    }

    private static class DisplayElem {

        private String toShow, forSort;

        private DisplayElem() {
        }

        public DisplayElem(String toShow, String forSort) {
            this.toShow = toShow;
            this.forSort = forSort;
        }

        @Override
        public String toString() {
            return toShow;
        }

        public String getForSort() {
            return forSort;
        }
    }
}
