/*
 *  Copyright 2002-2009 Mikhail Kryshen <mikhail@kryshen.net>
 *
 *  This file is part of BMTron.
 *
 *  BMTron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  BMTron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with BMTron.  If not, see <http://www.gnu.org/licenses/>.
 */
 
package kryshen.bmtron;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class PlayerSetup extends Container implements MouseListener, ActionListener {
    private final Game game;
    private final SettingsScreen settings;
    
    private boolean active = false;
    private boolean enabled;
    private int maxControl;
    private int maxColor;

    private int control;
    private int color;
    
    private Button enabledButton;
    private Button controlButton;
    private Button colorButton;
    private TextField nameField;

    PlayerSetup(SettingsScreen settings, boolean enabled, int control, int color, String name) {
        this.settings = settings;
        this.game = settings.game;
        this.enabled = enabled;
        this.control = control;
        this.color = color;
        
        maxControl = 5;//game.skin.controls.length - 1;
        maxColor = settings.colors.length - 1;
        
        setSize(getPreferredSize());
        
        setLayout(null);
        
        enabledButton = new Button(enabled ? game.skin.yes : game.skin.no, game);
        controlButton = new Button(game.skin.controls[control], game);
        colorButton = new Button(settings.colorImages[color], game);
        nameField = new TextField(name, game);
        
        enabledButton.setLocation(10, 10);
        nameField.setLocation(44, 5);
        colorButton.setLocation(387, 10);
        controlButton.setLocation(421, 5);
        
        enabledButton.addMouseListener(this);
        controlButton.addMouseListener(this);
        colorButton.addMouseListener(this);
        nameField.addMouseListener(this);

        enabledButton.addActionListener(this);
        controlButton.addActionListener(this);
        colorButton.addActionListener(this);
        
        add(enabledButton);
        add(controlButton);
        add(colorButton);
        add(nameField);
        
        addMouseListener(this);
    }

    public void paint(Graphics g) {
	super.paint(g);

        if (active) {
	    g.setColor(game.skin.activePanelBorderColor);
    	    g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
    	    g.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
	}
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == enabledButton) {
            setPlayerEnabled(!enabled);
            
            int ne = 0;
            for (int i = 0; i <= settings.playerSetups.length - 1; i++)
                if (settings.playerSetups[i].enabled) ne++;
            if (ne < 2) {
                for (int i = 0; i <= settings.playerSetups.length - 1; i++)
                    if (settings.playerSetups[i] != this &&
                        !settings.playerSetups[i].enabled) {
                        
                        settings.playerSetups[i].setPlayerEnabled(true);
                        break;
                    }
            }
        } else if (e.getSource() == controlButton) {
            //incControl(false);
	    incControl(true);
        } else if (e.getSource() == colorButton) {
            //incColor(false);
            incColor(true);
        }
    }

    void setPlayerEnabled(boolean enabled) {
        this.enabled = enabled;
        enabledButton.setImage(enabled ? game.skin.yes : game.skin.no);
        repaint();
    }
    
    void incControl(boolean skip) {
        control++;
	if (control > maxControl) control = 0;
        
	controlButton.setImage(game.skin.controls[control]);
        
        if (control <= 3) {
            for (int i = 0; i < settings.playerSetups.length; i++) {
                if (settings.playerSetups[i] != this &&
                    settings.playerSetups[i].control == control) {
                    
		    if (skip) incControl(true);
		    else settings.playerSetups[i].incControl(true);
		    
                    break;
                }
	    }
	}
    }
    
    void incColor(boolean skip) {
        color++;
        if (color > maxColor) color = 0;
	
	colorButton.setImage(settings.colorImages[color]);
	
        for (int i = 0; i < settings.playerSetups.length; i++) {
            if (settings.playerSetups[i] != this & settings.playerSetups[i].color == color)  {	    
                if (skip) incColor(true);
		else settings.playerSetups[i].incColor(true);
		
                break;
            }
        }
    }

    Player getPlayer(PlayerSetup lead, int team) {
        if (lead == null) lead = this;

        switch (control) {
        case 4:
            return new ComputerPlayer1(game.field, nameField.getText(),
                                       settings.colors[lead.color], team);
        case 5:
            return new ComputerPlayer2(game.field, nameField.getText(),
                                       settings.colors[lead.color], team);
	case 6:
            return new ComputerPlayer3(game.field, nameField.getText(),
                                       settings.colors[lead.color], team);
        default:
            return new HumanPlayer(game.field, nameField.getText(), 
                                   settings.colors[lead.color], 
                                   team, control);
        }
    }
    
    boolean isPlayerEnabled() {
        return enabled;
    }
    
    public void mouseEntered(MouseEvent e) { active = true; repaint(); }
    public void mouseExited(MouseEvent e) { active = false; repaint(); }
    public void mousePressed(MouseEvent e) {}    
    public void mouseReleased(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    
    public Dimension getPreferredSize() {
        return new Dimension(480, 45);
    }

    void displayTeams(int team, PlayerSetup lead) {
        if (team >= 0) {
//             if (lead == this) {
//                 colorButton.setImage
//                     (settings.colorImagesU[lead.color]);
//             } else {
//                 colorButton.setImage
//                     (settings.colorImagesD[lead.color]);
//             }
            colorButton.setImage(settings.colorImages[lead.color]);
            enabledButton.setImage(game.skin.yes);
            nameField.setNote("(Team " + (team + 1) + ")");
        } else {
            colorButton.setImage(settings.colorImages[this.color]);
            enabledButton.setImage(enabled ? game.skin.yes : game.skin.no);
            nameField.setNote(null);
        }
    }
}
