package region

import (
	"sync"
)

// WaterRegionType defines different types of regions
type WaterRegionType int32

const (
	RegionTypeUnsupported WaterRegionType = -2
	RegionTypeUntagged    WaterRegionType = -1
	RegionTypeNormal      WaterRegionType = 0
	RegionTypeWater       WaterRegionType = 1
	RegionTypeLava        WaterRegionType = 2
	RegionTypeZoneLine    WaterRegionType = 3
	RegionTypePVP         WaterRegionType = 4
	RegionTypeSlime       WaterRegionType = 5
	RegionTypeIce         WaterRegionType = 6
	RegionTypeVWater      WaterRegionType = 7
)

// WaterRegionClass defines different classes of water regions
type WaterRegionClass int32

const (
	ClassWaterVolume   WaterRegionClass = 0 // matching .region file type by name "watervol"
	ClassWaterRegion   WaterRegionClass = 1 // matching .region file type by name "waterregion"
	ClassWaterRegion2  WaterRegionClass = 2 // represents .region file name "water_region" potentially defunct
	ClassWaterOcean    WaterRegionClass = 3 // represents .region file with "ocean" and a select node as a parent
	ClassWaterCavern   WaterRegionClass = 4 // represents .region file with matches on name "ocean" and "water"
	ClassWaterOcean2   WaterRegionClass = 5 // represents .region file with matches on name "ocean"
)

// VersionRange represents a client version range for region maps
type VersionRange struct {
	MinVersion int32 // Minimum client version
	MaxVersion int32 // Maximum client version (0 = no maximum)
}

// RegionNode represents a region with environmental effects
type RegionNode struct {
	Name              string    // Region name
	EnvironmentName   string    // Environment name for effects
	GridID            uint32    // Associated grid ID
	TriggerWidgetID   uint32    // Widget that triggers this region
	Distance          float32   // Trigger distance
	Position          [3]float32 // Region center position
	BoundingBox       *BoundingBox // Region bounding box
	Active            bool      // Whether this region is active
	PlayerList        map[int32]bool // Players currently in this region
	mutex             sync.RWMutex   // Thread safety for player list
}

// BoundingBox represents a 3D bounding box for regions
type BoundingBox struct {
	MinX, MinY, MinZ float32
	MaxX, MaxY, MaxZ float32
}

// RegionMap defines the interface for region mapping implementations
type RegionMap interface {
	// ReturnRegionType returns the region type at the given location
	ReturnRegionType(location [3]float32, gridID int32) WaterRegionType
	
	// InWater checks if the location is in water
	InWater(location [3]float32, gridID int32) bool
	
	// InLava checks if the location is in lava
	InLava(location [3]float32, gridID int32) bool
	
	// InLiquid checks if the location is in any liquid
	InLiquid(location [3]float32) bool
	
	// InPvP checks if the location is in a PvP area
	InPvP(location [3]float32) bool
	
	// InZoneLine checks if the location is in a zone line
	InZoneLine(location [3]float32) bool
	
	// IdentifyRegionsInGrid identifies all regions in a grid for a client
	IdentifyRegionsInGrid(clientID int32, location [3]float32)
	
	// MapRegionsNearSpawn maps regions near a spawn
	MapRegionsNearSpawn(spawnID int32, clientID int32)
	
	// UpdateRegionsNearSpawn updates regions near a spawn
	UpdateRegionsNearSpawn(spawnID int32, clientID int32)
	
	// TicRegionsNearSpawn processes region effects for spawns
	TicRegionsNearSpawn(spawnID int32, clientID int32)
	
	// InsertRegionNode inserts a new region node
	InsertRegionNode(version int32, regionName, envName string, gridID, triggerWidgetID uint32, dist float32)
	
	// RemoveRegionNode removes a region node by name
	RemoveRegionNode(regionName string)
	
	// Load loads region data from a file
	Load(filename string) error
}

// RegionMapRange manages region maps for different client versions
type RegionMapRange struct {
	name       string                        // Zone name
	versionMap map[*VersionRange]RegionMap   // Version-specific region maps
	mutex      sync.RWMutex                  // Thread safety
}

// RegionManager manages all regions for a zone
type RegionManager struct {
	zoneName     string                    // Zone name
	regionMaps   *RegionMapRange          // Version-specific region maps
	activeNodes  map[string]*RegionNode   // Active region nodes
	playerRegions map[int32]map[string]bool // Player -> Region mapping
	mutex        sync.RWMutex             // Thread safety
	
	// Statistics
	regionChecks     int64 // Total region checks
	regionTransitions int64 // Total region transitions
	activeRegions    int32 // Currently active regions
}

// RegionEvent represents a region-related event
type RegionEvent struct {
	Type         RegionEventType // Event type
	PlayerID     int32          // Player ID
	SpawnID      int32          // Spawn ID (if applicable)
	RegionName   string         // Region name
	RegionType   WaterRegionType // Region type
	Position     [3]float32     // Position where event occurred
	Timestamp    int64          // Event timestamp
	EnterRegion  bool           // True if entering, false if leaving
}

// RegionEventType defines types of region events
type RegionEventType int32

const (
	RegionEventEnter RegionEventType = iota
	RegionEventLeave
	RegionEventUpdate
	RegionEventEnvironment
)

// RegionStats contains region system statistics
type RegionStats struct {
	TotalRegionChecks     int64   `json:"total_region_checks"`
	TotalRegionTransitions int64   `json:"total_region_transitions"`
	ActiveRegions         int32   `json:"active_regions"`
	ActivePlayers         int32   `json:"active_players"`
	RegionMapsLoaded      int32   `json:"region_maps_loaded"`
	AverageCheckTime      float64 `json:"average_check_time_ms"`
}