package WheelOfFortune.webMonitor;

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

public class WebMonitorOutput {

 JFrame mainFrm;

 JPanel connectPanel;
 JTextField addressFld;
 JTextField portFld;
 JTextField userNameFld;
 JPasswordField passwordFld;
 JButton connectBtn;
 JButton disconnectBtn;
 JLabel stateLbl;
 

 JPanel remotePanel;
 JList remoteList;
 JScrollPane remotePane;
 JButton refreshRemoteBtn;
 JButton viewBtn;

 JPanel viewPanel;
 JTextArea viewArea;
 JScrollPane viewPane;
 JButton approveBtn;
 JButton rejectBtn;

 public WebMonitorOutput() {
  mainFrm = new JFrame();
  mainFrm.getContentPane().setLayout(new BorderLayout());
  mainFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  mainFrm.setTitle("Web Monitor");
  mainFrm.setResizable(false);

  connectPanel = new JPanel();
  connectPanel.setLayout(new BorderLayout());

  JPanel addressPanel = new JPanel();
  addressFld = new JTextField(20);
  portFld = new JTextField(5);
  addressPanel.add(new JLabel("Server Address"));
  addressPanel.add(addressFld);
  addressPanel.add(new JLabel("Port"));
  addressPanel.add(portFld);

  JPanel loginPanel = new JPanel();
  loginPanel.setLayout(new FlowLayout());
  userNameFld = new JTextField(20);
  passwordFld = new JPasswordField(20);
  loginPanel.add(new JLabel("Username"));
  loginPanel.add(userNameFld);
  loginPanel.add(new JLabel("Password"));
  loginPanel.add(passwordFld);
 
  JPanel connectButtonPanel = new JPanel();
  connectBtn = new JButton("Connect");
  disconnectBtn = new JButton("Disconnect");
  stateLbl = new JLabel("");
  connectButtonPanel.add(connectBtn);
  connectButtonPanel.add(disconnectBtn);
  //connectButtonPanel.add(stateLbl);
    

  connectPanel.add(addressPanel,BorderLayout.NORTH);
  connectPanel.add(loginPanel,BorderLayout.CENTER);
  connectPanel.add(connectButtonPanel, BorderLayout.SOUTH);
 
  remotePanel = new JPanel();
  remotePanel.setLayout(new BorderLayout());
  remoteList = new JList();
  remotePane = new JScrollPane(remoteList);
  remotePane.setPreferredSize(new Dimension(200,200));
  refreshRemoteBtn = new JButton("Refresh");
  viewBtn = new JButton("View");

  JPanel remoteButtonPanel = new JPanel();
  remoteButtonPanel.add(refreshRemoteBtn); remoteButtonPanel.add(viewBtn);
  remotePanel.add(remotePane,BorderLayout.CENTER);
  remotePanel.add(remoteButtonPanel,BorderLayout.SOUTH);

  viewPanel = new JPanel();
  viewPanel.setLayout(new BorderLayout());
  viewArea = new JTextArea();
  viewArea.setEditable(false);
  viewPane = new JScrollPane(viewArea);
  viewPane.setPreferredSize(new Dimension(200,300));

  JPanel viewButtonPanel = new JPanel();
  approveBtn = new JButton("Approve");
  rejectBtn = new JButton("Reject");
  viewButtonPanel.add(approveBtn);
  viewButtonPanel.add(rejectBtn);

  viewPanel.add(viewPane,BorderLayout.NORTH);
  viewPanel.add(viewButtonPanel,BorderLayout.SOUTH);

  mainFrm.getContentPane().add(connectPanel,BorderLayout.NORTH);
  mainFrm.getContentPane().add(remotePanel,BorderLayout.WEST);
  mainFrm.getContentPane().add(viewPanel,BorderLayout.CENTER);
  mainFrm.pack();
  
  centerComponent(mainFrm);
  }


 public void display() {
  mainFrm.show();
 }

 public void setRemoteList(String[] names) {
  remoteList.setListData(names);
 }

 //How can I make the scrollbar go to the top?
 public void setViewAreaText(String text) {
  viewArea.setText(text);
 }

 public void setState(int state) {
  //if(state == WebMonitor.NOT_CONNECTED) stateLbl.setText("NOT CONNECTED");
  //else if(state == WebMonitor.IDLE) stateLbl.setText("IDLE");
  //else if(state == WebMonitor.DOWNLOADING) stateLbl.setText("DOWNLOADING");
  if(state == WebMonitor.NOT_CONNECTED) mainFrm.setTitle("Web Monitor - Not Connected");
  else if(state == WebMonitor.IDLE) mainFrm.setTitle("Web Monitor - Idle");
  else if(state == WebMonitor.DOWNLOADING) mainFrm.setTitle("Web Monitor - Downloading");
 }

 public void printErrorMessage(String message) {
  JOptionPane jop = new JOptionPane();
  jop.showMessageDialog(mainFrm, message, "Error", JOptionPane.ERROR_MESSAGE);
 }

 public JButton getConnectBtn() { return connectBtn; }
 public JButton getDisconnectBtn() { return disconnectBtn; }
 public JButton getRefreshRemoteBtn() { return refreshRemoteBtn; }
 public JButton getViewBtn() { return viewBtn; }
 public JButton getApproveBtn() { return approveBtn; }
 public JButton getRejectBtn() { return rejectBtn; }

 public JTextField getAddressFld() { return addressFld; }
 public JTextField getPortFld() { return portFld; }
 public JTextField getUserNameFld() { return userNameFld; }
 public JPasswordField getPasswordFld() { return passwordFld; }

 public JList getRemoteList() { return remoteList; }
 
 private void centerComponent(Component c) {
  Dimension componentSize = c.getSize();
  int width = (int)componentSize.getWidth();
  int height = (int)componentSize.getHeight();
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  int x = (int)screenSize.getWidth()/2 - width/2;
  int y = (int)screenSize.getHeight()/2 - height/2;
  c.setLocation(x,y);
 }
}