package com.pesets.resources;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

public class TextureGenerator {
	private static TextureGenerator instance = null;
	private static final int TextureSize = 256;
	
	private FrameBuffer tileBuffer;
	private TextureRegion[][] tiles;
	private TextureRegion[][] eqiupIcons;
	private TextureRegion[][] skillIcons;
	private int width, height;
	
	private TextureGenerator()
	{
		ResourceManager.init();
		/*tileBuffer = new FrameBuffer(Format.RGB565, TextureSize, TextureSize, false);
		tiles = TextureRegion.split(tileBuffer.getColorBufferTexture(), 32, 32);
		width = tiles.length;
		height = tiles[0].length; 
		
		generateTextures();*/
		tiles = TextureRegion.split(ResourceManager.getAssetManager().get(ResourceManager.textureFolder + "tiles.png", Texture.class), 32, 32);
		
		eqiupIcons = TextureRegion.split(ResourceManager.getAssetManager().get(ResourceManager.textureFolder + "equip.png", Texture.class), 32, 32);
		skillIcons = TextureRegion.split(ResourceManager.getAssetManager().get(ResourceManager.textureFolder + "skills.png", Texture.class), 32, 32);
	}

	private void generateTextures() {
		float wx = (Gdx.graphics.getWidth() - 32)/ (float)TextureSize;
		float wy = (Gdx.graphics.getHeight() + 32)/ (float)TextureSize;
		Random rand = new Random(1);
		float ox = 0;
		float oy = 0;
		
		tileBuffer.begin();
		
		ShapeRenderer rend = new ShapeRenderer();
		rend.begin(ShapeType.FilledRectangle);
		rend.setColor(0.19f, 0.8f, 0.19f, 1);
		rend.filledRect(ox , oy, 5 * 32 * wx, 32 * wy); 
		rend.setColor(0, 1, 0, 1);
		ox += 32 * wx;
		for (int i = 0; i < 100; i++)
		{
			rend.filledRect(ox + rand.nextFloat() * 32 * wx * 4, oy + rand.nextFloat() * 32 * wy, 2 * wx, 2 * wy);
		}
		rend.end();
		
		rend.begin(ShapeType.FilledCircle);
		rend.setColor(0, 0.5f, 0, 1);
		rend.filledCircle(16 * wx, 16 * wy, 16 * wx);
		rend.end();
		
		tileBuffer.end();
	}
	
	public static void init()
	{
		instance = new TextureGenerator();
	}
	
	public static TextureGenerator getInstance()
	{
		if (instance == null)
			init();
		return instance;
	}
	
	public TextureRegion getTile(int i)
	{
		return tiles[i / 2][i % 2];
	}
	
	public TextureRegion getIcon(int i)
	{
		return eqiupIcons[i / 4][i % 4];
	}
	
	public TextureRegion getSkillIcon(int i)
	{
		return skillIcons[i / 8][i % 8];
	}
}
