import 'package:flutter/material.dart';

import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../domain/order/order.dart';
import '../../../components/spaces/space.dart';

class OrderListPayment extends StatelessWidget {
  final Order? order;
  const OrderListPayment({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: AppStyle.lg.copyWith(
                  fontWeight: FontWeight.bold,
                  color: AppColor.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.empty(),
              ),
            ),
          ),
          const SpaceHeight(4),
          const Divider(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                'Jumlah yang Dibayar',
                style: AppStyle.md.copyWith(color: Colors.grey.shade700),
              ),
              Text(
                (order?.totalPaid ?? 0).currencyFormatRpV2,
                style: const 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: AppColor.success.withOpacity(0.2),
            borderRadius: BorderRadius.circular(4),
          ),
          child: Text(
            (order?.paymentStatus ?? "").toTitleCase(),
            style: AppStyle.xs.copyWith(
              fontSize: 10,
              fontWeight: FontWeight.bold,
              color: AppColor.success,
            ),
          ),
        );
      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: AppStyle.xs.copyWith(
              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: AppStyle.md.copyWith(fontWeight: FontWeight.w500),
              ),
              if ((payment.splitTotal) > 1)
                Text(
                  'Split ${payment.splitNumber} of ${payment.splitTotal}',
                  style: AppStyle.md.copyWith(color: Colors.grey.shade600),
                ),
            ],
          ),
        ),
        Text(
          (payment.amount).currencyFormatRpV2,
          style: AppStyle.md.copyWith(
            fontWeight: FontWeight.w600,
            color: AppColor.success,
          ),
        ),
      ],
    );
  }
}
