using Earthblend.DynamicCompile;
using Earthblend.Tts.Common.Core;
using Earthblend.Tts.Common.Core.CustomEventArgs;
using Earthblend.Tts.Common.Core.Members;
using Earthblend.Tts.Common.Core.MeshGenerators.Base;
using Earthblend.Tts.Shell.Members;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Xml.Serialization;

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

            HeadLength = 1.0D;
            Radius = .25D;
            Point1X = 0.0D;
            Slices = 16;
            ThetaStart = 0;
            ThetaStop = 16;
            Point1X = 0;
            Point1Y = -4;
            Point1Z = 0;

            Point2X = 0;
            Point2Y = 4;
            Point2Z = 0;
        }

        private ICommand _decreaseHeadLengthCommandIc;[XmlIgnore] public ICommand DecreaseHeadLengthCommandIc => _decreaseHeadLengthCommandIc ?? (_decreaseHeadLengthCommandIc = new RelayCommand<Object>(DecreaseHeadLengthCommandIcExecuted));
        public void DecreaseHeadLengthCommandIcExecuted(Object sender) { HeadLength -= MovementValue; }
        private ICommand _increaseHeadLengthCommandIc;[XmlIgnore] public ICommand IncreaseHeadLengthCommandIc => _increaseHeadLengthCommandIc ?? (_increaseHeadLengthCommandIc = new RelayCommand<Object>(IncreaseHeadLengthCommandIcExecuted));
        public void IncreaseHeadLengthCommandIcExecuted(Object sender) { HeadLength += MovementValue; }

        public static IList<String> Properties = new List<String>() { "HeadLength", "Point1X", "Point1Y", "Point1Z", "Point2X", "Point2Y", "Point2Z", "Radius", "Slices" }; //, "Stacks" };

        public static readonly DependencyProperty HeadLengthProperty = DependencyProperty.Register("HeadLength",typeof(Double),typeof(DoubleArrowMeshGenerator),new PropertyMetadata(1.0D, PropertyChanged));
        public Double HeadLength { set { SetValue(HeadLengthProperty, value); } get { return (Double)GetValue(HeadLengthProperty); }}

        public DoubleArrowMeshGenerator Duplicate()
        {
            DoubleArrowMeshGenerator DoubleArrowMeshGenerator = new DoubleArrowMeshGenerator();
            DoubleArrowMeshGenerator.HeadLength = HeadLength;
            DoubleArrowMeshGenerator.Radius = Radius;
            DoubleArrowMeshGenerator.Slices = Slices;

            DoubleArrowMeshGenerator.Point1X = Point1X;
            DoubleArrowMeshGenerator.Point1Y = Point1Y;
            DoubleArrowMeshGenerator.Point1Z = Point1Z;
            DoubleArrowMeshGenerator.Point2X = Point2X;
            DoubleArrowMeshGenerator.Point2Y = Point2Y;
            DoubleArrowMeshGenerator.Point2Z = Point2Z;

            return DoubleArrowMeshGenerator;
        }

        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.AddDoubleArrow(Point1,  Point2,  Radius*2, HeadLength,Slices,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));
        }
    }
}