# -*- coding: utf-8 -*-
"""
Mobile Locking Auxiliary Aiming Effect Enhancement Module
Version: 1.0
Description: Enhances aiming assistance effect with mobile locking mechanism
"""

import time
import math

class AimingEnhancement:
    def __init__(self):
        self.lock_status = False
        self.target_position = None
        self.enhancement_factor = 1.2
        self.sensitivity_threshold = 0.15
        
    def acquire_target_lock(self, target_coordinates):
        """
        Acquire lock on target coordinates
        
        Args:
            target_coordinates (tuple): (x, y) coordinates of target
        
        Returns:
            bool: Lock acquisition status
        """
        try:
            if self._validate_coordinates(target_coordinates):
                self.target_position = target_coordinates
                self.lock_status = True
                print(f"Target locked at coordinates: {target_coordinates}")
                return True
            return False
        except Exception as e:
            print(f"Lock acquisition failed: {str(e)}")
            return False
    
    def enhance_aiming_assistance(self, current_position, movement_vector):
        """
        Enhance aiming assistance based on lock status
        
        Args:
            current_position (tuple): Current (x, y) position
            movement_vector (tuple): (dx, dy) movement vector
        
        Returns:
            tuple: Enhanced (dx, dy) movement vector
        """
        if not self.lock_status or self.target_position is None:
            return movement_vector
        
        try:
            # Calculate direction to target
            target_vector = self._calculate_direction_vector(current_position, self.target_position)
            
            # Apply enhancement factor
            enhanced_vector = self._apply_enhancement(movement_vector, target_vector)
            
            # Apply smoothing
            smoothed_vector = self._smooth_movement(enhanced_vector)
            
            return smoothed_vector
            
        except Exception as e:
            print(f"Aiming enhancement error: {str(e)}")
            return movement_vector
    
    def release_lock(self):
        """Release current target lock"""
        self.lock_status = False
        self.target_position = None
        print("Target lock released")
    
    def _validate_coordinates(self, coordinates):
        """Validate input coordinates"""
        if not isinstance(coordinates, (tuple, list)):
            return False
        if len(coordinates) != 2:
            return False
        if not all(isinstance(coord, (int, float)) for coord in coordinates):
            return False
        return True
    
    def _calculate_direction_vector(self, current_pos, target_pos):
        """Calculate direction vector to target"""
        dx = target_pos[0] - current_pos[0]
        dy = target_pos[1] - current_pos[1]
        distance = math.sqrt(dx**2 + dy**2)
        
        if distance == 0:
            return (0, 0)
        
        return (dx/distance, dy/distance)
    
    def _apply_enhancement(self, movement_vec, target_vec):
        """Apply enhancement to movement vector"""
        enhanced_x = movement_vec[0] + (target_vec[0] * self.enhancement_factor)
        enhanced_y = movement_vec[1] + (target_vec[1] * self.enhancement_factor)
        
        return (enhanced_x, enhanced_y)
    
    def _smooth_movement(self, vector):
        """Apply smoothing to movement vector"""
        magnitude = math.sqrt(vector[0]**2 + vector[1]**2)
        
        if magnitude > self.sensitivity_threshold:
            # Normalize and scale
            scale_factor = self.sensitivity_threshold / magnitude
            return (vector[0] * scale_factor, vector[1] * scale_factor)
        
        return vector

# Example usage
if __name__ == "__main__":
    aim_enhancer = AimingEnhancement()
    
    # Simulate target acquisition
    target_pos = (100, 150)
    if aim_enhancer.acquire_target_lock(target_pos):
        # Simulate aiming enhancement
        current_pos = (50, 75)
        movement = (0.1, 0.2)
        enhanced_movement = aim_enhancer.enhance_aiming_assistance(current_pos, movement)
        
        print(f"Original movement: {movement}")
        print(f"Enhanced movement: {enhanced_movement}")
    
    # Release lock
    aim_enhancer.release_lock()