import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_driver/controller/home/profile/promos_controller.dart';
import 'package:siro_driver/views/widgets/my_scafold.dart';

import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../widgets/mycircular.dart';

class PromosPassengerPage extends StatelessWidget {
  const PromosPassengerPage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(PromosController());
    return MyScafolld(
      title: 'Promos For today'.tr,
      isleading: true,
      body: [
        GetBuilder<PromosController>(
          builder: (orderHistoryController) => orderHistoryController.isLoading
              ? const MyCircularProgressIndicator()
              : ListView.builder(
                  itemCount: orderHistoryController.promoList.length,
                  itemBuilder: (BuildContext context, int index) {
                    final rides = orderHistoryController.promoList[index];
                    return Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16, vertical: 8),
                      child: Card(
                        elevation: 4,
                        shadowColor:
                            Theme.of(context).shadowColor.withOpacity(0.1),
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(16)),
                        child: Padding(
                          padding: const EdgeInsets.all(16.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: [
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        SizedBox(
                                          height: 30,
                                          child: AnimatedTextKit(
                                            animatedTexts: [
                                              ScaleAnimatedText(
                                                  rides['promo_code'],
                                                  textStyle: Theme.of(context)
                                                      .textTheme
                                                      .titleLarge
                                                      ?.copyWith(
                                                        color: AppColor
                                                            .primaryColor,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      )),
                                              WavyAnimatedText(
                                                  rides['promo_code'],
                                                  textStyle: Theme.of(context)
                                                      .textTheme
                                                      .titleLarge
                                                      ?.copyWith(
                                                        color: AppColor
                                                            .primaryColor,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      )),
                                            ],
                                            repeatForever: true,
                                          ),
                                        ),
                                        const SizedBox(height: 8),
                                        Text(
                                          rides['description'],
                                          style: Theme.of(context)
                                              .textTheme
                                              .bodyMedium,
                                        ),
                                      ],
                                    ),
                                  ),
                                  Column(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      _buildDateBadge(context,
                                          rides['validity_start_date'], true),
                                      const SizedBox(height: 4),
                                      _buildDateBadge(context,
                                          rides['validity_end_date'], false),
                                    ],
                                  ),
                                ],
                              ),
                              const Divider(height: 24),
                              Text(
                                'Copy this Promo to use it in your Ride!'.tr,
                                textAlign: TextAlign.center,
                                style: Theme.of(context)
                                    .textTheme
                                    .labelMedium
                                    ?.copyWith(
                                      color: Theme.of(context).hintColor,
                                      fontStyle: FontStyle.italic,
                                    ),
                              )
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                ),
        )
      ],
    );
  }

  Widget _buildDateBadge(BuildContext context, String date, bool isStart) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: isStart
            ? Colors.green.withOpacity(0.1)
            : Colors.red.withOpacity(0.1),
        borderRadius: BorderRadius.circular(6),
      ),
      child: Text(
        date,
        style: TextStyle(
          fontSize: 11,
          fontWeight: FontWeight.bold,
          color: isStart ? Colors.green : Colors.red,
        ),
      ),
    );
  }
}
