/*
 * 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.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import java.util.Random;
import org.wyrez.shootingstars.data.AudioDataManager;

/**
 *
 * @author Darth Affe
 */
public class GroundMoveControl extends BaseControl {

    private static final Random random = new Random();
    private static final float MOVE_UP_DURATION = 0.15f;
    private static final float MOVE_DOWN_DURATION = 0.3f;
    private static final float MOVE_DURATION = MOVE_UP_DURATION + MOVE_DOWN_DURATION;
    /**/
    private AudioDataManager audioDataManager;
    private float moveTimer = 0;
    private float internalTimer = 0;
    private Vector3f basePosition;
    private float moveHeight;

    public GroundMoveControl(AudioDataManager audioDataManager) {
        this.audioDataManager = audioDataManager;
    }

    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        basePosition = new Vector3f(spatial.getLocalTranslation());
    }

    @Override
    public void update(float tpf) {
        if (audioDataManager.isGroundMove() && moveTimer <= 0f) {
            if (random.nextBoolean()) {
                moveTimer = MOVE_UP_DURATION + MOVE_DOWN_DURATION;
                internalTimer = 0f;
                moveHeight = 300f * audioDataManager.getGroundMoveData()
                        * (0.5f + random.nextFloat() * 0.5f);
            }
        }

        if (moveTimer > 0f) {
            if (moveTimer < MOVE_DURATION - MOVE_UP_DURATION) {
                float moveFactor = 1f - ((internalTimer - MOVE_UP_DURATION) / MOVE_DOWN_DURATION);
                spatial.setLocalTranslation(basePosition.getX(),
                        basePosition.getY() + moveHeight * moveFactor, basePosition.getZ());
                moveTimer -= tpf;
                internalTimer += tpf;
            } else {
                float moveFactor = (internalTimer / MOVE_UP_DURATION);
                spatial.setLocalTranslation(basePosition.getX(),
                        basePosition.getY() + moveHeight * moveFactor, basePosition.getZ());
                moveTimer -= tpf;
                internalTimer += tpf;
            }
        } else if (internalTimer > 0f) {
            spatial.setLocalTranslation(basePosition.getX(),
                    basePosition.getY(), basePosition.getZ());
            internalTimer = 0f;
        }
    }

    public Control cloneForSpatial(Spatial spatial) {
        GroundMoveControl control = new GroundMoveControl(audioDataManager);
        control.setSpatial(spatial);
        return control;
    }
}
