import 'package:flutter/material.dart';
import '../constants/mpob_colors.dart';
import '../services/app_strings.dart';

/// A shared widget for displaying MPOB-standard bounding boxes.
class PalmBoundingBox extends StatelessWidget {
  final Rect normalizedRect;
  final String label;
  final double confidence;
  final BoxConstraints constraints;
  final bool isLocked;
  final bool isManual;
  final double opacity;

  const PalmBoundingBox({
    super.key,
    required this.normalizedRect,
    required this.label,
    required this.confidence,
    required this.constraints,
    this.isLocked = false,
    this.isManual = false,
    this.opacity = 1.0,
  });

  /// MPOB-standard color palette
  Color _getMPOBColor(String label) {
    if (isLocked) return const Color(0xFF22C55E); // Force Green when locked
    return getMPOBColor(label);
  }

  @override
  Widget build(BuildContext context) {
    final color = _getMPOBColor(label);

    return Positioned(
      left: normalizedRect.left * constraints.maxWidth,
      top: normalizedRect.top * constraints.maxHeight,
      width: normalizedRect.width * constraints.maxWidth,
      height: normalizedRect.height * constraints.maxHeight,
      child: Opacity(
        opacity: opacity,
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(color: color, width: 3),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Align(
            alignment: Alignment.topLeft,
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
              color: color,
              child: Text(
                "${AppStrings.className(label)} ${(confidence * 100).toStringAsFixed(0)}%${isManual ? AppStrings.t('added_suffix') : ''}",
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 10,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
