import 'package:flutter/material.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/enums/order_status.dart';
import 'package:sizer/sizer.dart';

class StatusStepTrigger extends StatelessWidget {
  final bool isDelivery;
  final String currentStatus;
  final void Function(String, Color, OrderStatus) onPressed;
  const StatusStepTrigger({
    super.key,
    required this.isDelivery,
    required this.currentStatus,
    required this.onPressed,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;

    final bool canConfirm =
        currentStatus == "Tertunda" || currentStatus == "Dikonfirmasi";

    final bool canInProgress =
        currentStatus == "Tertunda" ||
        currentStatus == "Dikonfirmasi" ||
        currentStatus == "Pengiriman";

    final bool canCompleted = isDelivery
        ? (currentStatus == "Pengiriman" || currentStatus == "Selesai")
        : true;

    return Container(
      padding: EdgeInsets.all(4.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(4.w),
        border: Border.all(color: Colors.grey.shade200),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.02),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Icon(
                Icons.edit_note_rounded,

                size: 5.w,
                color: AppColor.primaryColor,
              ),
              SizedBox(width: 2.w),
              Text(
                "Perbarui Status Pesanan",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: (AppFontSize.medium - (isTablet ? 0 : 1.25)).sp,
                  color: Colors.black87,
                ),
              ),
            ],
          ),
          SizedBox(height: 2.h),

          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: [
                if (isDelivery) ...[
                  _buildActionChip(
                    currentStatus == "Dikonfirmasi",
                    canConfirm,
                    "Dikonfirmasi",
                    canConfirm ? Colors.blue : Colors.grey.shade700,
                    OrderStatus.confirmed
                  ),
                  _buildActionChip(
                    currentStatus == "Pengiriman",
                    canInProgress,
                    "Pengiriman",
                    canInProgress ? Colors.orange : Colors.grey.shade700,
                    OrderStatus.in_progress
                  ),
                ],
                _buildActionChip(
                  currentStatus == "Selesai",
                  canCompleted,
                  "Selesai",
                  canCompleted ? Colors.green : Colors.grey.shade700,
                  OrderStatus.completed
                ),
                _buildActionChip(
                  currentStatus == "Dibatalkan",
                  true,
                  "Dibatalkan",
                  Colors.red,
                  OrderStatus.canceled
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildActionChip(
    bool isActive,
    bool canUpdate,
    String value,
    Color color,
    OrderStatus orderStatus
  ) {
    return Padding(
      padding: EdgeInsets.only(right: 2.w),
      child: ActionChip(
        onPressed: () =>
            !isActive && canUpdate ? onPressed.call(value, color, orderStatus) : null,
        label: Text(
          value,
          style: TextStyle(
            fontSize: AppFontSize.small.sp,
            fontWeight: FontWeight.bold,
            color: isActive ? Colors.white : color,
          ),
        ),
        backgroundColor: isActive ? color : color.withOpacity(0.05),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(2.5.w),
          side: BorderSide(color: color.withOpacity(0.2)),
        ),
        elevation: 0,
        padding: EdgeInsets.symmetric(horizontal: 3.w, vertical: 1.h),
      ),
    );
  }
}
