/*
 * Copyright (C) 2013 Darth Affe <http://wyrez.org> and contributors
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */
package org.wyrez.shootingstars.controls;

import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import org.wyrez.shootingstars.gui.GameGUI;
import org.wyrez.shootingstars.helper.UserDataKeys;

/**
 *
 * @author Snowsun
 */
public class PlayerShootControl extends BaseControl implements ActionListener {

    private static final String MAPPING_PLAYER_MOUSE_LEFT_CLICK = "PLAYER_Mouse_Left_Click";
    private static final float maxTimeToOverheat = 3f;
    private static final float maxCoolDown = 3f;
    /**/
    private InputManager inputManager;
    private GameGUI gui;
    private float timeToOverheat = maxTimeToOverheat;
    private float coolDown = 0f;
    private boolean isShooting = false;

    public PlayerShootControl(InputManager inputmanager, GameGUI gui) {
        this.inputManager = inputmanager;
        this.gui = gui;
        gui.setMaxTimeToOverheat(maxTimeToOverheat);
        initMappings();
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (spatial.getUserData(UserDataKeys.RUNNING)) {
            if (isShooting) {
                if (coolDown <= 0f) {
                    timeToOverheat -= tpf;
                    gui.updateOverhead(timeToOverheat);
                    if (timeToOverheat <= 0) {
                        setShooting(false);
                        coolDown = maxCoolDown;
                    }
                } else {
                    coolDown -= tpf;
                }

            }
        }
    }

    private void setShooting(boolean isShooting) {
        this.isShooting = isShooting;
        spatial.setUserData(UserDataKeys.SHOOTING, isShooting);
    }

    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
    }

    public Control cloneForSpatial(Spatial spatial) {
        PlayerShootControl control = new PlayerShootControl(inputManager, gui);
        spatial.addControl(control);
        return control;
    }

    public void onAction(String name, boolean isPressed, float tpf) {
        if (name.equals(MAPPING_PLAYER_MOUSE_LEFT_CLICK)) {
            if (isPressed) {
                setShooting(true);
            } else {
                setShooting(false);
                timeToOverheat = maxTimeToOverheat;
            }
        }
    }

    private void initMappings() {
        inputManager.addMapping(MAPPING_PLAYER_MOUSE_LEFT_CLICK, new MouseButtonTrigger(MouseInput.BUTTON_LEFT),
                new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addListener(this, new String[]{MAPPING_PLAYER_MOUSE_LEFT_CLICK});
    }
}
