package be.msyben.vinylManager.gui;

import be.msyben.vinylManager.controller.Controller;
import be.msyben.vinylManager.entities.Album;
import be.msyben.vinylManager.entities.Disque;
import be.msyben.vinylManager.entities.Titre;
import be.msyben.vinylManager.exceptions.VinylManagerException;
import be.msyben.vinylManager.gui.icon.Icons;
import be.msyben.vinylManager.interfaces.WaitListener;
import be.msyben.vinylManager.properties.Language;
import be.msyben.vinylManager.tools.WaitThread;
import be.msyben.vinylManager.tools.discogs.DiscogsSearch;
import be.msyben.vinylManager.tools.discogs.DiscogsSearchResult;
import be.msyben.vinylManager.tools.log.VinylManagerLogger;
import be.msyben.vinylManager.tools.lyrics.LyricsRetriever;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Mathieu Syben
 */
public class LyricsWindow extends JFrame {

    private static final long serialVersionUID = 597427l;

    private Titre t;
    private JButton ok = new JButton(Language.getInstance().get("button.valider"));
    private JButton vider = new JButton(Language.getInstance().get("button.vider"));
    private JButton cancel = new JButton(Language.getInstance().get("button.annuler"));
    private JButton rechercher = new JButton(Language.getInstance().get("button.rechercher.paroles"));
    private JTextArea lyrics = new JTextArea();
    private JScrollPane lyricsPane = new JScrollPane(lyrics);
    private Disque disque = null;
    private Album album;
    private Controller controller;
    private JFrame parent;
    private boolean fromMainWindow;

    private LyricsWindow() {
    }

    public LyricsWindow(Controller controller, Disque disque, Album album, Titre t) {
        super(t.getNom());
        parent = MainWindow.getInstance();
        fromMainWindow = true;
        parent.setEnabled(false);
        setIconImage(Icons.getInstance().icon.getImage());
        this.t = t;
        this.controller = controller;
        this.disque = disque;
        this.album = album;

        this.setSize(630, 530); //On donne une taille à notre fenêtre
        this.setLocationRelativeTo(null); //On centre la fenêtre sur l'ecran
        this.setResizable(false); //On interdit la redimensionnement de la fenêtre
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //On dit à l'application de se fermer

        putElements();

        if (t.getLyrics() != null) {
            lyrics.setText(t.getLyrics());
        }

        setVisible(true);
    }

    public LyricsWindow(JFrame parent, Album album, Titre t) {
        super(t.getNom());
        this.parent = parent;
        this.album = album;
        fromMainWindow = false;
        parent.setEnabled(false);
        setIconImage(Icons.getInstance().icon.getImage());
        this.t = t;

        this.setSize(630, 530); //On donne une taille à notre fenêtre
        this.setLocationRelativeTo(null); //On centre la fenêtre sur l'ecran
        this.setResizable(false); //On interdit la redimensionnement de la fenêtre
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //On dit à l'application de se fermer

        putElements();

        if (t.getLyrics() != null) {
            lyrics.setText(t.getLyrics());
        }

        setVisible(true);
    }

    private void putElements() {
        Container c = this.getContentPane();
        c.setLayout(null);
        c.setSize(630, 530);

        rechercher.setBounds(215, 10, 200, 25);
        c.add(rechercher);
        rechercher.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String artist;
                if (album.getCompliation()) {
                    artist = t.getArtiste();
                } else {
                    artist = album.getArtiste();
                }
                ///new SearchLyricsWindow(LyricsWindow.this, artist, t.getNom()).display();
                final WaitThread waitThread = getWaitThread();
                waitThread.start();
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            String lyricsFound = LyricsRetriever.getLyrics(artist, t.getNom());
                            if (waitThread.isRunning()) {
                                if (lyricsFound != null) {
                                    setLyrics(lyricsFound);
                                }
                                waitThread.arrete();
                            }
                        } catch (VinylManagerException ex) {
                            waitThread.arrete();
                            VinylManagerLogger.log(ex);
                            new ErrorWindow(LyricsWindow.this, ex.getMessage(), ex);
                        }
                    }
                }.start();
            }
        });

        lyricsPane.setBounds(5, 45, 615, 400);
        c.add(lyricsPane);

        ok.setBounds(5, 460, 100, 25);
        ok.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t.setLyrics(lyrics.getText());
                if (fromMainWindow) {
                    //controller.editDisque(album, disque);
                    controller.setModified();
                }
                parent.setEnabled(true);
                dispose();
            }
        });
        c.add(ok);

        vider.setBounds(255, 460, 100, 25);
        vider.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                lyrics.setText("");
            }
        });
        c.add(vider);

        cancel.setBounds(505, 460, 100, 25);
        cancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                parent.setEnabled(true);
                dispose();
            }
        });
        c.add(cancel);
    }

    public void setLyrics(String lyricsFound) {
        lyrics.setText(lyricsFound);
    }
    
    
    private WaitThread getWaitThread() {
        final WaitThread waitThread = new WaitThread();
        waitThread.addWaitListener(new WaitListener() {
            @Override
            public void onCancel() {
                waitThread.arrete();
            }
        });
        return waitThread;
    }
}
