/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program 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 2 of the License, or (at
your option) any later version.

This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.game.testgame;
import gecco.server.automaton.*;

import gecco.server.core.*;
import gecco.server.unitmanager.UnitManager;
import gecco.server.visibility.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

/**
 *  Description of the Class
 *
 */
public class ServerTest {
    /**
     *  Description of the Field
     */
    static int actionCtr = 0;

    /**
     *  Description of the Field
     */
    static HashMap role2BufferedImage = new HashMap();
    /**
     *  Description of the Field
     */
    static HashMap role2Frame = new HashMap();

    /**
     *  Description of the Field
     */
    static Server server;
    /**
     *  Description of the Field
     */
    static byte[] cols;
    /**
     *  Description of the Field
     */
    static int repaintCtr = 0;
    /**
     *  Description of the Field
     */
    static Dimension d;


    /**
     *  Constructor for the ServerTest object
     *
     * @param  _server  Description of Parameter
     */
    public ServerTest(Server _server) {
        server = _server;
        LogManager logger = ReferenceHolder.getLogManager();

        cols = server.getMap();
        d = server.getMapSize();

        UnitManager uM = ReferenceHolder.getUnitManager();
        initRoleGfx("RED_TEAM_LEADER");
        initRoleGfx("RED_TEAM_PLAYER1");
        initRoleGfx("RED_TEAM_PLAYER2");
        initRoleGfx("BLUE_TEAM_LEADER");
        initRoleGfx("BLUE_TEAM_PLAYER1");
        initRoleGfx("BLUE_TEAM_PLAYER2");

        try {
            Thread.sleep(2000);
        } catch (Exception e) {
        }
        ReferenceHolder.getVisibilityManager().startGame("RED_TEAM_LEADER");
        ReferenceHolder.getVisibilityManager().startGame("RED_TEAM_PLAYER1");
        ReferenceHolder.getVisibilityManager().startGame("RED_TEAM_PLAYER2");
        ReferenceHolder.getVisibilityManager().startGame("BLUE_TEAM_LEADER");
        ReferenceHolder.getVisibilityManager().startGame("BLUE_TEAM_PLAYER1");
        ReferenceHolder.getVisibilityManager().startGame("BLUE_TEAM_PLAYER2");

        AutomatonEvent evt = new AutomatonEvent("FIRE", 100);
        ReferenceHolder.getQManager().addEventToQueue(evt, 200, 50);

        /*
         * for(int i = 0; i < 100; i++) {
         * int x = (int) Math.floor(100*Math.random());
         * int y = (int) Math.floor(100*Math.random());
         * ReferenceHolder.getQManager().addEventToQueue(evt, x, y);
         * }
         */
        while (true) {
            try {
                Thread.sleep(1000);
                logger.log("ServerTest: Has slept for 1 second, invoking action on void.");
                try {

                    server.actionOnPosition("RED_TEAM_PLAYER1", actionCtr++, 0, "MOVE", 200, 50);
                    server.actionOnPosition("RED_TEAM_PLAYER2", actionCtr++, 1, "MOVE", 200, 50);
                    server.actionOnPosition("BLUE_TEAM_PLAYER1", actionCtr++, 2, "MOVE", 200, 50);
                    server.actionOnPosition("BLUE_TEAM_PLAYER2", actionCtr++, 3, "MOVE", 200, 50);

                    Thread.sleep(1000000);
                } catch (Exception ex) {
                    System.out.println("EXCEPTION: " + ex.getMessage());
                    actionCtr--;
                    server.abortAction("RED_TEAM_PLAYER1", actionCtr - 1);
                }

                logger.log("ServerTest: Action invoked, sleeping...");
                Thread.sleep(3000);

            } catch (Exception ex) {
                System.out.println("EXCEPTION: " + ex.getMessage());

            }
        }
    }


    /**
     *  Description of the Method
     *
     * @param  role  Description of Parameter
     */
    private void initRoleGfx(String role) {
        BufferedImage bufImg = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < d.width; x++) {
            for (int y = 0; y < d.height; y++) {
                Color c = new Color(cols[y * d.width + x]);
                bufImg.setRGB(x, y, c.darker().getRGB());
            }
        }
        ImageIcon icon = new ImageIcon(bufImg);
        JLabel label = new JLabel(icon);
        JFrame frame = new JFrame("Role: '" + role + "'");

        frame.addWindowListener(
            new WindowAdapter() {
                /**
                 *  Description of the Method
                 *
                 * @param  e  Description of Parameter
                 */
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        role2BufferedImage.put(role, bufImg);
        role2Frame.put(role, frame);
    }


    /**
     *  Description of the Method
     *
     * @param  role  Description of Parameter
     */
    public static void actionCompleted(String role) {
        double x = Math.random() * ReferenceHolder.getAutomatonMap().getXBound();
        double y = Math.random() * ReferenceHolder.getAutomatonMap().getYBound();

        if (role.equals("RED_TEAM_PLAYER1")) {
            server.actionOnPosition(role, actionCtr++, 0, "MOVE", x, y);
        } else if (role.equals("RED_TEAM_PLAYER2")) {
            server.actionOnPosition(role, actionCtr++, 1, "MOVE", x, y);
        } else if (role.equals("BLUE_TEAM_PLAYER1")) {
            server.actionOnPosition(role, actionCtr++, 2, "MOVE", x, y);
        } else if (role.equals("BLUE_TEAM_PLAYER2")) {
            server.actionOnPosition(role, actionCtr++, 3, "MOVE", x, y);
        }
    }


    /**
     *  Description of the Method
     *
     * @param  role  Description of Parameter
     */
    public static void refresh(String role) {
        JFrame frame = (JFrame) role2Frame.get(role);
        if (frame != null) {
            frame.repaint();
        }
    }


    /**
     *  Description of the Method
     *
     * @param  role  Description of Parameter
     * @param  x     Description of Parameter
     * @param  y     Description of Parameter
     * @param  c     Description of Parameter
     */
    public static void updatePixel(String role, int x, int y, Color c) {
        BufferedImage bufImg = (BufferedImage) role2BufferedImage.get(role);
        if (bufImg != null) {
            bufImg.setRGB(x, y, c.getRGB());
            JFrame frame = (JFrame) role2Frame.get(role);
            frame.repaint();
        }
    }


    /**
     *  Description of the Method
     *
     * @param  role  Description of Parameter
     * @param  x     Description of Parameter
     * @param  y     Description of Parameter
     */
    public static void restorePixel(String role, int x, int y) {
        BufferedImage bufImg = (BufferedImage) role2BufferedImage.get(role);
        if (bufImg != null) {
            Color c = new Color(bufImg.getRGB(x, y));
            bufImg.setRGB(x, y, c.darker().getRGB());
            JFrame frame = (JFrame) role2Frame.get(role);
            frame.repaint();
        }
    }
}
