using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using SurfacePlot3D.MeshGenerators;
using SurfacePlot3D.Behaviors;
using HelixToolkit.Wpf;
using SurfacePlot3D.Members;

namespace SurfacePlot3D.Services
{
    public class LetterService //: ILetterService
    {
        #region Constructor(s)

        public LetterService()
        {

        }

        #endregion

        public GeometryModel3D AddRibbonTextItem(StatePopulationDataItem masterDataItem)
        {
            var idenfitiferDataItem = new StatePopulationDataItem() { Name = "RibbonText" };
            masterDataItem.TextLinked3DObjectId = idenfitiferDataItem.Id;

            RibbonTextMeshGenerator meshGenerator = new RibbonTextMeshGenerator();
            meshGenerator.Text = masterDataItem.Text;
            meshGenerator.FontDepth = masterDataItem.FontDepth;
            meshGenerator.FontSize = masterDataItem.TextFontSize;
            meshGenerator.RotateX = masterDataItem.TextRotationX;
            meshGenerator.RotateY = masterDataItem.TextRotationY;
            meshGenerator.RotateZ = masterDataItem.TextRotationZ;
            meshGenerator.PosX = masterDataItem.PosX;
            meshGenerator.PosY = masterDataItem.PosY;
            meshGenerator.PosZ = masterDataItem.PosZ;
            meshGenerator.TextOffsetX = masterDataItem.TextOffsetPosX;
            meshGenerator.TextOffsetY = masterDataItem.TextOffsetPosY;
            meshGenerator.TextOffsetZ = masterDataItem.TextOffsetPosZ;

            GeometryModel3D geometryModel3D = new GeometryModel3D() { Material = new DiffuseMaterial(new SolidColorBrush(Colors.White)), BackMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Black)) };
            geometryModel3D.SetName(masterDataItem.Id.ToString());
            geometryModel3D.SetValue(ObjectBinder.DataItemProperty, masterDataItem);
            geometryModel3D.Geometry = meshGenerator.Geometry;

            Transform3DGroup transform3DGroup = new Transform3DGroup();

            if (masterDataItem.RotateTextX != 0D)
            {
                RotateTransform3D rotateTransform3DX = new RotateTransform3D();
                AxisAngleRotation3D axisAngleRotation3DX = new AxisAngleRotation3D() { Axis = new Vector3D(1, 0, 0), Angle = masterDataItem.RotateTextX };
                rotateTransform3DX.Rotation = axisAngleRotation3DX;
                transform3DGroup.Children.Add(rotateTransform3DX);
            }

            if (masterDataItem.RotateTextY != 0D)
            {
                RotateTransform3D rotateTransform3DY = new RotateTransform3D() { };
                AxisAngleRotation3D axisAngleRotation3DY = new AxisAngleRotation3D() { Axis = new Vector3D(0, 1, 0), Angle = masterDataItem.RotateTextY };
                rotateTransform3DY.Rotation = axisAngleRotation3DY;
                transform3DGroup.Children.Add(rotateTransform3DY);
            }

            if (masterDataItem.RotateTextZ != 0D)
            {
                RotateTransform3D rotateTransform3DZ = new RotateTransform3D();
                AxisAngleRotation3D axisAngleRotation3DZ = new AxisAngleRotation3D() { Axis = new Vector3D(0, 0, 1), Angle = masterDataItem.RotateTextZ };
                rotateTransform3DZ.Rotation = axisAngleRotation3DZ;
                transform3DGroup.Children.Add(rotateTransform3DZ);
            }

            TranslateTransform3D translateTransform3D = new TranslateTransform3D
            (
                  masterDataItem.TextOffsetPosX //masterDataItem.Ttx + 
                , masterDataItem.TextOffsetPosY //masterDataItem.Tty + 
                , masterDataItem.TextOffsetPosZ //masterDataItem.Ttz +
            );

            translateTransform3D.SetName("TestText");
            transform3DGroup.Children.Add(translateTransform3D);
            geometryModel3D.Transform = transform3DGroup;

            meshGenerator = null;

            return geometryModel3D;
        }
    }
}