package WheelOfFortune.localManager;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableModel;

import WheelOfFortune.game.MainMenu;

public class LocalManagerOutput {

 JFrame mainFrm;
 JPanel mainPanel;

 JList fileList;
 JScrollPane filePane;
 JButton refreshBtn;
 JButton openBtn;
 JButton newFileBtn;
 JButton deleteFileBtn;

 JTable puzzleTable;
 JScrollPane puzzlePane;
 JButton deleteBtn;
 JButton addBtn;


 JDialog addPuzzleDialog;
 JTextField solutionFld;
 JTextField categoryFld;

 JButton addPuzzleBtn;
 JButton clearPuzzleBtn;
 MainMenu mm;

  public LocalManagerOutput() {
  mainFrm = new JFrame();
  mainFrm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  mainFrm.setTitle("Local Manager");


  mainFrm.setResizable(false);

  mainPanel = new JPanel();
  mainPanel.setLayout(null);
  mainFrm.setContentPane(mainPanel);
  



  fileList = new JList();
  filePane = new JScrollPane(fileList);
  //filePane.setPreferredSize(new Dimension(140,200));

 filePane.setLocation(0,0);
 filePane.setSize(140,200);





  
  refreshBtn = new JButton("Refresh");
 refreshBtn.setLocation(0,(int)filePane.getSize().getHeight());
 refreshBtn.setSize((int)filePane.getSize().getWidth(),(int)refreshBtn.getPreferredSize().getHeight());




  openBtn = new JButton("Open");
  openBtn.setLocation(0,(int)filePane.getSize().getHeight() + (int)refreshBtn.getSize().getHeight());
  openBtn.setSize((int)filePane.getSize().getWidth(),(int)openBtn.getPreferredSize().getHeight());

    newFileBtn = new JButton("New File");
  newFileBtn.setLocation(0,(int)filePane.getSize().getHeight() + (int)refreshBtn.getSize().getHeight() + (int)openBtn.getSize().getHeight());
  newFileBtn.setSize((int)filePane.getSize().getWidth(),(int)newFileBtn.getPreferredSize().getHeight());

 deleteFileBtn = new JButton("Delete");
 deleteFileBtn.setLocation(0,(int)filePane.getSize().getHeight() + (int)refreshBtn.getSize().getHeight() + (int)openBtn.getSize().getHeight() + (int)newFileBtn.getSize().getHeight());
deleteFileBtn.setSize((int)filePane.getSize().getWidth(),(int)newFileBtn.getPreferredSize().getHeight());

  mainPanel.add(filePane);
 mainPanel.add(refreshBtn);
mainPanel.add(openBtn);
 mainPanel.add(newFileBtn);
 mainPanel.add(deleteFileBtn);

  puzzleTable = new JTable();
  puzzleTable.setDefaultRenderer(new Object().getClass(), new MyTableCellRenderer());
  puzzleTable.setDragEnabled(false);

  puzzlePane = new JScrollPane(puzzleTable);
  puzzlePane.setSize(puzzlePane.getPreferredSize());
  puzzlePane.setLocation(filePane.getWidth(),0);
  mainPanel.add(puzzlePane);

  JPanel puzzleBtnPanel = new JPanel();
  puzzleBtnPanel.setLayout(new FlowLayout());
  deleteBtn = new JButton("Delete");
  addBtn = new JButton("Add");

  puzzleBtnPanel.add(deleteBtn);
  puzzleBtnPanel.add(addBtn);
 
 puzzleBtnPanel.setLocation((int)filePane.getSize().getWidth(),(int)puzzlePane.getSize().getHeight());
 puzzleBtnPanel.setSize(puzzleBtnPanel.getPreferredSize());
 mainPanel.add(puzzleBtnPanel);



 //height should be Math.max of the two columns
 mainPanel.setPreferredSize(new Dimension((int)filePane.getSize().getWidth() + (int)puzzlePane.getSize().getWidth(),(int)puzzlePane.getSize().getHeight() + (int)puzzleBtnPanel.getSize().getHeight()));



  JPanel addPuzzlePanel = new JPanel();
  addPuzzlePanel.setLayout(new BoxLayout(addPuzzlePanel,BoxLayout.Y_AXIS));
  JPanel firstPanel = new JPanel();
  JLabel solutionLbl = new JLabel("Solution");
  solutionFld = new JTextField(32);

  firstPanel.add(solutionLbl);
  firstPanel.add(solutionFld);


  JPanel secondPanel = new JPanel();
  JLabel categoryLbl = new JLabel("Category");
  categoryFld = new JTextField(20);
  secondPanel.add(categoryLbl);
  secondPanel.add(categoryFld);

  JPanel thirdPanel = new JPanel();
  addPuzzleBtn = new JButton("Add");
  clearPuzzleBtn = new JButton("Clear");
  thirdPanel.add(addPuzzleBtn);
  thirdPanel.add(clearPuzzleBtn);

  addPuzzlePanel.add(firstPanel);
  addPuzzlePanel.add(secondPanel);
  addPuzzlePanel.add(thirdPanel);


  mainFrm.pack();
  centerComponent(mainFrm);

  addPuzzleDialog = new JDialog(mainFrm, "Add Puzzles", true);
  addPuzzleDialog.setResizable(false);
  addPuzzleDialog.getContentPane().add(addPuzzlePanel);
  addPuzzleDialog.pack();
  centerComponent(addPuzzleDialog);
 }


 public void display(MainMenu mm) {
  mainFrm.show();
  this.mm = mm;
 }

 public MainMenu getMM()
 {
 	return mm;
 }

 public void setFileList(String[] names) {
  fileList.setListData(names);
 }

 public void setPuzzleTableModel(TableModel tm) {
  puzzleTable.setModel(tm);
 }


 public void displayAddPuzzleDialog() {
  addPuzzleDialog.show();
 }

 public void clearPuzzle() {
  solutionFld.setText("");
  categoryFld.setText("");
 }

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

 public JFrame getMainFrm() { return mainFrm; }

 public JButton getRefreshBtn() { return refreshBtn; }
 public JButton getOpenBtn() { return openBtn; }
 public JButton getNewFileBtn() { return newFileBtn; }
 public JButton getDeleteFileBtn() { return deleteFileBtn; }

 public JButton getDeleteBtn() { return deleteBtn; }
 public JButton getAddBtn() { return addBtn; }

 public JList getFileList() { return fileList; }
 public JTable getPuzzleTable() { return puzzleTable; }


 public JButton getAddPuzzleBtn() { return addPuzzleBtn; }
 public JButton getClearPuzzleBtn() { return clearPuzzleBtn; }
 
 public JTextField getSolutionFld() { return solutionFld; }
 public JTextField getCategoryFld() { return categoryFld; }

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

}