//
// $Id$
//
// Nenya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/nenya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.jme.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.system.DisplaySystem;

import com.samskivert.util.StringUtil;

import com.threerings.jme.Log;

/**
 * Some static classes and methods of general utility to applications using JME.
 */
public class JmeUtil
{
    /**
     * Represents the current position and direction in an animation composed of a fixed number of
     * discrete frames using one of the repeat types defined in {@link Controller}.
     */
    public static class FrameState
    {
        /** The index of the current frame. */
        public int idx;

        /** The current direction of animation. */
        public int dir = +1;

        /** The fractional progress towards the next frame. */
        public float accum;

        /**
         * Resets the state back to the beginning.
         */
        public void reset ()
        {
            set(0, +1, 0f);
        }

        /**
         * Sets the entire frame state.
         */
        public void set (int idx, int dir, float accum)
        {
            this.idx = idx;
            this.dir = dir;
            this.accum = accum;
        }

        /**
         * Updates the frame state after some amount of time has elapsed.
         */
        public void update (float elapsed, float frameRate, int frameCount, int repeatType)
        {
            float frames = elapsed * frameRate;
            for (accum += frames; accum >= 1f; accum -= 1f) {
                advance(frameCount, repeatType);
            }
        }

        /**
         * Advances the position by one frame.
         */
        public void advance (int frameCount, int repeatType)
        {
            if ((idx += dir) >= frameCount) {
                if (repeatType == Controller.RT_CLAMP) {
                    idx = frameCount - 1;
                    dir = 0;
                } else if (repeatType == Controller.RT_WRAP) {
                    idx = 0;
                } else { // repeatType == Controller.RT_CYCLE
                    idx = frameCount - 2;
                    dir = -1;
                }
            } else if (idx < 0) {
                idx = 1;
                dir = +1;
            }
        }
    }

    /**
     * Attempts to parse a string containing an axis: either "x", "y", or "z", or three
     * comma-delimited values representing an axis vector.  The value returned may be one of
     * JME's "constant" vectors (for example, {@link Vector3f#UNIT_X}), so don't modify it.
     */
    public static Vector3f parseAxis (String axis)
    {
        if ("x".equalsIgnoreCase(axis)) {
            return Vector3f.UNIT_X;
        } else if ("y".equalsIgnoreCase(axis)) {
            return Vector3f.UNIT_Y;
        } else if ("z".equalsIgnoreCase(axis)) {
            return Vector3f.UNIT_Z;
        } else {
            Vector3f vector = parseVector3f(axis);
            if (vector != null) {
                vector.normalizeLocal();
            }
            return vector;
        }
    }

    /**
     * Attempts to parse a string containing three comma-delimited values and return a
     * {@link Vector3f}.
     */
    public static Vector3f parseVector3f (String vector)
    {
        if (vector != null) {
            float[] vals = StringUtil.parseFloatArray(vector);
            if (vals != null && vals.length == 3) {
                return new Vector3f(vals[0], vals[1], vals[2]);
            } else {
                Log.warning("Invalid vector [vector=" + vector + "].");
            }
        }
        return null;
    }

    /**
     * Attempts to parse a string describing one of the repeat types defined in {@link Controller}:
     * "clamp", "cycle", or "wrap".  Will return the specified default if the type is
     * <code>null</code> or invalid.
     */
    public static int parseRepeatType (String type, int defaultType)
    {
        if ("clamp".equals(type)) {
            return Controller.RT_CLAMP;
        } else if ("cycle".equals(type)) {
            return Controller.RT_CYCLE;
        } else if ("wrap".equals(type)) {
            return Controller.RT_WRAP;
        } else if (type != null) {
            Log.warning("Invalid repeat type [type=" + type + "].");
        }
        return defaultType;
    }

    /**
     * Loads the specified shaders (prepending the supplied preprocessor definitions)
     * and returns a GLSL shader state.  One of the supplied files may be <code>null</code>
     * in order to use the fixed-function pipeline for that part.
     *
     * @param defs a number of preprocessor definitions to be #defined in both shaders
     * (e.g., "ENABLE_FOG", "NUM_LIGHTS 2").
     */
    public static GLSLShaderObjectsState loadShaders (File vert, File frag, String... defs)
    {
        GLSLShaderObjectsState sstate =
            DisplaySystem.getDisplaySystem().getRenderer().createGLSLShaderObjectsState();
        sstate.load(
            (vert == null) ? null : readSource(vert, defs),
            (frag == null) ? null : readSource(frag, defs));
        return sstate;
    }

    /**
     * Loads an entire source file as a string, prepending the supplied preprocessor definitions.
     */
    protected static String readSource (File file, String[] defs)
    {
        StringBuffer buf = new StringBuffer();
        String ln = System.getProperty("line.separator");
        for (String def : defs) {
            buf.append("#define ").append(def).append(ln);
        }
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                buf.append(line).append(ln);
            }
        } catch (IOException e) {
            return null;
        }
        return buf.toString();
    }
}
