package WheelOfFortune.puzzleManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import WheelOfFortune.game.*;

public class PuzzleManagerOutput {

 JFrame mainFrm;

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

 JPanel localPanel;
 JList localList;
 JScrollPane localPane;
 JButton refreshLocalBtn;

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

 JButton uploadBtn;
 JButton downloadBtn;

 private MainMenu mm;

 public PuzzleManagerOutput(MainMenu mm) {
  this.mm = mm;
  mainFrm = new JFrame();
  mainFrm.getContentPane().setLayout(new BorderLayout());
  mainFrm.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  mainFrm.setTitle("Puzzle Manager");
  mainFrm.setResizable(false);
  mainFrm.addWindowListener(new PMListener());

  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);

  localPanel = new JPanel();
  localPanel.setLayout(new BorderLayout());
  localList = new JList();
  localPane = new JScrollPane(localList);
  localPane.setPreferredSize(new Dimension(200,200));
  refreshLocalBtn = new JButton("Refresh");
  localPanel.add(localPane,BorderLayout.NORTH);
  localPanel.add(refreshLocalBtn,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");
  remotePanel.add(remotePane,BorderLayout.NORTH);
  remotePanel.add(refreshRemoteBtn,BorderLayout.SOUTH);

  JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new BorderLayout());
  uploadBtn = new JButton("Upload ->");
  downloadBtn = new JButton("<- Download");
  buttonPanel.add(uploadBtn, BorderLayout.NORTH);
  buttonPanel.add(downloadBtn, BorderLayout.SOUTH);

  mainFrm.getContentPane().add(connectPanel,BorderLayout.NORTH);
  mainFrm.getContentPane().add(localPanel,BorderLayout.WEST);
  mainFrm.getContentPane().add(remotePanel,BorderLayout.EAST);  mainFrm.getContentPane().add(buttonPanel,BorderLayout.CENTER);
  
  mainFrm.pack();
  centerComponent(mainFrm);

 }
 public class PMListener extends WindowAdapter
 {
 	public void windowClosing(WindowEvent e)
 	{
 		mainFrm.dispose();
 		mm.setVisible(true);
 	}
 }

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

 public void setLocalList(String[] names) {
  localList.setListData(names);
 }

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

 public void setState(int state) {
  /*if(state == PuzzleManager.NOT_CONNECTED) stateLbl.setText("NOT CONNECTED");
  else if(state == PuzzleManager.IDLE) stateLbl.setText("IDLE");
  else if(state == PuzzleManager.DOWNLOADING) stateLbl.setText("DOWNLOADING");
  stateLbl.repaint();*/
  if(state == PuzzleManager.NOT_CONNECTED) mainFrm.setTitle("Puzzle Manager - Not Connected");
  else if(state == PuzzleManager.IDLE) mainFrm.setTitle("Puzzle Manager - Idle");
  else if(state == PuzzleManager.DOWNLOADING) mainFrm.setTitle("Puzzle Manager - Downloading");
  else if(state == PuzzleManager.UPLOADING) mainFrm.setTitle("Puzzle Manager - Uploading");
 }

 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 getRefreshLocalBtn() { return refreshLocalBtn; }
 public JButton getRefreshRemoteBtn() { return refreshRemoteBtn; }
 public JButton getUploadBtn() { return uploadBtn; }
 public JButton getDownloadBtn() { return downloadBtn; }

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

 public JList getLocalList() { return localList; }
 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);
 }
}