package vismon.gui;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstants;

import javax.swing.*;
import java.awt.*;

public class ProgressBar {

    private JFrame frame;

    private JProgressBar progressBar;
    private JLabel statusLabel;

    public void run() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setUp();
                start();
            }
        });
    }

    protected void setUp() {
        progressBar = new JProgressBar();
        progressBar.setStringPainted(false);
        progressBar.setIndeterminate(true);

        statusLabel = new JLabel("Loading...");

        initComponents();
    }

    protected void start() {
        setPanelLayout();
        addProgressBar();
        addStatusLabel();

        frame.setVisible(true);
    }

    protected void initComponents() {
        int width = 300;
        int height = 100;

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int xLocation = (int)((screenSize.getWidth() - width) * 0.5);
        int yLocation = (int)((screenSize.getHeight() - height) * 0.5);

        this.frame = new JFrame("");
        this.frame.setSize(new Dimension(width, height));
        this.frame.setLocation(xLocation, yLocation);
        this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.frame.setResizable(false);

        this.frame.getContentPane().setLayout(new TableLayout(new double[][]{{0, -1, 0}, {0, -1, 0}}));
    }

    private void setPanelLayout() {
        double[][] layout = new double[][]{{10, TableLayoutConstants.FILL, 10}, {10, 20, 10, 20, TableLayoutConstants.FILL}};
        this.frame.setLayout(new TableLayout(layout));
    }

    private void addProgressBar() {
        progressBar.setValue(0);
        this.frame.add(progressBar, "1,1");
    }

    private void addStatusLabel() {
        this.frame.add(statusLabel, "1,3");
    }

    /*public void setProgressBarValue(int value) {
        progressBar.setValue(value);
        this.frame.repaint();
    }*/

    public void setStatusLabelText(String text) {
        statusLabel.setText(text);
    }

    public void close() {
        this.frame.setVisible(false);
        this.frame.dispose();
    }
}