/*
 * Created on Dec 3, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package WheelOfFortune.game;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import WheelOfFortune.game.player.*;
import WheelOfFortune.game.wheel.*;

public class WheelSetupPanel extends JPanel
{
 //This panel is 589x311
 private static final int W = 589;
 private static final int H = 311;
 
 public static String wheelString = "WheelOfFortune\\game\\wheel.txt";
 DefaultListModel fullModel;
 DefaultListModel currentModel;
 
 JList fullList;
 
 
 JList currentList;

 JButton addBtn;
 JButton removeBtn;
 JButton moveUpBtn;
 JButton moveDownBtn;

 public WheelSetupPanel(Wheel fullWheel, Wheel currentWheel) {
  fullList = new JList();
  currentList = new JList();
  fullModel = new DefaultListModel();
  currentModel = new DefaultListModel();

  for(int i = 0;i < fullWheel.getNumSpaces();++i) {
   fullModel.add(fullModel.size(),fullWheel.getSpaceAt(i));
  }
   
  for(int i = 0;i < currentWheel.getNumSpaces();++i)
   currentModel.add(currentModel.size(),currentWheel.getSpaceAt(i));
  
  this.setLayout(null);
  JPanel column1 = new JPanel();
  column1.setLayout(new BoxLayout(column1,BoxLayout.Y_AXIS));
  JPanel column2 = new JPanel();
  JPanel column3 = new JPanel();

  fullList.setModel(fullModel);
  currentList.setModel(currentModel);

  JScrollPane fullPane = new JScrollPane(fullList);
  JScrollPane currentPane = new JScrollPane(currentList);


  JLabel avail = new JLabel("Available");
  avail.setFont(new Font(avail.getFont().getName(), avail.getFont().getStyle(),16));
  JLabel current = new JLabel("Current Wheel");
  current.setFont(new Font(avail.getFont().getName(), avail.getFont().getStyle(),16));
  addBtn = new JButton("Add ->");
  removeBtn = new JButton("Remove");
  moveUpBtn = new JButton("Move up");
  moveDownBtn = new JButton("Move down");
  
  addBtn.addActionListener(new MyActionListener());
  removeBtn.addActionListener(new MyActionListener());
  moveUpBtn.addActionListener(new MyActionListener());
  moveDownBtn.addActionListener(new MyActionListener());

  avail.setSize(avail.getPreferredSize());
  current.setSize(current.getPreferredSize());
  fullPane.setSize(200,190);
  currentPane.setSize(200,190);
  addBtn.setSize(addBtn.getPreferredSize());
  removeBtn.setSize(w(currentPane),(int)removeBtn.getPreferredSize().getHeight());
  moveUpBtn.setSize(w(currentPane),(int)moveUpBtn.getPreferredSize().getHeight());
  moveDownBtn.setSize(w(currentPane),(int)moveDownBtn.getPreferredSize().getHeight());
  
  
  
  fullPane.setLocation(30,10 + h(avail));
  currentPane.setLocation(W-30-w(currentPane),10 + h(current));
    
  avail.setLocation(x(fullPane) + (w(fullPane)/2 - w(avail)/2),10);
  current.setLocation(x(currentPane) + (w(currentPane)/2 - w(current)/2),10);
  

  removeBtn.setLocation(x(currentPane),y(currentPane) + h(currentPane));
  
  moveUpBtn.setLocation(x(currentPane),y(removeBtn) + h(removeBtn));
  moveDownBtn.setLocation(x(currentPane),y(moveUpBtn) + h(moveUpBtn));
  

  int gap = x(currentPane) - (x(fullPane) + w(fullPane));
  
  addBtn.setLocation(x(fullPane) + w(fullPane) + gap/2 - w(addBtn)/2 , y(currentPane) + h(currentPane)/2 - h(addBtn)/2);
  
  add(avail);
  add(current);
  add(addBtn);
  add(removeBtn);
  add(moveUpBtn);
  add(moveDownBtn);
  add(fullPane);
  add(currentPane);
 }
 
 public Wheel getCurrentWheel() {
  Vector spaces = new Vector();
  for(int i = 0;i < currentModel.size();++i) {
   spaces.add(currentModel.getElementAt(i));
  }
  Wheel w = null;
  
  try {
  w = new Wheel(spaces);
  }
  catch(Exception e) { }
  
  return w;
 }
 
 private int x(Component c) {
  return (int)c.getLocation().getX();
 }
 
  private int y(Component c) {
  return (int)c.getLocation().getY();
 }
 
 private int w(Component c) {
  return (int)c.getSize().getWidth();
 }
 
 private int h(Component c) {
  return (int)c.getSize().getHeight();
 }
 
 private class MyActionListener implements ActionListener {
  public void actionPerformed(ActionEvent ae) {
   if(ae.getSource().equals(addBtn)) {
    int[] indices = fullList.getSelectedIndices();
    for(int i = 0;i < indices.length;++i) {
     Object o = fullModel.getElementAt(indices[i]);
     currentModel.add(currentModel.size(),o);
    }

   }
   else if(ae.getSource().equals(removeBtn)) {
    int[] indices = currentList.getSelectedIndices();
    for(int i = indices.length - 1;i >= 0;--i) {
     currentModel.removeElementAt(indices[i]);
    }
   }
   else if(ae.getSource().equals(moveUpBtn)) {
    int i = currentList.getSelectedIndex();
    if(i == -1) return;
    if(i - 1 < 0) return;
     Object o1 = currentModel.getElementAt(i - 1);
     Object o2 = currentModel.getElementAt(i);
     currentModel.setElementAt(o2, i - 1);
     currentModel.setElementAt(o1, i);
     currentList.setSelectedIndex(i-1);
   }
   else if(ae.getSource().equals(moveDownBtn)) {
    int i = currentList.getSelectedIndex();
    if(i == -1) return;
    if(i + 1 >= currentModel.size()) return;
     Object o1 = currentModel.getElementAt(i);
     Object o2 = currentModel.getElementAt(i + 1);
     currentModel.setElementAt(o2, i);
     currentModel.setElementAt(o1, i + 1);
     currentList.setSelectedIndex(i+1);
   }
  }
 }
}