import 'package:flutter/material.dart';
import 'package:super_tooltip/super_tooltip.dart';

class ApprovalTooltip extends StatefulWidget {
  final List<Map<String, dynamic>> planStatusList;
  final bool hasApprovals;
  final String statusText;
  final bool isApproverApproved;
  final bool isApproverRejected;
  final Color Function(String) getStatusColor;

  const ApprovalTooltip({
    super.key,
    required this.planStatusList,
    required this.hasApprovals,
    required this.statusText,
    required this.isApproverApproved,
    required this.isApproverRejected,
    required this.getStatusColor,
  });

  @override
  State<ApprovalTooltip> createState() => _ApprovalTooltipState();
}

class _ApprovalTooltipState extends State<ApprovalTooltip> {
  late SuperTooltip tooltip;

  @override
  void initState() {
    super.initState();
    tooltip = SuperTooltip(
      popupDirection: TooltipDirection.down,
      content: _buildTooltipContent(),
      borderColor: Colors.grey.shade300,
      backgroundColor: Colors.white,
      shadowColor: Colors.black38,
      barrierColor: Colors.transparent,
    );
  }

  Widget _buildTooltipContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (!widget.hasApprovals)
          Center(
            child: Text(
              "---  No Approvals ---",
              style: const TextStyle(
                fontFamily: "Archivo",
                fontSize: 11,
                fontWeight: FontWeight.w500,
                color: Colors.black87,
              ),
            ),
          ),
        for (int i = 0; i < widget.planStatusList.length; i++) ...[
          if (widget.planStatusList[i].entries.any((entry) =>
              entry.key.contains('status') &&
              entry.value != null &&
              entry.value.toString().isNotEmpty)) ...[
            _buildApprovalItem(
              "Approver ${i + 1}",
              widget.planStatusList[i].entries
                  .firstWhere(
                    (entry) => entry.key.contains('status'),
                    orElse: () => MapEntry('', ''),
                  )
                  .value
                  .toString(),
            ),
            const SizedBox(height: 6),
          ],
        ],
      ],
    );
  }

  Widget _buildApprovalItem(String title, String value) {
    return Row(
      children: [
        Text(
          title,
          style: const TextStyle(
            fontSize: 12,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(width: 8),
        Text(
          value,
          style: const TextStyle(
            fontSize: 12,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color: widget.getStatusColor(widget.statusText)),
          borderRadius: BorderRadius.circular(8),
        ),
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 6),
        child: Text(
          widget.statusText,
          style: TextStyle(
            fontFamily: "Roboto",
            fontWeight: FontWeight.w400,
            fontSize: 12,
            color: widget.getStatusColor(
              widget.isApproverApproved
                  ? "Approved"
                  : widget.isApproverRejected
                      ? "Rejected"
                      : (widget.statusText),
            ),
          ),
        ),
      ),
    );
  }
}
