import 'package:flame/components.dart';
import 'package:flutter/material.dart';
import 'package:shitman/game/components/level_components.dart';
import 'package:shitman/attributes/resetable.dart';

/// Base house component
class HouseComponent extends StructureComponent {
  static const double houseSize = 80.0;
  
  HouseType houseType;
  Vector2? doorPosition;
  Vector2? yardCenter;
  
  List<Component> securitySystems = [];
  
  HouseComponent({
    required super.gridPosition,
    required this.houseType,
  }) {
    size = Vector2.all(houseSize);
  }

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    
    // Calculate door and yard positions
    doorPosition = position + Vector2(size.x / 2, size.y);
    yardCenter = position + size / 2;
    
    appLog.fine('House loaded at $gridPosition (type: $houseType)');
  }
  
  Color _getHouseColor() {
    switch (houseType) {
      case HouseType.suburban:
        return const Color(0xFF8B4513); // Brown
      case HouseType.modern:
        return const Color(0xFF4682B4); // Blue
      case HouseType.cottage:
        return const Color(0xFF228B22); // Green
      case HouseType.apartment:
        return const Color(0xFF696969); // Gray
    }
  }

  @override
  void render(Canvas canvas) {
    // Draw house with color based on type
    final housePaint = Paint()..color = _getHouseColor();
    canvas.drawRect(size.toRect(), housePaint);

    // Draw door
    final doorPaint = Paint()..color = const Color(0xFF654321);
    canvas.drawRect(
      Rect.fromLTWH(size.x / 2 - 8, size.y - 4, 16, 4),
      doorPaint,
    );

    // Draw windows
    final windowPaint = Paint()..color = const Color(0xFF87CEEB);

    // Left window
    canvas.drawRect(
      Rect.fromLTWH(size.x * 0.2, size.y * 0.3, 12, 12),
      windowPaint,
    );

    // Right window
    canvas.drawRect(
      Rect.fromLTWH(size.x * 0.7, size.y * 0.3, 12, 12),
      windowPaint,
    );
  }
  
  /// Add a security system to this house
  void addSecuritySystem(Component security) {
    securitySystems.add(security);
    add(security);
  }
  
  /// Remove all security systems
  void clearSecuritySystems() {
    for (final security in securitySystems) {
      remove(security);
    }
    securitySystems.clear();
  }
  
  /// Check if player is detected by any security system
  bool detectsPlayer(Vector2 playerPosition, double playerStealthLevel) {
    // Basic detection based on distance to house center
    if (yardCenter == null) return false;
    final distance = (playerPosition - yardCenter!).length;
    final detectionRadius = 40.0 * (1.0 - playerStealthLevel);
    return distance < detectionRadius;
  }
  
  @override
  Future<void> reset() async {
    // Reset all security systems
    for (final security in securitySystems) {
      if (security is Resetable) {
        await (security as Resetable).reset();
      }
    }
    appLog.fine('House reset at $gridPosition');
  }
}

/// Target house variant with special highlighting
class TargetHouseComponent extends HouseComponent {
  bool isActiveTarget = false;
  
  TargetHouseComponent({
    required super.gridPosition,
    required super.houseType,
  });

  void setAsTarget(bool isTarget) {
    isActiveTarget = isTarget;
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
    
    // Draw target indicator if this is the active target
    if (isActiveTarget) {
      final targetPaint = Paint()
        ..color = const Color(0xFFFF0000)
        ..style = PaintingStyle.stroke
        ..strokeWidth = 3.0;
      canvas.drawCircle(
        Offset(size.x / 2, size.y / 2),
        size.x / 2 + 10,
        targetPaint,
      );
    }
  }
  
  @override
  Future<void> reset() async {
    await super.reset();
    isActiveTarget = false;
  }
}

enum HouseType { suburban, modern, cottage, apartment }