import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/features/shifts/domain/shift_entity.dart';

class ShiftInfoTotalDurationWidget extends StatelessWidget {
  final ShiftEntity viewModel;

  const ShiftInfoTotalDurationWidget({super.key, required this.viewModel});

  @override
  Widget build(BuildContext context) {
    var formattedDuration;
    if (viewModel.clockOut == null || viewModel.clockIn == null) {
      formattedDuration = '00:00:00';
    }
    formattedDuration = _format(viewModel.clockOut
            ?.difference(viewModel.clockIn ?? DateTime.now())
            .inSeconds ??
        0);
    var formattedBreak = _format(viewModel.totalBreakTime!);

    return Expanded(
      child: Container(
        padding: const EdgeInsets.all(12),
        decoration: KwBoxDecorations.primaryLight12,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('total_time_breaks'.tr().toUpperCase(),
                style: AppTextStyles.captionReg
                    .copyWith(color: AppColors.blackCaptionText)),
            const Gap(16),
            Text(formattedBreak, style: AppTextStyles.headingH3),
            const Gap(6),
            Text('break_hours'.tr(),
                style: AppTextStyles.bodySmallReg
                    .copyWith(color: AppColors.blackGray)),
            const Gap(12),
            Text(formattedDuration, style: AppTextStyles.headingH3),
            const Gap(6),
            Text('total_hours'.tr(),
                style: AppTextStyles.bodySmallReg
                    .copyWith(color: AppColors.blackGray)),
          ],
        ),
      ),
    );
  }

  String _format(int duration) {
    var hours = (duration ~/ 3600).toString().padLeft(2, '0');
    var minutes = ((duration % 3600) ~/ 60).toString().padLeft(2, '0');
    var seconds = (duration % 60).toString().padLeft(2, '0');
    return '$hours:$minutes:$seconds';
  }
}
