
package be.msyben.vinylManager.gui;

import be.msyben.vinylManager.interfaces.WaitListener;
import be.msyben.vinylManager.properties.Language;
import be.msyben.vinylManager.tools.Tools;
import be.msyben.vinylManager.tools.ViewGif;
import be.msyben.vinylManager.tools.log.VinylManagerLogger;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

/**
 *
 * @author Mathieu Syben
 */
public class WaitWindow extends JWindow {

    /**
     * uid
     */
    private static final long serialVersionUID = 9248;
    /**
     * L'image à afficher (stream)
     */
    private InputStream imageStream;
    /** L'image à afficher (swing object)*/
    private ViewGif image = null;
    private JButton annuler = new JButton(Language.getInstance().get("button.annuler"));
    private List<WaitListener> waitListeners;

    private boolean showProgressBar;
    private JProgressBar jpb;
    
    
    /**
     * Constructeur
     */
    public WaitWindow() {
        this(false);
    }
    
    public WaitWindow(boolean showProgressBar) {
        super();
        
        this.showProgressBar = showProgressBar;

        waitListeners = new ArrayList<WaitListener>();

        this.setSize(400, showProgressBar?340:300); //On donne une taille à notre fenêtre
        this.setLocationRelativeTo(null); //On centre la fenêtre sur l'ecran
        this.setLayout(null);


        putComponents();
    }

    /**
     * Ajoute les composants
     */
    private void putComponents() {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setSize(400, showProgressBar?340:300);
        panel.setBorder(new LineBorder(Color.BLACK));
        this.getContentPane().add(panel);

        JLabel label = new JLabel(Language.getInstance().get("wait.label"));

        label.setBounds(5, 0, 390, 30);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setHorizontalTextPosition(SwingConstants.CENTER);
        label.setFont(new Font("Serif", Font.BOLD, 16));

        panel.add(label);

        try {
            imageStream = this.getClass().getResourceAsStream("/be/msyben/vinylManager/gui/icon/vinyleAnime.gif");
            image = new ViewGif(imageStream);
            image.setBounds(100, 50, 200, 200);
            panel.add(image);
        } catch (IOException ex) {
            VinylManagerLogger.log( ex);
        }

        if (showProgressBar) {
            jpb = new JProgressBar(0, 100);
            jpb.setString("");
            jpb.setStringPainted(true);
            jpb.setBounds(5,275,390,25);
            panel.add(jpb);
        }
        
        annuler.setBounds(150, showProgressBar?310:270, 100, 25);
        annuler.setVisible(false);
        annuler.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (WaitListener wl : waitListeners) {
                    wl.onCancel();
                }
            }
        });
        panel.add(annuler);

        /*this.setFocusableWindowState(true);
        this.addWindowFocusListener(new WindowFocusListener() {

            @Override
            public void windowGainedFocus(WindowEvent e) {
            }

            @Override
            public void windowLostFocus(WindowEvent e) {
                WaitWindow.this.toFront();
            }
        });
        this.toFront();*/
    }

    public void setProgressMax(int max) {
        if (showProgressBar) {
            jpb.setMaximum(max); 
        }
    }
    public void setProgressCurrent(String prefix, int current) {
        if (showProgressBar) {
            jpb.setValue(current);
            String txt = prefix + " : " + current + " / " + jpb.getMaximum();
            jpb.setString(txt);
        }
    }
    
    /**
     * Ferme la fenêtre
     */
    @Override
    public void dispose() {
        Tools.closeQuietly(imageStream);
        if (image != null) {
            image.closeStream();
        }
        super.dispose();
    }

    /**
     * Ajoute un listener.
     * @param listener Le listener.
     */
    public void addWaitListener(WaitListener listener) {
        if (listener != null) {
            waitListeners.add(listener);
            annuler.setVisible(true);
        }
    }
}
