import 'package:flutter/material.dart';

import '../../../../application/refund/refund_form/refund_form_bloc.dart';
import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../domain/order/order.dart';
import '../../../components/border/dashed_border.dart';
import '../../../components/page/page_title.dart';
import '../../../components/spaces/space.dart';

class RefundLeftPanel extends StatelessWidget {
  final RefundFormState state;
  const RefundLeftPanel({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(color: AppColor.white),
      child: Column(
        children: [
          PageTitle(title: 'Refund Pesanan', subtitle: state.order.orderNumber),
          _buildInfo(),
          Expanded(
            child: ListView.builder(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              itemCount: state.order.orderItems.length,
              itemBuilder: (context, index) {
                return _buildOrderItem(state.order.orderItems[index]);
              },
            ),
          ),
          _buildFooterSummary(),
        ],
      ),
    );
  }

  Widget _buildOrderItem(OrderItem item) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 12),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      item.productName,
                      style: AppStyle.lg.copyWith(
                        fontWeight: FontWeight.w600,
                        color: AppColor.textPrimary,
                      ),
                    ),
                    if (item.productVariantName.isNotEmpty)
                      Text(
                        item.productVariantName,
                        style: AppStyle.md.copyWith(
                          color: AppColor.textSecondary,
                          fontStyle: FontStyle.italic,
                        ),
                      ),
                    Text(
                      'Qty: ${item.quantity} x Rp ${item.unitPrice.currencyFormatRpV2} ',
                      style: AppStyle.md.copyWith(
                        color: AppColor.textSecondary,
                      ),
                    ),
                  ],
                ),
              ),
              Text(
                item.totalPrice.currencyFormatRpV2,
                style: AppStyle.lg.copyWith(
                  fontWeight: FontWeight.w600,
                  color: AppColor.textPrimary,
                ),
              ),
            ],
          ),
          if ((item.paidQuantity) > 1) ...[
            const SpaceHeight(6),
            Align(
              alignment: Alignment.centerRight,
              child: Container(
                padding: const EdgeInsets.symmetric(
                  horizontal: 10,
                  vertical: 4,
                ),
                decoration: BoxDecoration(
                  color: AppColor.primary.withOpacity(0.2),
                  borderRadius: BorderRadius.circular(8),
                  border: Border.all(color: AppColor.primary),
                ),
                child: RichText(
                  text: TextSpan(
                    children: [
                      TextSpan(
                        text: '${item.paidQuantity} ',
                        style: AppStyle.sm.copyWith(
                          fontWeight: FontWeight.w600,
                          color: AppColor.primary,
                        ),
                      ),
                      TextSpan(
                        text: 'dari ',
                        style: AppStyle.sm.copyWith(color: AppColor.primary),
                      ),
                      TextSpan(
                        text: '${item.quantity} ',
                        style: AppStyle.sm.copyWith(
                          fontWeight: FontWeight.w600,
                          color: AppColor.primary,
                        ),
                      ),
                      TextSpan(
                        text: 'kuantiti telah dibayar.',
                        style: AppStyle.sm.copyWith(color: AppColor.primary),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ],
      ),
    );
  }

  Container _buildInfo() {
    return Container(
      padding: const EdgeInsets.all(16.0),
      decoration: const BoxDecoration(
        border: Border(bottom: BorderSide(color: AppColor.border)),
      ),
      child: Column(
        children: [
          _buildInfoRow(
            icon: Icons.person,
            label: 'Pemesan',
            value: state.order.metadata['customer_name'] ?? "-",
          ),
          const SpaceHeight(4),
          _buildInfoRow(
            icon: Icons.restaurant_outlined,
            label: 'Tipe Pesanan',
            value: state.order.orderType,
          ),
          if (state.order.payments.isNotEmpty) ...[
            const SpaceHeight(4),
            _buildInfoRow(
              icon: Icons.wallet_outlined,
              label: 'Metode Pembayaran',
              value: state.order.payments.first.paymentMethodName,
            ),
          ],
          if (state.order.tableNumber != "") ...[
            const SpaceHeight(4),
            _buildInfoRow(
              icon: Icons.table_restaurant_outlined,
              label: 'No. Meja',
              value: state.order.tableNumber,
            ),
          ],
          const SpaceHeight(4),
          _buildInfoRow(
            icon: Icons.access_time_rounded,
            label: 'Waktu',
            value: (state.order.createdAt).toFormattedDateTime(),
          ),
        ],
      ),
    );
  }

  Container _buildFooterSummary() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: const BoxDecoration(
        border: Border(top: BorderSide(color: AppColor.border)),
      ),
      child: Column(
        children: [
          _buildSummaryItem(
            title: 'Subtotal',
            value: state.order.subtotal.currencyFormatRpV2,
          ),
          const SpaceHeight(4),
          _buildSummaryItem(
            title: 'Pajak',
            value: state.order.taxAmount.currencyFormatRpV2,
          ),
          const SpaceHeight(4),
          _buildSummaryItem(
            title: 'Diskon',
            value: state.order.discountAmount.currencyFormatRpV2,
          ),
          const SpaceHeight(8),
          const DashedDivider(color: AppColor.border),
          const SpaceHeight(8),
          _buildSummaryItem(
            title: 'Total Dibayar',
            value: state.order.totalAmount.currencyFormatRpV2,
            isTotal: true,
          ),
        ],
      ),
    );
  }

  Widget _buildInfoRow({
    required IconData icon,
    required String label,
    required String value,
    Color? valueColor,
    bool showBadge = false,
  }) {
    return Row(
      children: [
        Icon(icon, size: 18, color: AppColor.primary),
        const SizedBox(width: 12),
        Expanded(
          child: Text(
            label,
            style: AppStyle.md.copyWith(color: AppColor.textPrimary),
          ),
        ),
        if (showBadge && valueColor != null)
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
            decoration: BoxDecoration(
              color: valueColor.withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(Icons.check_circle, size: 14, color: valueColor),
                const SizedBox(width: 4),
                Text(
                  value,
                  style: AppStyle.sm.copyWith(
                    fontWeight: FontWeight.bold,
                    color: valueColor,
                  ),
                ),
              ],
            ),
          )
        else
          Text(
            value,
            style: AppStyle.md.copyWith(
              fontWeight: FontWeight.bold,
              color: valueColor ?? AppColor.primary,
            ),
          ),
      ],
    );
  }

  Row _buildSummaryItem({
    required String title,
    required String value,
    bool isTotal = false,
  }) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: AppStyle.md.copyWith(
            fontSize: isTotal ? 16 : 14,
            color: isTotal ? AppColor.primary : AppColor.textSecondary,
            fontWeight: isTotal ? FontWeight.bold : FontWeight.w500,
          ),
        ),
        Text(
          value,
          style: AppStyle.md.copyWith(
            fontSize: isTotal ? 16 : 14,
            color: isTotal ? AppColor.primary : AppColor.textSecondary,
            fontWeight: isTotal ? FontWeight.bold : FontWeight.w600,
          ),
        ),
      ],
    );
  }
}
