package vermilion;

import java.awt.Container;
import java.awt.event.ActionEvent;
import relative.*;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * A self-building interface window that shows a pattern's properties.
 */
public class JPatternPropertiesFrame extends JDialog
{
    /**
     * Indicates to the caller after showing the dialog that the user hit
     *  the 'OK' button and wishes to save the changes they made.
     */
    public boolean saveChanges = false;

    /**
     * Points to the pattern in the composition which we are changing the
     * properties for.
     */
    private PatternNode pattern;

    // These are the text fields we allow the user to change in order to change
    // the length, time signature etc. of the pattern.
    private JTextField nameField;
    private JTextField lengthField;
    private JTextField sigField;

    // Buttons to apply or cancel the changes made by the user. Pressing either
    // closes the frame and returns control to the caller.
    protected JButton okButton;
    private JButton cancelButton;

    /**
     * Creates a frame showing basic controls for handling the parameters on
     * a pattern.
     */
    public JPatternPropertiesFrame(PatternNode p)
    {
        super(Main.window, "Properties: " + p.patternID, true);

        pattern = p;

        RelativeLayout rel = new RelativeLayout(null);
        setLayout(rel);

        Dimension size = RelativeLayout.getSuggestedSize(0.25f, 0.2f);
        JPatternPropertiesFrameListener buttonListener = new JPatternPropertiesFrameListener(this);
        Container panel = getContentPane();

        JLabel l = new JLabel("Title:");
        panel.add(l, new RelativePlacement(0, 0, 333, 250));
        rel.registerForFontSizeChanges(l);

        nameField = new JTextField();
        nameField.setText(pattern.patternID);
        panel.add(nameField, new RelativePlacement(333, 0, 666, 250));
        rel.registerForFontSizeChanges(nameField);

        l = new JLabel("Length:");
        panel.add(l, new RelativePlacement(0, 250, 333, 250));
        rel.registerForFontSizeChanges(l);

        lengthField = new JTextField();
        lengthField.setText(String.valueOf(pattern.patternLength));
        panel.add(lengthField, new RelativePlacement(333, 250, 666, 250));
        rel.registerForFontSizeChanges(lengthField);

        l = new JLabel("Time Sig:");
        panel.add(l, new RelativePlacement(0, 500, 333, 250));
        rel.registerForFontSizeChanges(l);

        sigField = new JTextField();
        sigField.setText(String.valueOf(pattern.timeSig));
        panel.add(sigField, new RelativePlacement(333, 500, 666, 250));
        rel.registerForFontSizeChanges(sigField);

        cancelButton = new JButton("Cancel");
        panel.add(cancelButton, new RelativePlacement(333, 750, 333, 250));
        cancelButton.addActionListener(buttonListener);
        rel.registerForFontSizeChanges(cancelButton);

        okButton = new JButton("OK");
        panel.add(okButton, new RelativePlacement(666, 750, 333, 250));
        okButton.addActionListener(buttonListener);
        rel.registerForFontSizeChanges(okButton);

        setMinimumSize(size);
        setSize(size);
        setLocationRelativeTo(Main.window);

    }

    /**
     * Writes the values entered on the window into the pattern pointed to
     * by the constructor.
     */
    public void saveData()
    {
        try
        {
            pattern.patternID = nameField.getText();
            pattern.patternLength = Integer.parseInt(lengthField.getText());
            pattern.timeSig = Integer.parseInt(sigField.getText());
            saveChanges = true;
        }
        catch (Exception ex) { }
    }
}

// Handles pressing of the OK/Cancel buttons ont the frame.
class JPatternPropertiesFrameListener implements ActionListener
{
    private JPatternPropertiesFrame frame;

    public JPatternPropertiesFrameListener(JPatternPropertiesFrame f)
    {
        frame = f;
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource() == frame.okButton) frame.saveData();
        frame.setVisible(false);
    }
}