import 'package:Intaleq/controller/home/home_page_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:Intaleq/views/lang/languages.dart';

import 'HomePage/about_page.dart';
import 'HomePage/frequentlyQuestionsPage.dart';
import 'HomePage/share_app_page.dart';
import 'HomePage/trip_record_page.dart';

// NOTE: This is a placeholder for your actual CountryPickerFromSetting widget.
// You should remove this and import your own widget.
class CountryPickerFromSetting extends StatelessWidget {
  const CountryPickerFromSetting({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Change Country'.tr)),
      body: Center(
        child: Text('Country Picker Page Placeholder'.tr),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    // Using lazyPut to ensure the controller is available when needed.
    Get.lazyPut(() => HomePageController());

    return Scaffold(
      backgroundColor:
          const Color(0xFFF5F5F7), // A slightly off-white background
      appBar: AppBar(
        title: Text('Setting'.tr,
            style: const TextStyle(
                color: Colors.black87, fontWeight: FontWeight.bold)),
        backgroundColor: Colors.white,
        elevation: 0.5,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new, color: Colors.black87),
          onPressed: () => Get.back(),
        ),
      ),
      body: SafeArea(
        child: ListView(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 20.0),
          children: [
            _buildSectionHeader('General'.tr),
            _buildSettingsCard(
              children: [
                _buildSettingsTile(
                  icon: Icons.language,
                  color: Colors.blue,
                  title: 'Language'.tr,
                  subtitle: 'To change Language the App'.tr,
                  onTap: () => Get.to(() => const Language()),
                ),
                // const Divider(height: 1, indent: 68, endIndent: 16),
                // _buildSettingsTile(
                //   icon: Icons.map_outlined,
                //   color: Colors.green,
                //   title: 'Change Country'.tr,
                //   subtitle: 'You can change the Country to get all features'.tr,
                //   onTap: () => Get.to(() => const CountryPickerFromSetting()),
                // ),
              ],
            ),
            const SizedBox(height: 24),
            _buildSectionHeader('Preferences'.tr),
            _buildSettingsCard(
              children: [
                GetBuilder<HomePageController>(
                  builder: (controller) {
                    return _buildSettingsSwitchTile(
                      icon: Icons.vibration,
                      color: Colors.purple,
                      title: 'Vibration'.tr,
                      subtitle: 'Vibration feedback for all buttons'.tr,
                      value: controller.isVibrate,
                      onChanged: controller.changeVibrateOption,
                    );
                  },
                ),
                const Divider(height: 1, indent: 68, endIndent: 16),
                _buildSettingsTile(
                  icon: Icons.mic_none,
                  color: Colors.orange,
                  title: 'Trips recorded'.tr,
                  subtitle: 'Here recorded trips audio'.tr,
                  onTap: () => Get.to(() => const TripsRecordedPage()),
                ),
              ],
            ),
            const SizedBox(height: 24),
            _buildSectionHeader('Support & Info'.tr),
            _buildSettingsCard(
              children: [
                _buildSettingsTile(
                  icon: Icons.help_outline,
                  color: Colors.cyan,
                  title: 'Frequently Questions'.tr,
                  subtitle: 'Find answers to common questions'.tr,
                  onTap: () => Get.to(() => const FrequentlyQuestionsPage()),
                ),
                const Divider(height: 1, indent: 68, endIndent: 16),
                _buildSettingsTile(
                  icon: Icons.info_outline,
                  color: Colors.indigo,
                  title: 'About Us'.tr,
                  subtitle: 'Learn more about our app and mission'.tr,
                  onTap: () => Get.to(() => const AboutPage()),
                ),
                const Divider(height: 1, indent: 68, endIndent: 16),
                _buildSettingsTile(
                  icon: Icons.share_outlined,
                  color: Colors.redAccent,
                  title: 'Share App'.tr,
                  subtitle: 'Share with friends and earn rewards'.tr,
                  onTap: () => Get.to(() => ShareAppPage()),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSectionHeader(String title) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12.0, left: 8.0),
      child: Text(
        title,
        style: TextStyle(
          color: Colors.grey[700],
          fontWeight: FontWeight.bold,
          fontSize: 15,
        ),
      ),
    );
  }

  Widget _buildSettingsCard({required List<Widget> children}) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      clipBehavior: Clip.antiAlias,
      child: Column(
        children: children,
      ),
    );
  }

  Widget _buildSettingsTile({
    required IconData icon,
    required Color color,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return ListTile(
      onTap: onTap,
      leading: Container(
        padding: const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: color.withOpacity(0.1),
          borderRadius: BorderRadius.circular(10),
        ),
        child: Icon(icon, color: color, size: 22),
      ),
      title: Text(title,
          style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 16)),
      subtitle: Text(subtitle,
          style: TextStyle(color: Colors.grey[600], fontSize: 13)),
      trailing: Icon(Icons.chevron_right, color: Colors.grey[400]),
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
    );
  }

  Widget _buildSettingsSwitchTile({
    required IconData icon,
    required Color color,
    required String title,
    required String subtitle,
    required bool value,
    required ValueChanged<bool> onChanged,
  }) {
    return SwitchListTile(
      secondary: Container(
        padding: const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: color.withOpacity(0.1),
          borderRadius: BorderRadius.circular(10),
        ),
        child: Icon(icon, color: color, size: 22),
      ),
      title: Text(title,
          style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 16)),
      subtitle: Text(subtitle,
          style: TextStyle(color: Colors.grey[600], fontSize: 13)),
      value: value,
      onChanged: onChanged,
      activeColor: const Color(0xFF007AFF), // iOS-like blue
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
    );
  }
}
