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

class ShiftStatusLabelWidget extends StatefulWidget {
  final ShiftEntity viewModel;

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

  @override
  State<ShiftStatusLabelWidget> createState() => _ShiftStatusLabelWidgetState();
}

class _ShiftStatusLabelWidgetState extends State<ShiftStatusLabelWidget> {
  Color getColor() {
    switch (widget.viewModel.status) {
      case EventShiftRoleStaffStatus.assigned:
        return AppColors.primaryBlue;
      case EventShiftRoleStaffStatus.confirmed:
        return AppColors.statusWarning;
      case EventShiftRoleStaffStatus.ongoing:
        return AppColors.statusSuccess;
      case EventShiftRoleStaffStatus.completed:
        return AppColors.bgColorDark;
      case EventShiftRoleStaffStatus.canceledByAdmin:
      case EventShiftRoleStaffStatus.canceledByBusiness:
      case EventShiftRoleStaffStatus.canceledByStaff:
      case EventShiftRoleStaffStatus.requestedReplace:
      case EventShiftRoleStaffStatus.declineByStaff:
        return AppColors.statusError;
    }
  }

  String getText() {
    switch (widget.viewModel.status) {
      case EventShiftRoleStaffStatus.assigned:
        return _getAssignedAgo();
      case EventShiftRoleStaffStatus.confirmed:
        return _getStartIn();
      case EventShiftRoleStaffStatus.ongoing:
        return 'ongoing'.tr();
      case EventShiftRoleStaffStatus.completed:
        return 'completed'.tr();
      case EventShiftRoleStaffStatus.declineByStaff:
        return 'declined'.tr();
      case EventShiftRoleStaffStatus.canceledByAdmin:
      case EventShiftRoleStaffStatus.canceledByBusiness:
      case EventShiftRoleStaffStatus.canceledByStaff:
      case EventShiftRoleStaffStatus.requestedReplace:
        return 'canceled'.tr();
    }
  }

  String _getStartIn() {
    var duration = widget.viewModel.startDate.difference(DateTime.now());
    var startIn = '';
    if (duration.inMinutes < 0) {
      return 'started'.tr();
    }
    if (duration.inDays > 0) {
      startIn = '${duration.inDays}d ${duration.inHours.remainder(24)}h';
    } else if (duration.inHours.abs() > 0) {
      startIn = '${duration.inHours}h ${duration.inMinutes.remainder(60)}min';
    } else {
      startIn = '${duration.inMinutes}min';
    }
    return 'starts_in'.tr(namedArgs: {'time': startIn});
  }

  String _getAssignedAgo() {
    var duration = DateTime.now().difference(widget.viewModel.assignedDate);
    var timeAgo = '';
    if (duration.inDays > 0) {
      timeAgo = '${duration.inDays}d ago';
    } else if (duration.inHours > 0) {
      timeAgo = '${duration.inHours}h ago';
    } else {
      timeAgo = '${duration.inMinutes}m ago';
    }
    return 'assigned_ago'.tr(namedArgs: {'time': timeAgo});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 24,
      padding: const EdgeInsets.symmetric(horizontal: 8),
      decoration: BoxDecoration(
        color: getColor(),
        borderRadius: BorderRadius.circular(12),
      ),
      child: Center(
        child: Text(
          getText(),
          style: AppTextStyles.bodySmallMed
              .copyWith(color: AppColors.grayWhite, height: 0.7),
        ),
      ),
    );
  }
}
