using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using EVE.EVEObjects.EVEObjectComponents;

namespace EVE.EVEObjects {
	public class EVETexturedQuad : EVEObject, IEVERenderable, IEVELoadable {
		private string myAsset;

		private Texture2D myTexture;
		private VertexBuffer myVertices;
		private IndexBuffer myIndices;

		public EVETexturedQuad(string textureAsset) {
			myAsset = textureAsset;
		}

		public void LoadGraphicsContent(GraphicsDevice myDevice, ContentManager myLoader) {
			myTexture = myLoader.Load<Texture2D>(myAsset) as Texture2D;//kap00ch to ch00j

			VertexPositionTexture[] verts = {
				new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 0)),
				new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 0)),		
				new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 1)),	
				new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 1))
			};

			int[] inds = { 0, 1, 2, 1, 3, 2 };

			myVertices = new VertexBuffer(
				myDevice, typeof(VertexPositionTexture), 4,
				BufferUsage.WriteOnly
			);

			myVertices.SetData<VertexPositionTexture>(verts, 0, 4);

			myIndices = new IndexBuffer(
				myDevice, typeof(int), 6,
				BufferUsage.WriteOnly
			);

			myIndices.SetData<int>(inds, 0, 6);
		}

		public void Render(GraphicsDevice myDevice) {
			using(VertexDeclaration declaration = new VertexDeclaration(
				myDevice, VertexPositionTexture.VertexElements)
			) {
				myDevice.VertexDeclaration = declaration;

				myDevice.Textures[0] = myTexture;
				myDevice.Vertices[0].SetSource(myVertices, 0, VertexPositionTexture.SizeInBytes);

				myDevice.Indices = myIndices;
				myDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
			}
		}
	}
}