using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using devDept.Eyeshot;
using devDept.Eyeshot.Control;
using devDept.Eyeshot.Entities;
using devDept.Geometry;
using WpfEngineeringSketch.Models.Scene;
using WpfEngineeringSketch.ViewModels;

namespace WpfEngineeringSketch.Tools3D
{
    public class MoveTool3D : I3DTool
    {
        public string Name => "Move";

        private System.Windows.Media.Media3D.Point3D? _startPoint;
        private SceneObject? _targetObject;
        private System.Windows.Media.Media3D.Point3D _originalPosition;
        
        private bool _isMoving = false;

        public void Activate(MainViewModel vm)
        {
            // Nothing special on activate for now
        }

        public void Deactivate(MainViewModel vm)
        {
            _isMoving = false;
            _startPoint = null;
            _targetObject = null;
        }

        public void OnMouseDown(MainViewModel vm, Design design, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                if (!_isMoving)
                {
                    // Phase 1: Pick Start Point
                    // Check if we are checking an object.
                    // If something is already selected, we move that.
                    // If nothing selected, we try to pick and move?
                    // Standard sketchup: Select tool selects, Move tool moves SELECTION.
                    
                    if (vm.SelectedObject == null) return;
                    
                    var mousePos = e.GetPosition(design);
                    
                    // Simple snap to item under mouse
                    var snap = SnappingHelper.FindClosestSnapPoint(design, mousePos, 20);
                    
                    if (snap != null)
                    {
                        _startPoint = snap;
                    }
                    else
                    {
                        // Raycast to plane Y=0?
                        design.ScreenToPlane(new System.Drawing.Point((int)mousePos.X, (int)mousePos.Y), Plane.XY, out devDept.Geometry.Point3D worldPoint);
                        _startPoint = new System.Windows.Media.Media3D.Point3D(worldPoint.X, worldPoint.Y, worldPoint.Z);
                    }

                    if (_startPoint != null)
                    {
                        _isMoving = true;
                        _targetObject = vm.SelectedObject;
                        _originalPosition = _targetObject.Position;
                    }
                }
                else
                {
                    // Phase 2: Confirm Move
                    _isMoving = false;
                    // Commit done (already updated model in MouseMove)
                }
            }
        }

        public void OnMouseMove(MainViewModel vm, Design design, MouseEventArgs e)
        {
            if (_isMoving && _targetObject != null && _startPoint != null)
            {
                var mousePos = e.GetPosition(design);
                
                // Calculate Delta
                // We need current World Point.
                // Assuming movement on XY plane for now? Or relative to camera?
                // Standard CAD move: Move in plane parallel to view or specific plane.
                // Let's assume XY Plane for simplicity of this sketch app.
                
                design.ScreenToPlane(new System.Drawing.Point((int)mousePos.X, (int)mousePos.Y), Plane.XY, out devDept.Geometry.Point3D currentPoint);
                
                if (currentPoint != null)
                {
                    // Vector delta = current - start
                    var current = new System.Windows.Media.Media3D.Point3D(currentPoint.X, currentPoint.Y, currentPoint.Z);
                    var delta = current - _startPoint.Value;
                    
                    // Update object position
                    // We must update the SceneObject properties so the SceneBinder updates the Entity/BlockRef
                    _targetObject.Position = new System.Windows.Media.Media3D.Point3D(_originalPosition.X + delta.X, 
                                                         _originalPosition.Y + delta.Y, 
                                                         _originalPosition.Z + delta.Z);
                }
            }
        }

        public void OnMouseUp(MainViewModel vm, Design design, MouseButtonEventArgs e)
        {
            // SketchUp style is Click-Move-Click usually, not Drag. 
            // So we don't end on MouseUp unless we want Drag style.
            // Let's stick to Click-Move-Click for precision as per plan.
        }

    }
}



