import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_driver/constant/colors.dart';
import 'package:siro_driver/controller/profile/setting_controller.dart';
import 'package:siro_driver/views/lang/languages.dart';
import 'package:siro_driver/views/widgets/my_scafold.dart';
import 'package:siro_driver/views/widgets/mydialoug.dart';

// تأكد من صحة مسارات الاستيراد هذه
import '../../../../controller/functions/vibrate.dart'; // Controller with isVibrate
import '../../../auth/country_widget.dart';
import 'about_us.dart';
import 'frequantly_question.dart';
import 'using_app_page.dart';

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

  @override
  Widget build(BuildContext context) {
    // تحميل الـ Controllers المطلوبة
    final settingsController = Get.put(SettingController());
    final homeController = Get.put(HomePageController());

    return MyScafolld(
      title: 'Settings'.tr,
      isleading: true,
      body: [
        ListView(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 20.0),
          children: <Widget>[
            // --- القسم الأول: عام ---
            _buildSectionHeader('General'.tr, context),
            _buildSettingsCard(
              children: [
                _buildListTile(
                  icon: Icons.language_outlined,
                  title: 'Language'.tr,
                  subtitle: 'Change the app language'.tr,
                  onTap: () => Get.to(() => const Language()),
                ),
                // _buildListTile(
                //   icon: Icons.flag_outlined,
                //   title: 'Change Country'.tr,
                //   subtitle: 'Get features for your country'.tr,
                //   onTap: () => Get.to(
                //     () => MyScafolld(
                //       title: 'Change Country'.tr,
                //       body: [CountryPickerFromSetting()],
                //       isleading: true,
                //     ),
                //   ),
                // ),
              ],
            ),
            const SizedBox(height: 20),

            // --- القسم الثاني: تفضيلات التطبيق ---
            _buildSectionHeader('App Preferences'.tr, context),
            _buildSettingsCard(
              children: [
                _buildSwitchTile(
                  icon: Icons.dark_mode_outlined,
                  title: 'App Dark Mode'.tr,
                  subtitle: 'Switch between light and dark themes'.tr,
                  controller: settingsController,
                  valueGetter: (ctrl) => (ctrl).isDarkMode,
                  onChanged: (ctrl) => (ctrl).toggleAppTheme(),
                ),
                _buildSwitchTile(
                  icon: Icons.map_outlined,
                  color: AppColor.redColor,
                  title: 'Google Map App'.tr,
                  subtitle: 'Run Google Maps directly'.tr,
                  controller: settingsController,
                  valueGetter: (ctrl) => (ctrl).isGoogleMapsEnabled,
                  onChanged: (ctrl) => (ctrl).onChangMapApp(),
                ),
                _buildSwitchTile(
                  icon: Icons.map_outlined,
                  title: 'Map Dark Mode'.tr,
                  subtitle: 'Switch between light and dark map styles'.tr,
                  controller: settingsController,
                  valueGetter: (ctrl) => (ctrl).isMapDarkMode,
                  onChanged: (ctrl) => (ctrl).toggleMapTheme(),
                ),

                _buildSwitchTile(
                  icon: Icons.vibration,
                  title: 'Vibration'.tr,
                  subtitle: 'Vibration feedback for buttons'.tr,
                  controller: homeController,
                  valueGetter: (ctrl) => (ctrl).isVibrate,
                  onChanged: (ctrl) => (ctrl)
                      .changeVibrateOption(true), // قد تحتاج لتعديل الدالة
                ),
              ],
            ),
            const SizedBox(height: 20),

            // --- القسم الثالث: المساعدة والدعم ---
            _buildSectionHeader('Help & Support'.tr, context),
            _buildSettingsCard(
              children: [
                _buildListTile(
                  icon: Icons.quiz_outlined,
                  title: 'Frequently Questions'.tr,
                  onTap: () => Get.to(() => const FrequentlyQuestionsPage()),
                ),
                _buildListTile(
                  icon: Icons.support_agent,
                  title: "How to use App".tr,
                  onTap: () => Get.to(() => const UsingAppPage()),
                ),
                _buildListTile(
                  icon: Icons.info_outline,
                  title: 'About Us'.tr,
                  onTap: () => Get.to(() => const AboutPage()),
                ),
              ],
            ),
            const SizedBox(height: 20),

            // --- القسم الرابع: تسجيل الخروج ---
            _buildSectionHeader('Account'.tr, context),
            _buildSettingsCard(
              children: [
                ListTile(
                  leading: const Icon(Icons.logout, color: Colors.red),
                  title: Text(
                    'Logout'.tr,
                    style: const TextStyle(
                        color: Colors.red, fontWeight: FontWeight.w500),
                  ),
                  onTap: () {
                    MyDialog().getDialog(
                      'Logout'.tr,
                      'Are you sure you want to logout?'.tr,
                      () {
                        // أضف دالة تسجيل الخروج هنا
                        Get.back(); // لإغلاق مربع الحوار
                      },
                      // isConfirmation: true,
                    );
                  },
                )
              ],
            )
          ],
        ),
      ],
    );
  }

  // ويدجت لبناء عنوان كل قسم
  Widget _buildSectionHeader(String title, BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 8.0, bottom: 12.0),
      child: Text(
        title,
        style: Theme.of(context).textTheme.titleSmall?.copyWith(
              color: Colors.grey.shade600,
              fontWeight: FontWeight.bold,
            ),
      ),
    );
  }

  // ويدجت لبناء بطاقة الإعدادات
  Widget _buildSettingsCard({required List<Widget> children}) {
    return Card(
      elevation: 2,
      shadowColor: Colors.black.withOpacity(0.1),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
      clipBehavior: Clip.antiAlias, // مهم لجعل splash effect داخل حدود البطاقة
      child: Column(children: children),
    );
  }

  // ويدجت لبناء عنصر قابل للضغط (مثل اللغة، عن التطبيق)
  Widget _buildListTile({
    required IconData icon,
    required String title,
    String? subtitle,
    required VoidCallback onTap,
  }) {
    return ListTile(
      leading: Icon(icon, color: Colors.grey.shade700),
      title: Text(title, style: const TextStyle(fontWeight: FontWeight.w500)),
      subtitle: subtitle != null ? Text(subtitle) : null,
      trailing:
          const Icon(Icons.arrow_forward_ios, size: 16, color: Colors.grey),
      onTap: onTap,
    );
  }

  // ويدجت لبناء عنصر يحتوي على مفتاح تفعيل/إلغاء (Switch)
  Widget _buildSwitchTile<T extends GetxController>({
    required IconData icon,
    Color? color,
    required String title,
    required String subtitle,
    required T controller,
    required bool Function(T) valueGetter,
    required Function(T) onChanged,
  }) {
    return GetBuilder<T>(
      init: controller,
      builder: (ctrl) {
        return SwitchListTile(
          secondary: Icon(icon, color: color ?? Colors.grey.shade700),
          title:
              Text(title, style: const TextStyle(fontWeight: FontWeight.w500)),
          subtitle: Text(subtitle),
          value: valueGetter(ctrl),
          onChanged: (value) => onChanged(ctrl),
          activeColor: AppColor.primaryColor,
        );
      },
    );
  }
}
