import 'dart:math' as math;
import 'package:siro_rider/constant/box_name.dart';
import 'package:siro_rider/main.dart';
import 'package:siro_rider/splash_screen_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class CouponPainter extends CustomPainter {
  final Color primaryColor;
  final Color secondaryColor;

  CouponPainter({
    required this.primaryColor,
    required this.secondaryColor,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final Paint primaryPaint = Paint()
      ..color = primaryColor
      ..style = PaintingStyle.fill; //

    final Paint secondaryPaint = Paint()
      ..color = secondaryColor
      ..style = PaintingStyle.fill;

    final Path path = Path();

    // Draw the main ticket shape
    path.moveTo(0, size.height * 0.1);
    path.lineTo(size.width * 0.93, size.height * 0.1);
    path.arcToPoint(
      Offset(size.width, size.height * 0.2),
      radius: const Radius.circular(20),
      clockwise: false,
    );
    path.lineTo(size.width, size.height * 0.8);
    path.arcToPoint(
      Offset(size.width * 0.93, size.height * 0.9),
      radius: const Radius.circular(20),
      clockwise: false,
    );
    path.lineTo(0, size.height * 0.9);
    path.close();

    canvas.drawPath(path, primaryPaint);

    // Draw decorative circles on the left side
    for (int i = 0; i < 5; i++) {
      canvas.drawCircle(
        Offset(0, size.height * (0.2 + i * 0.15)),
        10,
        secondaryPaint,
      );
    }

    // Draw a wavy pattern on the right side
    final wavePaint = Paint()
      ..color = secondaryColor.withOpacity(0.3)
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2;

    for (int i = 0; i < 20; i++) {
      canvas.drawLine(
        Offset(size.width * 0.8, i * 10.0),
        Offset(
          size.width,
          i * 10.0 + math.sin(i * 0.5) * 10,
        ),
        wavePaint,
      );
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

class PromoBanner extends StatelessWidget {
  final String promoCode;
  final String discountPercentage;
  final String validity;

  const PromoBanner({
    Key? key,
    required this.promoCode,
    required this.discountPercentage,
    required this.validity,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: CouponPainter(
        primaryColor: Colors.blue[800]!,
        secondaryColor: Colors.white,
      ),
      child: Container(
        width: 320,
        height: 240,
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            const SizedBox(
              height: 10,
            ),
            Text(
              'Enter the promo code and get'.tr,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
              textAlign: TextAlign.center,
            ),
            Text(
              '${'DISCOUNT'.tr} $discountPercentage ${'for'.tr} $validity'
                  .toUpperCase(),
              style: const TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 10),
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.1),
                    blurRadius: 4,
                    offset: const Offset(0, 2),
                  ),
                ],
              ),
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              child: Text(
                promoCode,
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue[800],
                ),
              ),
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              onPressed: () {
                // Copy promo code to clipboard
                Clipboard.setData(ClipboardData(text: promoCode));
                // Show a Snackbar or other feedback to the user
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Promo code copied to clipboard!'.tr)),
                );
                box.write(BoxName.isGiftToken, '1');
                box.write(BoxName.isFirstTime, '1');
                Get.back();
              },
              style: ElevatedButton.styleFrom(
                foregroundColor: Colors.blue[800], // Customize the color
                backgroundColor: Colors.white, // Customize the background color
              ),
              child: Text('Copy Code'.tr),
            )
          ],
        ),
      ),
    );
  }
}

// import 'package:siro_rider/constant/colors.dart';
// import 'package:flutter/cupertino.dart';
// import 'package:flutter/material.dart';

// class CouponPainter extends CustomPainter {
//   @override
//   void paint(Canvas canvas, Size size) {
//     final Paint paint = Paint()
//       ..color = AppColor.blueColor
//       ..style = PaintingStyle.fill;

//     final Path path = Path();

//     // Draw the ticket shape (like the image)
//     path.moveTo(0, 0);
//     path.lineTo(size.width * 0.7, 0); // top left to top right edge

//     // Draw curve for the cut on the right side (ticket look)
//     path.arcToPoint(Offset(size.width, size.height * 0.15),
//         radius: const Radius.circular(15), clockwise: false);
//     path.lineTo(size.width, size.height * 0.85);
//     path.arcToPoint(Offset(size.width * 0.7, size.height),
//         radius: const Radius.circular(15), clockwise: false);
//     path.lineTo(0, size.height);

//     canvas.drawPath(path, paint);
//   }

//   @override
//   bool shouldRepaint(CustomPainter oldDelegate) {
//     return false;
//   }
// }

// class PromoBanner extends StatelessWidget {
//   final String promoCode;
//   final String discountPercentage;
//   final String validity;

//   const PromoBanner({
//     required this.promoCode,
//     required this.discountPercentage,
//     required this.validity,
//   });

//   @override
//   Widget build(BuildContext context) {
//     return CustomPaint(
//       painter: CouponPainter(),
//       child: Container(
//         width: 300, // Fixed width for the promo banner
//         height: 180, // Set the desired height for your banner
//         padding: const EdgeInsets.all(16),
//         child: Column(
//           mainAxisAlignment: MainAxisAlignment.spaceAround,
//           children: [
//             Text(
//               'Enter the promo code and get'.toUpperCase(),
//               style: const TextStyle(
//                 fontSize: 16,
//                 fontWeight: FontWeight.bold,
//                 color: Colors.white,
//               ),
//               textAlign: TextAlign.center,
//             ),
//             Text(
//               '$discountPercentage OFF for $validity'.toUpperCase(),
//               style: const TextStyle(
//                 fontSize: 18,
//                 fontWeight: FontWeight.bold,
//                 color: Colors.white,
//               ),
//               textAlign: TextAlign.center,
//             ),
//             const SizedBox(height: 10),
//             Container(
//               decoration: BoxDecoration(
//                 color: Colors.white,
//                 borderRadius: BorderRadius.circular(10),
//               ),
//               padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
//               child: Text(
//                 promoCode,
//                 style: TextStyle(
//                   fontSize: 20,
//                   fontWeight: FontWeight.bold,
//                   color: Colors.blue[800],
//                 ),
//               ),
//             ),
//           ],
//         ),
//       ),
//     );
//   }
// }
