using Earthblend.Tts.Common.Core.Base;
using Earthblend.Tts.Common.Core.MeshGenerators.Base;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace Earthblend.Tts.Common.MeshGenerators
{
    public class IcosahedronMeshGenerator : PolyhedronMeshBase
    {
        static readonly double G = (1 + Math.Sqrt(5)) / 2;

        public static IList<String> Properties = new List<String>() { "G" };

        public IcosahedronMeshGenerator Duplicate()
        {
            IcosahedronMeshGenerator meshGenerator = new IcosahedronMeshGenerator();

            return meshGenerator;
        }

        static readonly Point3D[,] faces = new Point3D[20, 3]
        {
            { new Point3D(0, G, 1), new Point3D(-1, 0, G), new Point3D(1, 0, G) },
            { new Point3D(-1, 0, G), new Point3D(0, -G, 1), new Point3D(1, 0, G) },

            { new Point3D(0, G, 1), new Point3D(1, 0, G), new Point3D(G, 1, 0) },
            { new Point3D(1, 0, G), new Point3D(G, -1, 0), new Point3D(G, 1, 0) },

            { new Point3D(0, G, 1), new Point3D(G, 1, 0), new Point3D(0, G, -1) },
            { new Point3D(G, 1, 0), new Point3D(1, 0, -G), new Point3D(0, G, -1) },

            { new Point3D(0, G, 1), new Point3D(0, G, -1), new Point3D(-G, 1, 0) },
            { new Point3D(0, G, -1), new Point3D(-1, 0, -G), new Point3D(-G, 1, 0) },

            { new Point3D(0, G, 1), new Point3D(-G, 1, 0), new Point3D(-1, 0, G) },
            { new Point3D(-G, 1, 0), new Point3D(-G, -1, 0), new Point3D(-1, 0, G) },

            { new Point3D(1, 0, G), new Point3D(0, -G, 1), new Point3D(G, -1, 0) },
            { new Point3D(0, -G, 1), new Point3D(0, -G, -1), new Point3D(G, -1, 0) },

            { new Point3D(G, 1, 0), new Point3D(G, -1, 0), new Point3D(1, 0, -G) },
            { new Point3D(G, -1, 0), new Point3D(0, -G, -1), new Point3D(1, 0, -G) },

            { new Point3D(0, G, -1), new Point3D(1, 0, -G), new Point3D(-1, 0, -G) },
            { new Point3D(1, 0, -G), new Point3D(0, -G, -1), new Point3D(-1, 0, -G) },

            { new Point3D(-G, 1, 0), new Point3D(-1, 0, -G), new Point3D(-G, -1, 0) },
            { new Point3D(-1, 0, -G), new Point3D(0, -G, -1), new Point3D(-G, -1, 0) },

            { new Point3D(-1, 0, G), new Point3D(-G, -1, 0), new Point3D(0, -G, 1) },
            { new Point3D(-G, -1, 0), new Point3D(0, -G, -1), new Point3D(0, -G, 1) },

        };

        public IcosahedronMeshGenerator()
        {
            // Set TextureCoordinates to default values.
            PointCollection textures = TextureCoordinates;
            TextureCoordinates = null;

            textures.Add(new Point(1, 0));
            textures.Add(new Point(0, 0));
            textures.Add(new Point(1, 1));

            textures.Add(new Point(0, 0));
            textures.Add(new Point(0, 1));
            textures.Add(new Point(1, 1));

            TextureCoordinates = textures;
        }

        protected override Point3D[,] Faces
        {
            get
            {
                return faces;
            }
        }

        //protected override Freezable CreateInstanceCore()
        //{
        //    return new IcosahedronMeshGenerator();
        //}
    }
}
