import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../controller/rate/rate_app_controller.dart';

class RatingScreen extends StatelessWidget {
  final RatingController ratingController = Get.put(RatingController());

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text(
          "Rate Our App".tr,
          style: const TextStyle(fontWeight: FontWeight.bold),
        ),
      ),
      child: SafeArea(
        child: SingleChildScrollView(
          child: Center(
            child: Obx(() {
              return Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 20, vertical: 30),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    // App logo or visual
                    ClipRRect(
                      borderRadius: BorderRadius.circular(16),
                      child: Image.asset(
                        'assets/images/logo.gif',
                        height: 120,
                      ),
                    ),
                    const SizedBox(height: 20),

                    // Rating Prompt
                    Text(
                      "How would you rate our app?".tr,
                      textAlign: TextAlign.center,
                      style: CupertinoTheme.of(context)
                          .textTheme
                          .navTitleTextStyle
                          .copyWith(fontSize: 18, fontWeight: FontWeight.w600),
                    ),
                    const SizedBox(height: 20),

                    // Comment Box
                    CupertinoTextField(
                      controller: ratingController.comment,
                      placeholder: 'Write your comment here'.tr,
                      padding: const EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        border: Border.all(color: CupertinoColors.systemGrey4),
                        borderRadius: BorderRadius.circular(12),
                        color: CupertinoColors.white,
                      ),
                      prefix: Padding(
                        padding: const EdgeInsets.only(left: 8.0),
                        child: Icon(CupertinoIcons.pencil_ellipsis_rectangle,
                            color: CupertinoColors.systemGrey),
                      ),
                    ),
                    const SizedBox(height: 20),

                    // Star Rating Section
                    _buildRatingStars(),

                    const SizedBox(height: 30),

                    // Submit Button
                    CupertinoButton(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 24, vertical: 14),
                      borderRadius: BorderRadius.circular(12),
                      color: CupertinoColors.activeGreen,
                      onPressed: () {
                        if (ratingController.userRating.value > 0) {
                          ratingController
                              .submitRating(ratingController.userRating.value);
                          Get.snackbar(
                            "Thank You!".tr,
                            "Your rating has been submitted.".tr,
                            backgroundColor: CupertinoColors.systemGrey6,
                            snackPosition: SnackPosition.BOTTOM,
                            margin: const EdgeInsets.all(16),
                            borderRadius: 12,
                          );
                        } else {
                          Get.snackbar(
                            "Error".tr,
                            "Please select a rating before submitting.".tr,
                            backgroundColor: CupertinoColors.systemRed,
                            snackPosition: SnackPosition.BOTTOM,
                            margin: const EdgeInsets.all(16),
                            borderRadius: 12,
                          );
                        }
                      },
                      child: Text(
                        "Submit Rating".tr,
                        style: const TextStyle(
                          color: CupertinoColors.white,
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ],
                ),
              );
            }),
          ),
        ),
      ),
    );
  }

  // Widget for building rating stars with animations
  Widget _buildRatingStars() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(5, (index) {
        return GestureDetector(
          onTap: () {
            ratingController.userRating.value = index + 1;
          },
          child: AnimatedContainer(
            duration: const Duration(milliseconds: 300),
            margin: const EdgeInsets.symmetric(horizontal: 4),
            child: Icon(
              CupertinoIcons.star_fill,
              size: 40,
              color: index < ratingController.userRating.value
                  ? CupertinoColors.systemYellow
                  : CupertinoColors.systemGrey3,
            ),
          ),
        );
      }),
    );
  }
}
