package pathfinder

import (
	"math"
	"math/rand"
)

// NullPathfinder is a fallback pathfinder that generates straight-line paths
// This is used when no proper pathfinding data is available
type NullPathfinder struct {
	name string
}

// NewNullPathfinder creates a new null pathfinder
func NewNullPathfinder() *NullPathfinder {
	return &NullPathfinder{
		name: BackendTypeNull,
	}
}

// FindRoute generates a straight-line path between start and end points
func (np *NullPathfinder) FindRoute(start, end [3]float32, flags PathingPolyFlags) *PathfindingResult {
	return np.FindPath(start, end, GetDefaultPathfinderOptions())
}

// FindPath generates a straight-line path with the given options
func (np *NullPathfinder) FindPath(start, end [3]float32, options *PathfinderOptions) *PathfindingResult {
	// Calculate distance
	dx := end[0] - start[0]
	dy := end[1] - start[1]
	dz := end[2] - start[2]
	distance := float32(math.Sqrt(float64(dx*dx + dy*dy + dz*dz)))
	
	// Create simple two-node path (start -> end)
	path := &Path{
		Nodes: []*PathNode{
			{Position: start, Teleport: false},
			{Position: end, Teleport: false},
		},
		Partial:  false, // Null pathfinder always generates complete paths
		Distance: distance,
	}
	
	return &PathfindingResult{
		Path:      path,
		Partial:   false,
		Stuck:     false,
		Distance:  distance,
		NodeCount: 2,
	}
}

// GetRandomLocation returns a random location near the start point
func (np *NullPathfinder) GetRandomLocation(start [3]float32) [3]float32 {
	// Generate random offset within RandomLocationRadius
	angle := rand.Float32() * 2.0 * math.Pi
	distance := rand.Float32() * RandomLocationRadius
	
	return [3]float32{
		start[0] + float32(math.Cos(float64(angle)))*distance,
		start[1] + float32(math.Sin(float64(angle)))*distance,
		start[2], // Keep same Z coordinate
	}
}

// IsLoaded always returns true for null pathfinder
func (np *NullPathfinder) IsLoaded() bool {
	return true
}

// GetName returns the name of this pathfinder
func (np *NullPathfinder) GetName() string {
	return np.name
}