using Earthblend.Tts.Common.Core.CustomEventArgs;
using Earthblend.Tts.Common.Core.Members;
using Earthblend.Tts.Common.Core.MeshGenerators.Base;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace Earthblend.Tts.Common.Core.MeshGenerators
{
    public class CylinderMeshGenerator : CylindricalMeshBase
    {
        public CylinderMeshGenerator()
        {
            PropertyChanged(new DependencyPropertyChangedEventArgs());
        }

        public static readonly DependencyProperty BaseCapProperty = DependencyProperty.Register("BaseCap", typeof(Boolean), typeof(CylinderMeshGenerator), new PropertyMetadata(true, PropertyChanged));
        public static readonly DependencyProperty TopCapProperty = DependencyProperty.Register("TopCap", typeof(Boolean), typeof(CylinderMeshGenerator), new PropertyMetadata(true, PropertyChanged));

        public static IList<String> Properties = new List<String>() { "BaseCap", "TopCap", "Radius","Length", "Point1X", "Point1Y", "Point1Z", "Point2X", "Point2Y", "Point2Z","Slices","Stacks" };
        
        public Boolean BaseCap { set { SetValue(BaseCapProperty, value); } get { return (Boolean)GetValue(BaseCapProperty); } }
        public Boolean TopCap { set { SetValue(TopCapProperty, value); } get { return (Boolean)GetValue(TopCapProperty); } }

        protected virtual void Point1PropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            Point1 = new Point3D(Point1X, Point1Y, Point1Z);
        }
        protected virtual void Point2PropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            Point2 = new Point3D(Point2X, Point2Y, Point2Z);
        }

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

            meshGenerator.Point1X = Point1X;
            meshGenerator.Point1Y = Point1Y;
            meshGenerator.Point1Z = Point1Z;
            meshGenerator.Point2X = Point2X;
            meshGenerator.Point2Y = Point2Y;
            meshGenerator.Point2Z = Point2Z;
            meshGenerator.Point1 = Point1;
            meshGenerator.Point2 = Point2;
            meshGenerator.Radius = Radius;
            meshGenerator.Slices = Slices;
            meshGenerator.BaseCap = BaseCap;
            meshGenerator.TopCap = TopCap;
            meshGenerator.ThetaStart = ThetaStart;
            meshGenerator.ThetaStop = ThetaStop;

            return meshGenerator;
        }

        protected override void Triangulate(DependencyPropertyChangedEventArgs args, Point3DCollection vertices,Vector3DCollection normals, Int32Collection indices, PointCollection textures)
        {
            vertices.Clear();
            normals.Clear();
            indices.Clear();
            textures.Clear();

            CustomMeshBuilder meshBuilder = new CustomMeshBuilder();

            meshBuilder.AddCylinder(Point1, Point2, Radius, Slices, BaseCap, TopCap,ThetaStart,ThetaStop);

            vertices = meshBuilder.Positions;
            normals = meshBuilder.Normals;
            indices = meshBuilder.TriangleIndices;
            textures = meshBuilder.TextureCoordinates;

            Geometry = new MeshGeometry3D() { Positions = vertices, Normals = normals, TriangleIndices = indices, TextureCoordinates = textures };

            meshBuilder = null;

            OnMeshChanged(new MeshEventArgs(Geometry as MeshGeometry3D));
        }

    }
}