import com.cult3d.*;
import com.cult3d.world.*;

import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;

// You must have a texture from 3DStudio Max (or other modelling application),
// which you can change in Java later on.
// It is recommended that the texture are not smaller than 16x16.
// And the textures should be in power of 2 (2, 4, 8, 16, 32, 64, 128, 256, 1024, 2048) not neccessiraly square textures.
//
// The Java startup class must implement Cult3DScript
// To connect this class in the Designer, choose the Window->Java Actions and in the new box
// add your classes there and in the Startupclass dialog enter the name of the javaclass which
// implements Cult3DScript, for instance JavaTextureChange.class. Note you should also
// write the .class extension the first time you add the startup class.
// (In the designer after 5.0 you only had to choose your startup class in the dropdown box).
//
// How to use this class:
// Define a new color, for example "private Color red = new Color(198, 58,  41);"
// Add the variable name of that color into the colors array,
// use the method changeColor to loop through the colors.
public class ColorSwap implements Cult3DScript, Runnable
{

	// change this name to the texture you have applied in your
	// modelling application.
	private static final String TEXTURE_NAME = "MADAROSE";

    // if you want to add more colors add a Color instance here
    // and add it to the colors array. The numbers are in RGB values (0 to 255, where 0 is black and 255 is white)
    private Color red       = new Color(198, 58,  41);
    private Color blue      = new Color(99,  93,  163);
    private Color green     = new Color(138, 149, 78);
    private Color grey      = new Color(136, 136, 136);
    private Color lightBlue = new Color(121, 130, 173);
    private Color black     = new Color(64, 64, 64);
    private Color[] colors = {red,
							  blue,
							  green,
							  grey,
							  lightBlue,
							  black
							  };

///////////////////////////////////////////////////////////////////////////


   	private Texture      texture;
	private TextureImage textureImage;
	private int textureWidth, textureHeight;

    private Graphics graphics;

    // declare this variable volatile, so it always keeps the right value
	private volatile boolean loaded = false;
    private int colorCounter = 0;

	// Standard constructor
	public ColorSwap()
	{
		Thread thread = new Thread(this);
		thread.start();
	}

	// Thread method which loads "heavy" resources/textures
	// To avoid the viewer hang when loading.
	public void run()
	{
		texture = new Texture(TEXTURE_NAME);

		textureWidth  	= texture.getWidth();
		textureHeight 	= texture.getHeight();

		textureImage  	= (TextureImage)Cult.createImage(textureWidth, textureHeight);
		graphics = textureImage.getGraphics();

		loaded = true;
	}

	/*
	  Note: Lines started with two slashes (//) is line comments and is not in the final class file,
	        Lines with slash-star  is another comment which is called "block comment" which comments a block
	        of code until it encounters star-slash. As this comment is.
	*/

	// use this method from the Designer to loop the colors.
	public void changeColor(String s)
	{
		if (!loaded)
		{
			System.out.println("The Java code is not yet loaded");
			return;
		}

		if (colorCounter >= colors.length)
		{
			colorCounter = 0;
		}

		switchColor(colors[colorCounter]);
		colorCounter++;
	}


	// The actual color change
	private void switchColor(Color color)
	{
		graphics.setColor(color);
		graphics.fillRect(0,0, textureWidth,textureHeight);
		texture.setTexture(textureImage);
	}

	public void cult3dDestroy()
	{
	}
}
