import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/main.dart';
import 'package:sefer_driver/views/widgets/error_snakbar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../constant/colors.dart';
import '../../../controller/home/captin/help/assurance_controller.dart';

import 'package:flutter/cupertino.dart';

class AssuranceHealthPage extends StatelessWidget {
  AssuranceHealthPage({super.key});
  AssuranceHealthController assuranceHealthController =
      Get.put(AssuranceHealthController());

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text("Health Insurance".tr),
      ),
      child: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: GetBuilder<AssuranceHealthController>(
              builder: (assuranceHealthController) {
            return Column(
              children: [
                Text(
                  "When you complete 500 trips, you will be eligible for exclusive health insurance offers."
                      .tr,
                  textAlign: TextAlign.center,
                  style: CupertinoTheme.of(context).textTheme.navTitleTextStyle,
                ),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    CupertinoButton.filled(
                      child: Text("Show My Trip Count".tr),
                      onPressed: () async {
                        assuranceHealthController.getTripCountByCaptain();
                      },
                    ),
                    _buildTripCountAvatar(
                      assuranceHealthController.tripCount['count'] == null
                          ? '0'
                          : assuranceHealthController.tripCount['count']
                              .toString(),
                    ),
                  ],
                ),
                const SizedBox(height: 10),
                Container(
                  decoration: BoxDecoration(
                    color: CupertinoColors.systemGrey6,
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(14),
                    child: Text(
                      "We have partnered with health insurance providers to offer you special health coverage. Complete 500 trips and receive a 20% discount on health insurance premiums."
                          .tr,
                      style: CupertinoTheme.of(context)
                          .textTheme
                          .textStyle
                          .copyWith(fontWeight: FontWeight.w600),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
                const SizedBox(height: 10),
                CupertinoButton.filled(
                  disabledColor: AppColor.blueColor,
                  padding:
                      const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
                  borderRadius: BorderRadius.circular(12),
                  child: Text(
                    "Would you like to proceed with health insurance?".tr,
                    style: const TextStyle(
                        fontSize: 16, fontWeight: FontWeight.bold),
                  ),
                  onPressed: () async {
                    // Show confirmation dialog before proceeding
                    showCupertinoDialog(
                      context: context,
                      builder: (BuildContext context) {
                        // Variable to store the health insurance provider chosen by the driver
                        TextEditingController providerController =
                            TextEditingController();

                        return CupertinoAlertDialog(
                          title: Text(
                            "Confirmation".tr,
                            style: const TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18),
                          ),
                          content: Column(
                            children: [
                              Text(
                                "Would you like to proceed with health insurance?"
                                    .tr,
                                style: const TextStyle(fontSize: 14),
                              ),
                              const SizedBox(height: 20),
                              CupertinoTextField(
                                controller: providerController,
                                placeholder:
                                    "Do you have a disease for a long time?".tr,
                                padding: const EdgeInsets.all(12),
                                decoration: BoxDecoration(
                                  border: Border.all(
                                      color: CupertinoColors.systemGrey,
                                      width: 1),
                                  borderRadius: BorderRadius.circular(8),
                                ),
                              ),
                            ],
                          ),
                          actions: <Widget>[
                            CupertinoDialogAction(
                              isDefaultAction: true,
                              child: Text(
                                "Yes".tr,
                                style: const TextStyle(
                                    color: CupertinoColors.activeBlue),
                              ),
                              onPressed: () async {
                                // Ensure the provider name is not empty
                                if (providerController.text.isNotEmpty) {
                                  // Call the function to insert data into the database
                                  await assuranceHealthController
                                      .addDriverHealthAssurance(
                                    healthInsuranceProvider:
                                        providerController.text,
                                  );

                                  // Close the dialog and navigate to a success screen or show a snackbar
                                  Navigator.of(context).pop();
                                } else {
                                  // Show an alert if the provider name is empty
                                  showCupertinoDialog(
                                    context: context,
                                    builder: (_) => CupertinoAlertDialog(
                                      title: Text("Error".tr),
                                      content: Text(
                                          "Do you have a disease for a long time?"
                                              .tr),
                                      actions: [
                                        CupertinoDialogAction(
                                          child: Text("OK".tr),
                                          onPressed: () =>
                                              Navigator.of(context).pop(),
                                        ),
                                      ],
                                    ),
                                  );
                                }
                              },
                            ),
                            CupertinoDialogAction(
                              child: Text(
                                "No".tr,
                                style: const TextStyle(
                                    color: CupertinoColors.destructiveRed),
                              ),
                              onPressed: () {
                                Navigator.of(context)
                                    .pop(); // Just close the dialog
                                // Optionally show feedback if the driver opts out
                                // Get.snackbar(
                                //     "Opted out".tr,
                                //     "You have chosen not to proceed with health insurance."
                                //         .tr,
                                //     backgroundColor:
                                //         CupertinoColors.systemGrey);
                                mySnackeBarError(
                                    "You have chosen not to proceed with health insurance."
                                        .tr);
                              },
                            ),
                          ],
                        );
                      },
                    );
                  },
                ),
              ],
            );
          }),
        ),
      ),
    );
  }

  Widget _buildTripCountAvatar(String count) {
    return Container(
      width: 80,
      height: 80,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        gradient: const RadialGradient(
          colors: [
            Color(0xFF42A5F5),
            Color(0xFF1976D2),
          ], // Health theme colors
          center: Alignment.center,
          radius: 0.8,
        ),
        boxShadow: [
          BoxShadow(
            color: CupertinoColors.black.withOpacity(0.2),
            blurRadius: 8,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Center(
        child: Text(
          count,
          style: const TextStyle(
            fontSize: 22,
            fontWeight: FontWeight.bold,
            color: CupertinoColors.white,
          ),
        ),
      ),
    );
  }
}
