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

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.oresund;

import gecco.server.core.*;

/**
 *  Description of the Class
 *
 */
public class Refuel extends Action {

    /**
     *  Description of the Field
     */
    private double addX;
    /**
     *  Description of the Field
     */
    private double addY;
    /**
     *  Description of the Field
     */
    private double stepLength = 2;
    /**
     *  Description of the Field
     */
    private int range = 500;



    /**
     *  Constructor for the Refuel object
     */
    public Refuel() {
        super();
    }


    // Override superclass methods

    /**
     *  Description of the Method
     *
     * @param  _actionHandle  Description of Parameter
     * @param  _unitHandle    Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle) {
        throw new IllegalArgumentException("This action must be instanciated with a unit as an argument.");
    }


    /**
     *  Description of the Method
     *
     * @param  _actionHandle   Description of Parameter
     * @param  _unitHandle     Description of Parameter
     * @param  _argUnitHandle  Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle, int _argUnitHandle) {
        super.initiate(_actionHandle, _unitHandle, _argUnitHandle);
    }


    /**
     *  Description of the Method
     *
     * @param  _actionHandle  Description of Parameter
     * @param  _unitHandle    Description of Parameter
     * @param  _argX          Description of Parameter
     * @param  _argY          Description of Parameter
     */
    public void initiate(int _actionHandle, int _unitHandle, double _argX, double _argY) {
        throw new IllegalArgumentException("This action must be instantiated " +
					   "with a unit as an argument.");
    }


    /**
     *  Description of the Method
     *
     * @return    Description of the Returned Value
     */
    public ActionReturn checkPoint() {

        String unitMessage;

        double curX = getUnit().getX();
        double curY = getUnit().getY();
        double enemyX = getArgumentUnit().getX();
        double enemyY = getArgumentUnit().getY();

        // Check if we're inside the radius!
        double dx = enemyX - curX;
        double dy = enemyY - curY;
        double dist = dx * dx + dy * dy;

        if (dx * dx + dy * dy <= range) {
            // YES! Transfer!


            double fuel = getUnit().getDoubleProperty("Fuel");
            double maxfuel = getUnit().getDoubleProperty("MaxFuel");
            double argfuel = getArgumentUnit().getDoubleProperty("Fuel");
            if (argfuel > (maxfuel - fuel)) {
                unitMessage = "Refuel";
                getUnit().setProperty("Fuel", maxfuel);
                sendEventToUnit(getArgumentUnit(), "Refuel", (int) (maxfuel - fuel));
                if (getUnit() instanceof Helicopter) {
                    getUnit().removeBlockedAction("Fly");
                } else {
                    getUnit().removeBlockedAction("Move");
                }
            } else {
                unitMessage = "No enough Fuel!";
            }
        } else {
            unitMessage = "Target is too far away range! Closing in!";
        }

        return new ActionReturnCompleted(unitMessage);
    }
}
