/*
 * 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 java.util.Random;
import org.wyrez.shootingstars.data.AudioDataManager;

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

    private static final Random random = new Random();
    private static final float GLOW_DURATION = 0.33f;
    /**/
    private AudioDataManager audioDataManager;
    private Material material;
    private float glowTimer = 0;
    private ColorRGBA color;

    public GroundGlowControl(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.isFlash() && glowTimer <= 0f) {
            if (random.nextBoolean()) {
                glowTimer = GLOW_DURATION;
                color = calcColor(audioDataManager.getFlashData());
            }
        }

        if (glowTimer > 0f) {
            float glowFactor = (glowTimer / GLOW_DURATION);
            if (glowFactor > 0.5f) {
                glowFactor = 1f;
            } else {
                glowFactor += 0.5f;
            }
            material.setColor("GlowColor", new ColorRGBA(color.r * glowFactor,
                    color.b * glowFactor, color.g * glowFactor, 1f));
            glowTimer -= tpf;
        }
    }

    private ColorRGBA calcColor(float value) {
        int r, g, b;
        if (value < 0.25f) {
            r = 0;
            g = Math.round(255 * value / 0.25f);
            b = 255;
        } else if (value < 0.5f) {
            r = 0;
            g = 255;
            b = Math.round(255 * ((0.25f - (value - 0.25f)) / 0.25f));
        } else if (value < 0.75f) {
            g = 255;
            r = Math.round(255 * (value - 0.5f) / 0.25f);
            b = 0;
        } else {
            g = Math.round(255 * ((0.25f - (value - 0.75f)) / 0.25f));
            r = 255;
            b = 0;
        }
        return new ColorRGBA((float) r / 255f, (float) g / 255f, (float) b / 255f, 1f);
    }

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