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';

import '../../widgets/my_scafold.dart';

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

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return MyScafolld(
      title: "Health Insurance".tr,
      isleading: true,
      body: [
        GetBuilder<AssuranceHealthController>(
          builder: (controller) {
            return SingleChildScrollView(
              padding: const EdgeInsets.all(20.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Text(
                    "When you complete 500 trips, you will be eligible for exclusive health insurance offers."
                        .tr,
                    textAlign: TextAlign.center,
                    style: theme.textTheme.titleMedium?.copyWith(
                      color:
                          theme.textTheme.bodyMedium?.color?.withOpacity(0.8),
                      height: 1.5,
                    ),
                  ),
                  const SizedBox(height: 24),
                  Card(
                    elevation: 4,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(16)),
                    child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Row(
                        children: [
                          Expanded(
                            child: ElevatedButton(
                              onPressed: () =>
                                  controller.getTripCountByCaptain(),
                              style: ElevatedButton.styleFrom(
                                backgroundColor: AppColor.primaryColor,
                                foregroundColor: Colors.white,
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(12)),
                                padding:
                                    const EdgeInsets.symmetric(vertical: 12),
                              ),
                              child: Text("Show My Trip Count".tr),
                            ),
                          ),
                          const SizedBox(width: 16),
                          _buildTripCountAvatar(
                            controller.tripCount['count'] == null
                                ? '0'
                                : controller.tripCount['count'].toString(),
                          ),
                        ],
                      ),
                    ),
                  ),
                  const SizedBox(height: 24),
                  Container(
                    padding: const EdgeInsets.all(18),
                    decoration: BoxDecoration(
                      color: theme.colorScheme.surfaceVariant.withOpacity(0.5),
                      borderRadius: BorderRadius.circular(12),
                      border: Border.all(color: theme.dividerColor),
                    ),
                    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: theme.textTheme.bodyLarge
                          ?.copyWith(fontWeight: FontWeight.w500),
                      textAlign: TextAlign.center,
                    ),
                  ),
                  const SizedBox(height: 32),
                  ElevatedButton(
                    onPressed: () => _showInsuranceDialog(context, controller),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColor.primaryColor,
                      foregroundColor: Colors.white,
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12)),
                    ),
                    child: Text(
                      "Would you like to proceed with health insurance?".tr,
                      style: const TextStyle(fontWeight: FontWeight.bold),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ],
    );
  }

  void _showInsuranceDialog(
      BuildContext context, AssuranceHealthController controller) {
    final TextEditingController providerController = TextEditingController();
    Get.dialog(
      AlertDialog(
        title: Text("Confirmation".tr),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text("Would you like to proceed with health insurance?".tr),
            const SizedBox(height: 16),
            TextField(
              controller: providerController,
              decoration: InputDecoration(
                hintText: "Do you have a disease for a long time?".tr,
                border:
                    OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
              ),
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text("No".tr, style: const TextStyle(color: Colors.red)),
          ),
          ElevatedButton(
            onPressed: () async {
              if (providerController.text.isNotEmpty) {
                await controller.addDriverHealthAssurance(
                  healthInsuranceProvider: providerController.text,
                );
                Get.back();
              } else {
                Get.snackbar("Error".tr,
                    "Please provide details about any long-term diseases.".tr);
              }
            },
            child: Text("Yes".tr),
          ),
        ],
      ),
    );
  }

  Widget _buildTripCountAvatar(String count) {
    return Container(
      width: 70,
      height: 70,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        gradient: const LinearGradient(
          colors: [Color(0xFF42A5F5), Color(0xFF1976D2)],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 8,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Center(
        child: Text(
          count,
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
