/*
 *  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.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.FilteredImageSource;

public class SettingsScreen extends Container
        implements ActionListener, Runnable {
    
    private static final int POINTS = 10;
    private static final int TEAMPLAY_POINTS = 15;
    
    Game game;
    PlayerSetup playerSetups[] = {null, null, null, null};
    
    Button startGame;
    Button teamPlay;
    
    /** Start game in teamplay mode. */
    boolean teams = false;
    
    final Color[] colors = {new Color(0xFF, 0x00, 0x00),
    new Color(0x00, 0x00, 0xFF),
    new Color(0x00, 0xFF, 0x00),
    new Color(0xFF, 0xFF, 0x00),
    new Color(0xFF, 0x00, 0xFF),
    new Color(0x00, 0xFF, 0xFF),
    new Color(0xFF, 0xFF, 0xFF),
    new Color(0x7F, 0x7F, 0xFF),
    new Color(0x7F, 0xFF, 0x7F),
    new Color(0xFF, 0x7F, 0x7F)};
    
    Image[] colorImages = new Image[colors.length];
    //Image[] colorImagesU = new Image[colors.length];
    //Image[] colorImagesD = new Image[colors.length];
    
    SettingsScreen(Game game) {
        this.game = game;
        
        createColorImages();
        
        setLayout(null);
        
        playerSetups[0] = new PlayerSetup(this, true, 5, 0, "Player 1");
        playerSetups[0].setLocation(10, 10 + 33 + 15);
        playerSetups[1] = new PlayerSetup(this, true, 3, 1, "Player 2");
        playerSetups[1].setLocation(10, 75 + 33 - 5 + 15);
        playerSetups[2] = new PlayerSetup(this, false, 4, 2, "Player 3");
        playerSetups[2].setLocation(10, 140 + 33 - 10 + 15);
        playerSetups[3] = new PlayerSetup(this, false, 5, 3, "Player 4");
        playerSetups[3].setLocation(10, 205 + 33 - 15 + 15);
        
        startGame = new Button(game.skin.start, game);
        //startGame.setLocation(164, 298);
        startGame.setLocation(55, 303);
        
        teamPlay = new Button(game.skin.teamplay, game);
        teamPlay.setLocation(273, 303);
        
        startGame.addActionListener(this);
        teamPlay.addActionListener(this);
        
        add(playerSetups[0]);
        add(playerSetups[1]);
        add(playerSetups[2]);
        add(playerSetups[3]);
        add(startGame);
        add(teamPlay);
        
        teamPlay.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                playerSetups[0].displayTeams(0, playerSetups[0]);
                playerSetups[1].displayTeams(0, playerSetups[0]);
                playerSetups[2].displayTeams(1, playerSetups[2]);
                playerSetups[3].displayTeams(1, playerSetups[2]);
            }
            
            public void mouseExited(MouseEvent e) {
                playerSetups[0].displayTeams(-1, null);
                playerSetups[1].displayTeams(-1, null);
                playerSetups[2].displayTeams(-1, null);
                playerSetups[3].displayTeams(-1, null);
            }
        });
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startGame)
            teams = false;
        else if (e.getSource() == teamPlay)
            teams = true;
        else
            return;
        
        setVisible(false);
        game.setStatus("Loading...", "wait, please", true);
        (new Thread(this)).start();
    }
    
    public void run() {
        int k = 0;
                
        for (int i = 0; i < playerSetups.length; i++) {
            if (teams || playerSetups[i].isPlayerEnabled())
                k++;
        }
        
        Player[] players = new Player[k];
        k = 0;
        
        for (int i = 0; i < playerSetups.length; i++) {
            if (teams || playerSetups[i].isPlayerEnabled()) {
                PlayerSetup lead = teams ?
                    playerSetups[i - (i % 2)] : null;
                
                int team = teams ? (i / 2) : i;
                
                players[k] = playerSetups[i].getPlayer(lead, team);
                k++;
            }
        }
        
        game.field.setup(players, teams, teams ? TEAMPLAY_POINTS : POINTS);
        // game.field.setVisible(true);
        game.showField();
        game.requestFocus();
        game.field.start();
    }
    
    public void paint(Graphics g) {
        int iw = game.skin.settings.getWidth(this);
        int ih = game.skin.settings.getHeight(this);
        for (int ix = 0; ix < getSize().width; ix = ix + iw)
            for (int iy = 0; iy < getSize().height; iy = iy + ih)
                g.drawImage(game.skin.settings, ix, iy, this);
        
        super.paint(g);
    }
    
//     private void createColorImages() {
//         for (int i = 0; i <= colorImages.length - 1; i++) {
//             colorImages[i] = game.createImage(22, 22);
//             Graphics g = colorImages[i].getGraphics();
// 	    g.setColor(game.skin.buttonBackColor);
// 	    g.fillRect(0, 0, 22, 22);
// 	    Game.setAA(g, false);
//             g.setColor(colors[i]);
//             g.fillOval(2, 2, 17, 17);
//             g.setColor(game.skin.buttonBorderColor);
//             g.drawOval(2, 2, 17, 17);
//         }
//     }
    
    private void createColorImages() {
        MediaTracker tracker = new MediaTracker(game);
        
        for (int i = 0; i < colorImages.length; i++) {
            PlayerColorFilter filter = new PlayerColorFilter(colors[i]);
            
            colorImages[i] = game.createImage
                    (new FilteredImageSource
                    (game.skin.snake[Field.BODY_V].getSource(), filter))
                    .getScaledInstance(22, 22, Image.SCALE_SMOOTH);
            tracker.addImage(colorImages[i], 0);
            
// 	    colorImagesU[i] = game.createImage
//                 (new FilteredImageSource
//                  (game.skin.snake[Field.HEAD_U].getSource(), filter))
//                 .getScaledInstance(22, 22, Image.SCALE_FAST);
// 	    tracker.addImage(colorImagesU[i], 0);
            
// 	    colorImagesD[i] = game.createImage
//                 (new FilteredImageSource
//                  (game.skin.snake[Field.HEAD_D].getSource(), filter))
//                 .getScaledInstance(22, 22, Image.SCALE_FAST);
// 	    tracker.addImage(colorImagesD[i], 0);
        }
        
        try {
            //// TEST
            //Thread.currentThread().sleep(5000);
            tracker.waitForAll();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    public boolean isOpaque() {
        // Skins should not set transparent background
        //     for the settings pane.
        return true;
    }
}
