import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/data/models/response/discount_response_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/presentation/home/bloc/checkout/checkout_bloc.dart';

import '../../../core/constants/colors.dart';
import '../../setting/bloc/discount/discount_bloc.dart';

class DiscountDialog extends StatefulWidget {
  const DiscountDialog({super.key});

  @override
  State<DiscountDialog> createState() => _DiscountDialogState();
}

class _DiscountDialogState extends State<DiscountDialog> {
  @override
  void initState() {
    context.read<DiscountBloc>().add(const DiscountEvent.getDiscounts());
    super.initState();
  }

  int discountIdSelected = 0;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      backgroundColor: AppColors.white,
      title: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Pilih Diskon',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: AppColors.black,
                  ),
                ),
                const SizedBox(height: 4.0),
                Text(
                  'Pilih diskon yang ingin diterapkan pada pesanan',
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ),
          SpaceWidth(12),
          IconButton(
            icon: const Icon(Icons.close, color: AppColors.black),
            onPressed: () {
              context.pop();
            },
          ),
        ],
      ),
      content: BlocBuilder<DiscountBloc, DiscountState>(
        builder: (context, state) {
          return state.maybeWhen(
            orElse: () => const SizedBox.shrink(),
            loading: () => const Center(
              child: CircularProgressIndicator(),
            ),
            loaded: (discounts) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: discounts
                    .map((discount) => _buildDiscountItem(discount))
                    .toList(),
              );
            },
          );
        },
      ),
    );
  }

  Widget _buildDiscountItem(Discount discount) {
    return GestureDetector(
      onTap: () {
        setState(() {
          discountIdSelected = discount.id!;
          context.read<CheckoutBloc>().add(
                CheckoutEvent.addDiscount(
                  discount,
                ),
              );
        });
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
        margin: const EdgeInsets.only(bottom: 8.0),
        decoration: BoxDecoration(
          color: discountIdSelected == discount.id
              ? AppColors.primary
              : AppColors.white,
          borderRadius: BorderRadius.circular(8.0),
          border: Border.all(
            color: discountIdSelected == discount.id
                ? AppColors.primary
                : AppColors.grey,
            width: 1.0,
          ),
        ),
        child: Row(
          children: [
            Expanded(
              child: Text(
                "${discount.name} (${discount.value})",
                style: TextStyle(
                  fontSize: 16,
                  color: discountIdSelected == discount.id
                      ? AppColors.white
                      : AppColors.black,
                  fontWeight: FontWeight.w500,
                ),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            SpaceWidth(12.0),
            Icon(Icons.check_circle,
                color: discountIdSelected == discount.id
                    ? AppColors.green
                    : Colors.transparent),
          ],
        ),
      ),
    );
  }
}
