import 'package:flutter/material.dart';
import 'package:shitman/game/components/level_components.dart';
import 'dart:math';

/// Horizontal road segment
class HorizontalRoad extends RoadComponent {
  HorizontalRoad({required super.gridPosition});

  @override
  void renderRoadMarkings(Canvas canvas, Paint paint) {
    // Draw center line
    final centerY = size.y / 2;
    for (double x = 5; x < size.x; x += 15) {
      canvas.drawLine(
        Offset(x, centerY - 1),
        Offset(x + 8, centerY - 1),
        paint,
      );
    }
  }

  @override
  Future<void> reset() async {
    // Nothing to reset for road components
  }
}

/// Vertical road segment
class VerticalRoad extends RoadComponent {
  VerticalRoad({required super.gridPosition});

  @override
  void renderRoadMarkings(Canvas canvas, Paint paint) {
    // Draw center line
    final centerX = size.x / 2;
    for (double y = 5; y < size.y; y += 15) {
      canvas.drawLine(
        Offset(centerX - 1, y),
        Offset(centerX - 1, y + 8),
        paint,
      );
    }
  }

  @override
  Future<void> reset() async {
    // Nothing to reset for road components
  }
}

/// Road intersection (crossroads)
class IntersectionRoad extends RoadComponent {
  IntersectionRoad({required super.gridPosition});

  @override
  void renderRoadMarkings(Canvas canvas, Paint paint) {
    // Draw intersection markings - just corner lines
    const offset = 10.0;

    // Top-left corner
    canvas.drawLine(
      const Offset(offset, offset),
      Offset(offset + 15, offset),
      paint,
    );
    canvas.drawLine(
      const Offset(offset, offset),
      Offset(offset, offset + 15),
      paint,
    );

    // Top-right corner
    canvas.drawLine(
      Offset(size.x - offset - 15, offset),
      Offset(size.x - offset, offset),
      paint,
    );
    canvas.drawLine(
      Offset(size.x - offset, offset),
      Offset(size.x - offset, offset + 15),
      paint,
    );

    // Bottom-left corner
    canvas.drawLine(
      Offset(offset, size.y - offset - 15),
      Offset(offset, size.y - offset),
      paint,
    );
    canvas.drawLine(
      Offset(offset, size.y - offset),
      Offset(offset + 15, size.y - offset),
      paint,
    );

    // Bottom-right corner
    canvas.drawLine(
      Offset(size.x - offset, size.y - offset - 15),
      Offset(size.x - offset, size.y - offset),
      paint,
    );
    canvas.drawLine(
      Offset(size.x - offset - 15, size.y - offset),
      Offset(size.x - offset, size.y - offset),
      paint,
    );
  }

  @override
  Future<void> reset() async {
    // Nothing to reset for road components
  }
}

/// Corner road (L-shaped)
class CornerRoad extends RoadComponent {
  final CornerDirection direction;

  CornerRoad({required super.gridPosition, required this.direction});

  @override
  void renderRoadMarkings(Canvas canvas, Paint paint) {
    const curveRadius = 10.0;

    switch (direction) {
      case CornerDirection.topLeft:
        // Draw curve from top to left
        canvas.drawArc(
          Rect.fromLTWH(0, 0, curveRadius * 2, curveRadius * 2),
          0,
          pi / 2,
          false,
          paint..style = PaintingStyle.stroke,
        );
        break;
      case CornerDirection.topRight:
        // Draw curve from top to right
        canvas.drawArc(
          Rect.fromLTWH(
            size.x - curveRadius * 2,
            0,
            curveRadius * 2,
            curveRadius * 2,
          ),
          pi / 2,
          pi / 2,
          false,
          paint..style = PaintingStyle.stroke,
        );
        break;
      case CornerDirection.bottomLeft:
        // Draw curve from bottom to left
        canvas.drawArc(
          Rect.fromLTWH(
            0,
            size.y - curveRadius * 2,
            curveRadius * 2,
            curveRadius * 2,
          ),
          3 * pi / 2,
          pi / 2,
          false,
          paint..style = PaintingStyle.stroke,
        );
        break;
      case CornerDirection.bottomRight:
        // Draw curve from bottom to right
        canvas.drawArc(
          Rect.fromLTWH(
            size.x - curveRadius * 2,
            size.y - curveRadius * 2,
            curveRadius * 2,
            curveRadius * 2,
          ),
          pi,
          pi / 2,
          false,
          paint..style = PaintingStyle.stroke,
        );
        break;
    }
  }

  @override
  Future<void> reset() async {
    // Nothing to reset for road components
  }
}

/// Dead end road
class DeadEndRoad extends RoadComponent {
  final DeadEndDirection direction;

  DeadEndRoad({required super.gridPosition, required this.direction});

  @override
  void renderRoadMarkings(Canvas canvas, Paint paint) {
    // Draw a line across the dead end
    switch (direction) {
      case DeadEndDirection.north:
        canvas.drawLine(
          const Offset(10, 10),
          Offset(size.x - 10, 10),
          paint..strokeWidth = 4.0,
        );
        break;
      case DeadEndDirection.south:
        canvas.drawLine(
          Offset(10, size.y - 10),
          Offset(size.x - 10, size.y - 10),
          paint..strokeWidth = 4.0,
        );
        break;
      case DeadEndDirection.east:
        canvas.drawLine(
          Offset(size.x - 10, 10),
          Offset(size.x - 10, size.y - 10),
          paint..strokeWidth = 4.0,
        );
        break;
      case DeadEndDirection.west:
        canvas.drawLine(
          const Offset(10, 10),
          Offset(10, size.y - 10),
          paint..strokeWidth = 4.0,
        );
        break;
    }
  }

  @override
  Future<void> reset() async {
    // Nothing to reset for road components
  }
}

enum CornerDirection { topLeft, topRight, bottomLeft, bottomRight }

enum DeadEndDirection { north, south, east, west }
