/*
 * 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.game;

import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import java.util.Random;
import org.wyrez.shootingstars.controls.StarDeathControl;
import org.wyrez.shootingstars.controls.StarFaceControl;
import org.wyrez.shootingstars.controls.StarPointControl;
import org.wyrez.shootingstars.data.AudioDataManager;
import org.wyrez.shootingstars.factories.Materials;
import org.wyrez.shootingstars.helper.MathHelper;
import org.wyrez.shootingstars.helper.UserDataKeys;
import org.wyrez.shootingstars.mesh.Star;
import org.wyrez.shootingstars.states.GameState;

/**
 *
 * @author Darth Affe
 */
public class StarManager {

    private static final Random random;
    private static final float MAX_STAR_SIZE = 30f;
    private static final float VAR_STAR_SIZE = 15f;

    static {
        random = new Random();
    }
    /**/
    private AudioDataManager audioDataManager;
    private Spatial player;
    private Node starNode;

    public StarManager(AudioDataManager audioDataManager, Spatial player, Node starNode) {
        this.audioDataManager = audioDataManager;
        this.player = player;
        this.starNode = starNode;
        addLight();
    }

    public void update(float tpf) {
        float[] data = audioDataManager.getSpawnData();
        if (data == null) {
            return;
        }
        for (float d : data) {
            if (d > 0f) {
                spawn(calcPosition(), calcSize(d), d);
            }
        }
    }

    private Vector3f calcPosition() {
        float radius = (GameState.FIELD_RADIUS - MAX_STAR_SIZE) * random.nextFloat();

        return MathHelper.calcPointOnCircle(MathHelper.degreeToRadian(random.nextFloat() * 360f),
                radius, random.nextFloat() * GameState.FIELD_HEIGHT + GameState.FIELD_LOWER_POS);
    }

    private float calcSize(float data) {
        return MAX_STAR_SIZE - VAR_STAR_SIZE * data;
    }

    private void spawn(Vector3f location, float size, float value) {
        Geometry star = new Geometry("Star", new Star(size, size * 0.2f, 0.5f));
        star.setLocalTranslation(location);
        Material mat = Materials.STAR.create();
        mat.setColor("Color", ColorRGBA.Yellow);
        star.setMaterial(mat);
        star.setUserData(UserDataKeys.SIZE, size);

        star.addControl(new StarFaceControl(player));
        star.addControl(new StarDeathControl());
        star.addControl(new StarPointControl(player, value));

        starNode.attachChild(star);
    }

    private void addLight() {
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(ColorRGBA.White);
        starNode.addLight(ambient);
    }
}
