import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/core/extensions/int_ext.dart';
import 'package:enaklo_pos/core/extensions/string_ext.dart';
import 'package:enaklo_pos/data/models/response/order_response_model.dart';
import 'package:flutter/material.dart';

class SalesPayment extends StatelessWidget {
  final Order? order;
  const SalesPayment({super.key, this.order});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                'Informasi Pembayaran',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  color: AppColors.primary,
                ),
              ),
              _buildPaymentStatus(),
            ],
          ),
          const SpaceHeight(12),
          ...List.generate(
            order?.payments?.length ?? 0,
            (index) => Padding(
              padding: const EdgeInsets.only(bottom: 8.0),
              child: _buildPaymentItem(order?.payments?[index] ?? Payment()),
            ),
          ),
          const SpaceHeight(4),
          const Divider(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                'Jumlah yang Dibayar',
                style: TextStyle(color: Colors.grey.shade700),
              ),
              Text(
                (order?.totalPaid ?? 0).currencyFormatRpV2,
                style: TextStyle(fontWeight: FontWeight.w500),
              ),
            ],
          ),
          if (((order?.totalAmount ?? 0) - (order?.totalPaid ?? 0)) != 0) ...[
            const SpaceHeight(4),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Sisa Tagihan',
                  style: TextStyle(
                    color: Colors.red.shade700,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                Text(
                  ((order?.totalAmount ?? 0) - (order?.totalPaid ?? 0))
                      .currencyFormatRpV2,
                  style: TextStyle(
                    color: Colors.red.shade700,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }

  Container _buildPaymentStatus() {
    switch (order?.paymentStatus) {
      case 'completed':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: AppColors.green.withOpacity(0.2),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text(
            (order?.paymentStatus ?? "").toTitleCase(),
            style: TextStyle(
              fontSize: 10,
              fontWeight: FontWeight.bold,
              color: AppColors.green,
            ),
          ),
        );
      default:
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: Colors.amber.shade100,
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text(
            (order?.paymentStatus ?? "").toTitleCase(),
            style: TextStyle(
              fontSize: 10,
              fontWeight: FontWeight.bold,
              color: Colors.amber.shade800,
            ),
          ),
        );
    }
  }

  Row _buildPaymentItem(Payment payment) {
    return Row(
      children: [
        Container(
          width: 32,
          height: 32,
          decoration: BoxDecoration(
            color: Colors.green.shade100,
            borderRadius: BorderRadius.circular(4),
          ),
          child: Icon(
            Icons.payments,
            color: Colors.green.shade700,
            size: 16,
          ),
        ),
        const SizedBox(width: 12),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                payment.paymentMethodName ?? "",
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                ),
              ),
              if ((payment.splitTotal ?? 0) > 1)
                Text(
                  'Split ${payment.splitNumber ?? 0} of ${payment.splitTotal ?? 0}',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey.shade600,
                  ),
                ),
            ],
          ),
        ),
        Text(
          (payment.amount ?? 0).currencyFormatRpV2,
          style: TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.w600,
            color: AppColors.green,
          ),
        ),
      ],
    );
  }
}
