/*
 * 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.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import org.wyrez.shootingstars.data.AudioDataManager;

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

    private static final float GLOW_DURATION = 0.25f;
    /**/
    private AudioDataManager audioDataManager;
    private Material material;
    private float glowTimer = 0;
    private boolean reset = false;

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

    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        this.material = ((Geometry) spatial).getMaterial();
        this.material.setColor("GlowColor", ColorRGBA.Black);
    }

    @Override
    public void update(float tpf) {
        if (audioDataManager.isGroundMove()) {
            glowTimer = GLOW_DURATION;
            reset = true;
        }

        if (glowTimer > 0f) {
            float glowFactor = (glowTimer / GLOW_DURATION);
            if (glowFactor > 0.5f) {
                glowFactor = 1f;
            } else {
                glowFactor += 0.5f;
            }
            float glowValue = (0.75f + (audioDataManager.getGroundMoveData() * 0.25f)) * glowFactor;
            material.setColor("GlowColor", new ColorRGBA(glowValue, glowValue, glowValue, 1f));
            glowTimer -= tpf;
        } else if (reset) {
            material.setColor("GlowColor", ColorRGBA.Black);
            reset = false;
        }
    }

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