import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';

import 'dart:math' as math;

import 'package:core_localization/core_localization.dart';


/// Widget for displaying staff benefits, using design system tokens.
class BenefitsWidget extends StatelessWidget {
  /// Creates a [BenefitsWidget].
  const BenefitsWidget({super.key});

  @override
  Widget build(BuildContext context) {
    final i18n = t.staff.home.benefits;
    return Container(
      padding: const EdgeInsets.all(UiConstants.space4),
      decoration: BoxDecoration(
        color: UiColors.white,
        borderRadius: BorderRadius.circular(UiConstants.radiusBase),
        border: Border.all(color: UiColors.border),
        boxShadow: [
          BoxShadow(
            color: UiColors.black.withValues(alpha: 0.05),
            blurRadius: 2,
            offset: const Offset(0, 1),
          ),
        ],
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                i18n.title,
                style: UiTypography.title1m.textPrimary,
              ),
              GestureDetector(
                onTap: () => Modular.to.pushNamed('/benefits'),
                child: Row(
                  children: [
                    Text(
                      i18n.view_all,
                      style: UiTypography.buttonL.textPrimary,
                    ),
                    Icon(
                      UiIcons.chevronRight,
                      size: UiConstants.space4,
                      color: UiColors.primary,
                    ),
                  ],
                ),
              ),
            ],
          ),
          const SizedBox(height: UiConstants.space4),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _BenefitItem(
                label: i18n.items.sick_days,
                current: 10,
                total: 40,
                color: UiColors.primary,
              ),
              _BenefitItem(
                label: i18n.items.vacation,
                current: 40,
                total: 40,
                color: UiColors.primary,
              ),
              _BenefitItem(
                label: i18n.items.holidays,
                current: 24,
                total: 24,
                color: UiColors.primary,
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class _BenefitItem extends StatelessWidget {
  final String label;
  final double current;
  final double total;
  final Color color;

  const _BenefitItem({
    required this.label,
    required this.current,
    required this.total,
    required this.color,
  });

  @override
  Widget build(BuildContext context) {
    final i18n = t.staff.home.benefits;
    return Column(
      children: [
        SizedBox(
          width: UiConstants.space14,
          height: UiConstants.space14,
          child: CustomPaint(
            painter: _CircularProgressPainter(
              progress: current / total,
              color: color,
              backgroundColor: UiColors.border,
              strokeWidth: 4,
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '${current.toInt()}/${total.toInt()}',
                    style: UiTypography.body3m.textPrimary,
                  ),
                  Text(
                    i18n.hours_label,
                    style: UiTypography.footnote1r.textTertiary,
                  ),
                ],
              ),
            ),
          ),
        ),
        const SizedBox(height: UiConstants.space2),
        Text(
          label,
          style: UiTypography.body3m.textSecondary,
        ),
      ],
    );
  }
}

class _CircularProgressPainter extends CustomPainter {
  final double progress;
  final Color color;
  final Color backgroundColor;
  final double strokeWidth;

  _CircularProgressPainter({
    required this.progress,
    required this.color,
    required this.backgroundColor,
    required this.strokeWidth,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = (size.width - strokeWidth) / 2;

    final backgroundPaint = Paint()
      ..color = backgroundColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth;
    canvas.drawCircle(center, radius, backgroundPaint);

    final progressPaint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth
      ..strokeCap = StrokeCap.round;
    final sweepAngle = 2 * math.pi * progress;
    canvas.drawArc(
      Rect.fromCircle(center: center, radius: radius),
      -math.pi / 2,
      sweepAngle,
      false,
      progressPaint,
    );
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
