import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../core/controllers/theme_controller.dart';
import '../../core/services/auth_service.dart';
import '../../core/utils/snackbar_util.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Settings'),
      ),
      body: ListView(
        padding: EdgeInsets.all(16.w),
        children: [
          // Theme Settings Section
          _buildSectionHeader('Appearance'),
          _buildThemeSettings(),
          SizedBox(height: 24.h),

          // Account Section
          _buildSectionHeader('Account'),
          _buildAccountSettings(),
          SizedBox(height: 24.h),

          // Notifications Section
          _buildSectionHeader('Notifications'),
          _buildNotificationSettings(),
          SizedBox(height: 24.h),

          // About Section
          _buildSectionHeader('About'),
          _buildAboutSettings(),
          SizedBox(height: 32.h),

          // Logout Button
          _buildLogoutButton(context),
        ],
      ),
    );
  }

  Widget _buildSectionHeader(String title) {
    return Padding(
      padding: EdgeInsets.only(left: 8.w, bottom: 12.h),
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w600,
          color: Colors.grey.shade600,
          letterSpacing: 0.5,
        ),
      ),
    );
  }

  Widget _buildThemeSettings() {
    return Card(
      child: Column(
        children: [
          // Theme Mode Selector
          Obx(() {
            return ListTile(
              leading: Container(
                padding: EdgeInsets.all(10.w),
                decoration: BoxDecoration(
                  color: Colors.blue.shade50,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Icon(
                  ThemeController.to.getThemeIcon(),
                  color: Colors.blue.shade600,
                  size: 24.w,
                ),
              ),
              title: Text(
                'Theme',
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
              ),
              subtitle: Text(
                ThemeController.to.getThemeModeString(),
                style: TextStyle(fontSize: 14.sp),
              ),
              trailing: Icon(Icons.arrow_forward_ios_rounded, size: 16.w),
              onTap: () => _showThemeDialog(),
            );
          }),

          Divider(height: 1.h, indent: 72.w),

          // Quick Theme Toggle
          Obx(() {
            return SwitchListTile(
              secondary: Container(
                padding: EdgeInsets.all(10.w),
                decoration: BoxDecoration(
                  color: Colors.orange.shade50,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Icon(
                  Icons.brightness_6_rounded,
                  color: Colors.orange.shade600,
                  size: 24.w,
                ),
              ),
              title: Text(
                'Dark Mode',
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
              ),
              subtitle: Text(
                'Override system settings',
                style: TextStyle(fontSize: 14.sp),
              ),
              value: ThemeController.to.isDarkMode,
              onChanged: (value) {
                ThemeController.to.toggleTheme();
              },
              activeColor: Colors.green.shade600,
            );
          }),
        ],
      ),
    );
  }

  Widget _buildAccountSettings() {
    return Card(
      child: Column(
        children: [
          // Profile
          Obx(() {
            final user = AuthService.to.currentUser.value;
            return ListTile(
              leading: Container(
                padding: EdgeInsets.all(10.w),
                decoration: BoxDecoration(
                  color: Colors.green.shade50,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Icon(
                  Icons.person_rounded,
                  color: Colors.green.shade600,
                  size: 24.w,
                ),
              ),
              title: Text(
                user?.fullName ?? 'User',
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
              ),
              subtitle: Text(
                user?.mobileNumber ?? '',
                style: TextStyle(fontSize: 14.sp),
              ),
              trailing: Icon(Icons.arrow_forward_ios_rounded, size: 16.w),
              onTap: () {
                SnackbarUtil.showInfo('Profile page coming soon');
              },
            );
          }),

          Divider(height: 1.h, indent: 72.w),

          // Change Password
          ListTile(
            leading: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.purple.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.lock_rounded,
                color: Colors.purple.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'Change Password',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            subtitle: Text(
              'Update your password',
              style: TextStyle(fontSize: 14.sp),
            ),
            trailing: Icon(Icons.arrow_forward_ios_rounded, size: 16.w),
            onTap: () {
              SnackbarUtil.showInfo('Change password feature coming soon');
            },
          ),
        ],
      ),
    );
  }

  Widget _buildNotificationSettings() {
    return Card(
      child: Column(
        children: [
          SwitchListTile(
            secondary: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.red.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.notifications_rounded,
                color: Colors.red.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'Push Notifications',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            subtitle: Text(
              'Receive push notifications',
              style: TextStyle(fontSize: 14.sp),
            ),
            value: true,
            onChanged: (value) {
              SnackbarUtil.showInfo('Notification settings coming soon');
            },
            activeColor: Colors.green.shade600,
          ),

          Divider(height: 1.h, indent: 72.w),

          SwitchListTile(
            secondary: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.orange.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.payment_rounded,
                color: Colors.orange.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'Payment Reminders',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            subtitle: Text(
              'Reminders for upcoming payments',
              style: TextStyle(fontSize: 14.sp),
            ),
            value: true,
            onChanged: (value) {
              SnackbarUtil.showInfo('Notification settings coming soon');
            },
            activeColor: Colors.green.shade600,
          ),

          Divider(height: 1.h, indent: 72.w),

          SwitchListTile(
            secondary: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.blue.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.casino_rounded,
                color: Colors.blue.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'Draw Notifications',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            subtitle: Text(
              'Alerts for lottery draws',
              style: TextStyle(fontSize: 14.sp),
            ),
            value: true,
            onChanged: (value) {
              SnackbarUtil.showInfo('Notification settings coming soon');
            },
            activeColor: Colors.green.shade600,
          ),
        ],
      ),
    );
  }

  Widget _buildAboutSettings() {
    return Card(
      child: Column(
        children: [
          ListTile(
            leading: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.blue.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.info_rounded,
                color: Colors.blue.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'App Version',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            subtitle: Text(
              '1.0.0',
              style: TextStyle(fontSize: 14.sp),
            ),
          ),

          Divider(height: 1.h, indent: 72.w),

          ListTile(
            leading: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.green.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.privacy_tip_rounded,
                color: Colors.green.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'Privacy Policy',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            trailing: Icon(Icons.arrow_forward_ios_rounded, size: 16.w),
            onTap: () {
              SnackbarUtil.showInfo('Privacy policy page coming soon');
            },
          ),

          Divider(height: 1.h, indent: 72.w),

          ListTile(
            leading: Container(
              padding: EdgeInsets.all(10.w),
              decoration: BoxDecoration(
                color: Colors.purple.shade50,
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Icon(
                Icons.description_rounded,
                color: Colors.purple.shade600,
                size: 24.w,
              ),
            ),
            title: Text(
              'Terms of Service',
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
            ),
            trailing: Icon(Icons.arrow_forward_ios_rounded, size: 16.w),
            onTap: () {
              SnackbarUtil.showInfo('Terms of service page coming soon');
            },
          ),
        ],
      ),
    );
  }

  Widget _buildLogoutButton(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: 52.h,
      child: ElevatedButton.icon(
        onPressed: () => _showLogoutDialog(context),
        icon: Icon(Icons.logout_rounded, size: 20.w),
        label: Text(
          'Logout',
          style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
        ),
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.red.shade600,
          foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(12.r),
          ),
        ),
      ),
    );
  }

  void _showThemeDialog() {
    Get.dialog(
      AlertDialog(
        title: const Text('Choose Theme'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            RadioListTile<ThemeMode>(
              title: const Text('Light'),
              value: ThemeMode.light,
              groupValue: ThemeController.to.themeMode,
              onChanged: (value) {
                ThemeController.to.setLightTheme();
                Get.back();
              },
              activeColor: Colors.green.shade600,
            ),
            RadioListTile<ThemeMode>(
              title: const Text('Dark'),
              value: ThemeMode.dark,
              groupValue: ThemeController.to.themeMode,
              onChanged: (value) {
                ThemeController.to.setDarkTheme();
                Get.back();
              },
              activeColor: Colors.green.shade600,
            ),
            RadioListTile<ThemeMode>(
              title: const Text('System Default'),
              value: ThemeMode.system,
              groupValue: ThemeController.to.themeMode,
              onChanged: (value) {
                ThemeController.to.setSystemTheme();
                Get.back();
              },
              activeColor: Colors.green.shade600,
            ),
          ],
        ),
      ),
    );
  }

  void _showLogoutDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('Logout'),
        content: const Text('Are you sure you want to logout?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text('Cancel'),
          ),
          ElevatedButton(
            onPressed: () {
              Navigator.of(context).pop();
              AuthService.to.logout();
              SnackbarUtil.showSuccess('Logged out successfully');
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.red,
              foregroundColor: Colors.white,
            ),
            child: const Text('Logout'),
          ),
        ],
      ),
    );
  }
}

