package profiles.uitest;

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

public class UIDevicePropertiesPanel extends JPanel implements ActionListener{
  JTextField posX = new JTextField();
  JTextField posY = new JTextField();
  JTextField radius = new JTextField();
  JTextField actualProfile = new JTextField();
  JTextField requestedProfile = new JTextField();
  UIDevice dev;

  public UIDevicePropertiesPanel(UIDevice dev) {
    this.dev = dev;
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Id: "));
    p.add(new JLabel(dev.getId()));
    this.add(p);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("X: "));
    p.add(posX);
    this.add(p);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Y: "));
    p.add(posY);
    this.add(p);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Radius: "));
    p.add(radius);
    this.add(p);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Actual Profile: "));
    p.add(actualProfile);
    this.add(p);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Requested Profile: "));
    p.add(requestedProfile);
    this.add(p);

    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Actual state: "));
    p.add(new JTextArea(dev.toString(),3,10));
    this.add(p);


    p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    JButton setB = new JButton("Update");
    setB.addActionListener(this);
    p.add(setB);
    this.add(p);

    posX.setText(Integer.toString(dev.getPosX()));
    posY.setText(Integer.toString(dev.getPosY()));
    radius.setText(Integer.toString(dev.getRadius()));
    actualProfile.setText(dev.getProfile().getActualValue());
    requestedProfile.setText(dev.getProfile().getRequestedValue());
  }

  public void actionPerformed(ActionEvent actionEvent) {
    try {
      dev.setPosX(Integer.parseInt(posX.getText()));
      dev.setPosY(Integer.parseInt(posY.getText()));
      dev.setRadius(Integer.parseInt(radius.getText()));
      dev.getProfile().setActualValue(actualProfile.getText());
      dev.getProfile().setRequestedValue(requestedProfile.getText());
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(this, ex.toString());
    }
  }
}