/*
 * 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.asset.AssetManager;
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.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import org.wyrez.shootingstars.helper.UserDataKeys;

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

    private AssetManager assetManager;
    private Node rootNode;
    private InputManager inputManager;
    private Spatial player;

    public WeaponProjectileControl(AssetManager assetManager, Node rootNode, InputManager inputManager) {
        this.assetManager = assetManager;
        this.rootNode = rootNode;
        this.inputManager = inputManager;
        initMappings();
    }

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

    @Override
    protected void controlUpdate(float tpf) {
        if (player.getUserData(UserDataKeys.SHOOTING)) {
            shoot();
        } else {
            rootNode.detachChild(spatial);
        }
    }

    public Control cloneForSpatial(Spatial spatial) {
        WeaponProjectileControl control = new WeaponProjectileControl(assetManager, rootNode, inputManager);
        spatial.addControl(control);
        return control;
    }

    public void onAction(String name, boolean isPressed, float tpf) {
        if (name.equals("PLAYER_Mouse_Left_Click") && isPressed) {
            rootNode.attachChild(spatial);
        } else if (name.equals("PLAYER_Mouse_Left_Click") && !isPressed) {
            rootNode.detachChild(spatial);
        }
    }

    public void setPlayer(Spatial player) {
        this.player = player;
    }

    public Spatial getPlayer() {
        return this.player;
    }

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

    /*
     * Fires a projectile 
     */
    private void shoot() {
        spatial.setLocalTranslation(player.getLocalTranslation().add(0, 0, -4));

        Vector3f vectorDifference = new Vector3f(player.getLocalTranslation().subtract(player.getWorldTranslation()));
        spatial.setLocalTranslation(vectorDifference.addLocal(player.getLocalTranslation()));

        Quaternion worldDiff = new Quaternion(player.getLocalRotation().subtract(player.getWorldRotation()));
        spatial.setLocalRotation(worldDiff.addLocal(player.getLocalRotation()));

        spatial.move(player.getLocalRotation().getRotationColumn(2).mult(-3.5f));
//        spatial.move(player.getLocalRotation().getRotationColumn(1).mult(0f));
//        spatial.move(player.getLocalRotation().getRotationColumn(0).mult(0));
//        spatial.rotate(0f, FastMath.PI, 0);
    }
}
