import 'package:flutter/material.dart';

import 'package:asciineverdie/src/shared/retro_colors.dart';

/// 픽셀 스타일 테두리를 그리는 CustomPainter
/// 8-bit 게임의 UI 프레임 느낌을 재현
class PixelBorderPainter extends CustomPainter {
  const PixelBorderPainter({
    this.borderWidth = 3.0,
    this.outerColor = RetroColors.panelBorderOuter,
    this.innerColor = RetroColors.panelBorderInner,
    this.fillColor,
  });

  /// 테두리 두께 (픽셀 단위로 표현)
  final double borderWidth;

  /// 외곽 테두리 색상 (어두운 색)
  final Color outerColor;

  /// 내곽 테두리 색상 (밝은 색, 입체감 표현)
  final Color innerColor;

  /// 배경 채우기 색상 (null이면 투명)
  final Color? fillColor;

  @override
  void paint(Canvas canvas, Size size) {
    final outerPaint = Paint()
      ..color = outerColor
      ..style = PaintingStyle.fill;

    final innerPaint = Paint()
      ..color = innerColor
      ..style = PaintingStyle.fill;

    // 배경 채우기
    if (fillColor != null) {
      final bgPaint = Paint()
        ..color = fillColor!
        ..style = PaintingStyle.fill;
      canvas.drawRect(
        Rect.fromLTWH(
          borderWidth,
          borderWidth,
          size.width - borderWidth * 2,
          size.height - borderWidth * 2,
        ),
        bgPaint,
      );
    }

    // 외곽 테두리 (상단, 좌측 - 어두운 색으로 깊이감)
    // 상단
    canvas.drawRect(Rect.fromLTWH(0, 0, size.width, borderWidth), outerPaint);
    // 좌측
    canvas.drawRect(Rect.fromLTWH(0, 0, borderWidth, size.height), outerPaint);

    // 외곽 테두리 (하단, 우측 - 어두운 색)
    // 하단
    canvas.drawRect(
      Rect.fromLTWH(0, size.height - borderWidth, size.width, borderWidth),
      outerPaint,
    );
    // 우측
    canvas.drawRect(
      Rect.fromLTWH(size.width - borderWidth, 0, borderWidth, size.height),
      outerPaint,
    );

    // 내곽 하이라이트 (상단, 좌측 내부 - 밝은 색으로 입체감)
    // 상단 내부
    canvas.drawRect(
      Rect.fromLTWH(
        borderWidth,
        borderWidth,
        size.width - borderWidth * 2,
        borderWidth * 0.5,
      ),
      innerPaint,
    );
    // 좌측 내부
    canvas.drawRect(
      Rect.fromLTWH(
        borderWidth,
        borderWidth,
        borderWidth * 0.5,
        size.height - borderWidth * 2,
      ),
      innerPaint,
    );
  }

  @override
  bool shouldRepaint(covariant PixelBorderPainter oldDelegate) {
    return borderWidth != oldDelegate.borderWidth ||
        outerColor != oldDelegate.outerColor ||
        innerColor != oldDelegate.innerColor ||
        fillColor != oldDelegate.fillColor;
  }
}

/// 골드 테두리 스타일 (타이틀, 중요 패널용)
class GoldBorderPainter extends PixelBorderPainter {
  const GoldBorderPainter({super.borderWidth = 3.0, super.fillColor})
    : super(
        outerColor: RetroColors.goldDark,
        innerColor: RetroColors.goldLight,
      );
}
