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

import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.util.BufferUtils;

/**
 *
 * @author Darth Affe
 */
public class Star extends Mesh {

    private static final short[] GEOMETRY_INDICES_DATA = {
        0, 1, 2,/**/ 0, 2, 3,/**/ 0, 3, 4,/**/ 0, 4, 5,/**/ 0, 5, 6,/**/ 0, 6, 7,/**/
        0, 7, 8,/**/ 0, 8, 9,/**/ 0, 9, 10,/**/ 0, 10, 11,/**/ 0, 11, 12,/**/ 0, 12, 1,/**/
        13, 2, 1,/**/ 13, 3, 2,/**/ 13, 4, 3,/**/ 13, 5, 4,/**/ 13, 6, 5,/**/ 13, 7, 6,/**/
        13, 8, 7,/**/ 13, 9, 8,/**/ 13, 10, 9,/**/ 13, 11, 10,/**/ 13, 12, 11,/**/ 13, 1, 12};
    //TODO fix
    private static final float[] GEOMETRY_TEXTURE_DATA = {
        0.5f, 0.5f, /**/ 0.5f, 0f,/**/ 0.75f, 0.25f,/**/ 1f, 0f,/**/ 0.75f, 0.5f,/**/ 1f, 1f,/**/ 0.75f, 0.75f,
        1f, 0.5f,/**/ 0.25f, 0.75f,/**/ 0f, 1f,/**/ 0f, 0.5f,/**/ 0f, 0f,/**/ 0.25f, 0.25f,/**/ 0f, 0f
    };
    private float radius;
    private float height;
    private float factor;

    public Star(float radius, float height, float factor) {
        super();
        updateGeometry(radius, height, factor);
    }

    /**
     * Empty constructor for serialization only. Do not use!
     */
    public Star() {
        super();
    }

    @Override
    public Star clone() {
        return new Star(radius, height, factor);
    }

    protected final void updateGeometry(float radius, float height, float factor) {
        this.radius = radius;
        this.height = height;
        this.factor = factor;
        updateGeometryIndices();
        updateGeometryVertices();
        updateGeometryTextures();
    }

    protected void updateGeometryIndices() {
        if (getBuffer(VertexBuffer.Type.Index) == null) {
            setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createShortBuffer(GEOMETRY_INDICES_DATA));
        }
    }

    protected void updateGeometryTextures() {
        if (getBuffer(VertexBuffer.Type.TexCoord) == null) {
            setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(GEOMETRY_TEXTURE_DATA));
        }
    }

    protected void updateGeometryVertices() {
        Vector3f[] vertices = computeVertices();
        setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
        updateBound();
    }

    protected Vector3f[] computeVertices() {
        Vector3f[] vertices = new Vector3f[14];
        vertices[0] = new Vector3f(new Vector3f(0f, -(height / 2f), 0f));
        vertices[13] = new Vector3f(new Vector3f(0f, (height / 2f), 0f));
        //outer
        for (int i = 0; i < 12; i += 2) {
            vertices[i + 1] = new Vector3f(radius * FastMath.cos(i * FastMath.PI / 6),
                    0f, (radius * FastMath.sin(i * FastMath.PI / 6)));
        }
        //inner
        for (int i = 1; i < 12; i += 2) {
            vertices[i + 1] = new Vector3f((radius * factor) * FastMath.cos(i * FastMath.PI / 6),
                    0f, ((radius * factor) * FastMath.sin(i * FastMath.PI / 6)));
        }
        return vertices;
    }
}
