using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Primitives3D
{
    /// <summary>
    /// Geometric primitive class for drawing cubes.
    /// </summary>
    public class CubePrimitive : GeometricPrimitive
    {
        #region Variables

        //public float texCoordScale = 0.7f;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructs a new cube primitive, with the specified size.
        /// </summary>
        public CubePrimitive(GraphicsDevice graphicsDevice, Vector3 minPoint, Vector3 maxPoint, float texCoordScale)
        {

            // Variables
            Vector3 pointOne;
            Vector3 pointTwo;
            Vector3 pointThree;
            Vector3 pointFour;

            ///////////////////// FRONT FACE ///////////////////////////////////
            // Front face (clockwise)
            pointOne = new Vector3(minPoint.X,minPoint.Y,maxPoint.Z);// 0
            pointTwo = new Vector3(minPoint.X, maxPoint.Y, maxPoint.Z); // 1
            pointThree = new Vector3(maxPoint.X, maxPoint.Y, maxPoint.Z); // 2
            pointFour = new Vector3(maxPoint.X, minPoint.Y, maxPoint.Z); // 3

            // First triangle
            AddIndex(0);
            AddIndex(1);
            AddIndex(2);

            // Second triangle
            AddIndex(0);
            AddIndex(2);
            AddIndex(3);

            // Four vertices per face.
            AddVertex(pointOne, new Vector3(0, 1, 0), new Vector2(0, 1) * texCoordScale); // Perfekt
            AddVertex(pointTwo, new Vector3(0, 1, 0), new Vector2(0, 0) * texCoordScale); // Perfekt
            AddVertex(pointThree, new Vector3(0, 1, 0), new Vector2(1, 0) * texCoordScale); // Perfekt
            AddVertex(pointFour, new Vector3(0, 1, 0), new Vector2(1, 1) * texCoordScale); // Perfekt
            //////////////// END FRONT FACE /////////////////////////////

            ///////////////////// BACK FACE ///////////////////////////////////
            // Back face (clockwise)
            pointOne = new Vector3(maxPoint.X, minPoint.Y, minPoint.Z); // 4
            pointTwo = new Vector3(maxPoint.X, maxPoint.Y, minPoint.Z); // 5
            pointThree = new Vector3(minPoint.X, maxPoint.Y, minPoint.Z); // 6
            pointFour = new Vector3(minPoint.X, minPoint.Y, minPoint.Z);// 7
            
            // First triangle
            AddIndex(4);
            AddIndex(5);
            AddIndex(6);

            // Second triangle
            AddIndex(4);
            AddIndex(6);
            AddIndex(7);

            // Four vertices per face.
            AddVertex(pointOne, new Vector3(0, 1, 0), new Vector2(0, 1) * texCoordScale); // Perfekt
            AddVertex(pointTwo, new Vector3(0, 1, 0), new Vector2(0, 0) * texCoordScale); // Perfekt
            AddVertex(pointThree, new Vector3(0, 1, 0), new Vector2(1, 0) * texCoordScale); // Perfekt
            AddVertex(pointFour, new Vector3(0, 1, 0), new Vector2(1, 1) * texCoordScale); // Perfekt
            ///////////////////// END BACK FACE ///////////////////////////////////

            ///////////////////// RIGHT FACE ///////////////////////////////////
            // Right face (clockwise)
            pointOne = new Vector3(minPoint.X, minPoint.Y, minPoint.Z); // 8
            pointTwo = new Vector3(minPoint.X, maxPoint.Y, minPoint.Z); // 9
            pointThree = new Vector3(minPoint.X, maxPoint.Y, maxPoint.Z); // 10
            pointFour = new Vector3(minPoint.X, minPoint.Y, maxPoint.Z); // 11

            // First triangle
            AddIndex(8);
            AddIndex(9);
            AddIndex(10);

            // Second triangle
            AddIndex(8);
            AddIndex(10);
            AddIndex(11);

            // Four vertices per face.
            AddVertex(pointOne, new Vector3(0, 1, 0), new Vector2(0, 1) * texCoordScale); // Perfekt
            AddVertex(pointTwo, new Vector3(0, 1, 0), new Vector2(0, 0) * texCoordScale); // Perfekt
            AddVertex(pointThree, new Vector3(0, 1, 0), new Vector2(1, 0) * texCoordScale); // Perfekt
            AddVertex(pointFour, new Vector3(10, 1, 0), new Vector2(1, 1) * texCoordScale); // Perfekt
            ///////////////////// END RIGHT FACE ///////////////////////////////////

            ///////////////////// LEFT FACE ///////////////////////////////////
            // Left face (clockwise)
            pointOne = new Vector3(maxPoint.X, maxPoint.Y, maxPoint.Z); // 12
            pointTwo = new Vector3(maxPoint.X, maxPoint.Y, minPoint.Z); // 13
            pointThree = new Vector3(maxPoint.X, minPoint.Y, minPoint.Z); // 14
            pointFour = new Vector3(maxPoint.X, minPoint.Y, maxPoint.Z); // 15

            // First triangle
            AddIndex(12);
            AddIndex(13);
            AddIndex(14);

            // Second triangle
            AddIndex(12);
            AddIndex(14);
            AddIndex(15);

            // Four vertices per face.
            AddVertex(pointOne, new Vector3(1, 0, 0), new Vector2(0, 0) * texCoordScale);
            AddVertex(pointTwo, new Vector3(1, 0, 0), new Vector2(1, 0) * texCoordScale);
            AddVertex(pointThree, new Vector3(1, 0, 0), new Vector2(1, 1) * texCoordScale);
            AddVertex(pointFour, new Vector3(1, 0, 0), new Vector2(0, 1) * texCoordScale);
            ///////////////////// END LEFT FACE ///////////////////////////////////

            ///////////////////// TOP FACE ///////////////////////////////////
            // Top face (clockwise)
            pointOne = new Vector3(minPoint.X, maxPoint.Y, maxPoint.Z); // 16
            pointTwo = new Vector3(minPoint.X, maxPoint.Y, minPoint.Z); // 17
            pointThree = new Vector3(maxPoint.X, maxPoint.Y, minPoint.Z); // 18
            pointFour = new Vector3(maxPoint.X, maxPoint.Y, maxPoint.Z); // 19

            // First triangle
            AddIndex(16);
            AddIndex(17);
            AddIndex(18);

            // Second triangle
            AddIndex(16);
            AddIndex(18);
            AddIndex(19);

            // Four vertices per face.
            AddVertex(pointOne, new Vector3(0, 1, 0), new Vector2(0, 1) * texCoordScale);
            AddVertex(pointTwo, new Vector3(0, 1, 0), new Vector2(0, 0) * texCoordScale);
            AddVertex(pointThree, new Vector3(0, 1, 0), new Vector2(1, 0) * texCoordScale);
            AddVertex(pointFour, new Vector3(0, 1, 0), new Vector2(1, 1) * texCoordScale);
            ///////////////////// END TOP FACE ///////////////////////////////////

            ///////////////////// BOTTOM FACE ///////////////////////////////////
            // Bottom face (clockwise)
            pointOne = new Vector3(minPoint.X, minPoint.Y, maxPoint.Z); // 20
            pointTwo = new Vector3(maxPoint.X, minPoint.Y, maxPoint.Z); // 21
            pointThree = new Vector3(maxPoint.X, minPoint.Y, minPoint.Z); // 22
            pointFour = new Vector3(minPoint.X, minPoint.Y, minPoint.Z); // 23

            // First triangle
            AddIndex(20);
            AddIndex(21);
            AddIndex(22);

            // Second triangle
            AddIndex(20);
            AddIndex(22);
            AddIndex(23);

            // Four vertices per face.
            AddVertex(pointOne, new Vector3(0, 1, 0), new Vector2(0, 1) * texCoordScale);
            AddVertex(pointTwo, new Vector3(0, 1, 0), new Vector2(0, 0) * texCoordScale);
            AddVertex(pointThree, new Vector3(0, 1, 0), new Vector2(1, 0) * texCoordScale);
            AddVertex(pointFour, new Vector3(0, 1, 0), new Vector2(1, 1) * texCoordScale);
            ///////////////////// END BOTTOM FACE ///////////////////////////////////

            // Create the rectangle
            InitializeTexturePrimitive(graphicsDevice);

        } // End of constructor

        #endregion

    } // End of class

} // End of namespace