/*
 * Copyright (C) 2013 Snowsun <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 MAX_TIME_TO_OVERHEAT = 3f;
    private static final float MAX_COOLDOWN = 1f;
    /**/
    private InputManager inputManager;
    private GameGUI gui;
    private float overheatTimer = 0f;
    private float cooldownTimer = 0f;
    private boolean wasOverheat = false;
    private boolean isShooting = false;

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

    @Override
    protected void controlUpdate(float tpf) {
        if (spatial.getUserData(UserDataKeys.RUNNING)) {
            if (isShooting) {
                overheatTimer += tpf;
                if (overheatTimer >= MAX_TIME_TO_OVERHEAT) {
                    setShooting(false);
                    overheatTimer = MAX_TIME_TO_OVERHEAT;
                    cooldownTimer = MAX_COOLDOWN;
                    wasOverheat = true;
                }
            } else {
                if (cooldownTimer > 0f) {
                    cooldownTimer -= tpf;
                } else {
                    if (overheatTimer > 0f) {
                        overheatTimer -= tpf;
                    } else {
                        overheatTimer = 0f;
                        wasOverheat = false;
                    }
                }
            }
            gui.updateOverhead(overheatTimer / MAX_TIME_TO_OVERHEAT);
        }
    }

    private void setShooting(boolean isShooting) {
        if (!(wasOverheat && overheatTimer > 0f)) {
            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) && (Boolean) spatial.getUserData(UserDataKeys.RUNNING)) {
            setShooting(isPressed);
        }
    }

    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});
    }
}
