import 'package:flutter/material.dart';
import '../services/user_service.dart';

class NotificationsSettingsScreen extends StatefulWidget {
  const NotificationsSettingsScreen({super.key});

  @override
  _NotificationsSettingsScreenState createState() => _NotificationsSettingsScreenState();
}

class _NotificationsSettingsScreenState extends State<NotificationsSettingsScreen> {
  bool isLoading = true;
  bool isSaving = false;
  String? errorMessage;
  Map<String, dynamic>? userData;
  
  final Map<String, String> subscriptionLabels = {
    'newposts': 'New Posts',
    'postcomments': 'Post Comments',
    'newinvites': 'New Invitations',
    'invitesfollowup': 'Invitation Follow-ups',
    'appnews': 'App News',
  };
  
  Map<String, bool> subscriptionStates = {};

  @override
  void initState() {
    super.initState();
    _loadUserData();
  }

  Future<void> _loadUserData() async {
    setState(() {
      isLoading = true;
      errorMessage = null;
    });

    final result = await UserService.getCurrentUser();

    if (mounted) {
      setState(() {
        isLoading = false;
        if (result['success'] == true) {
          userData = result['data'];
          _initializeSubscriptions();
        } else {
          errorMessage = result['message'] ?? 'Failed to load user data';
        }
      });
    }
  }

  void _initializeSubscriptions() {
    List<String> userSubscriptions = [];
    
    if (userData != null && userData!['subscriptions'] != null) {
      userSubscriptions = List<String>.from(userData!['subscriptions']);
    }
    
    for (String key in subscriptionLabels.keys) {
      subscriptionStates[key] = userSubscriptions.contains(key);
    }
  }

  Future<void> _saveSettings() async {
    setState(() {
      isSaving = true;
      errorMessage = null;
    });

    List<String> activeSubscriptions = subscriptionStates.entries
        .where((entry) => entry.value)
        .map((entry) => entry.key)
        .toList();

    final result = await UserService.updateUserSubscriptions(activeSubscriptions);

    if (mounted) {
      setState(() {
        isSaving = false;
      });

      if (result['success'] == true) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Notification settings saved'),
            backgroundColor: Color(0xFF6A4C93),
          ),
        );
        Navigator.of(context).pop();
      } else {
        setState(() {
          errorMessage = result['message'] ?? 'Failed to save settings';
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Notifications', style: TextStyle(fontWeight: FontWeight.w600)),
        backgroundColor: Colors.white,
        foregroundColor: Color(0xFF6A4C93),
        elevation: 0,
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(1),
          child: Container(height: 1, color: Colors.grey[200]),
        ),
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (isLoading) ...[
              Expanded(
                child: Center(
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF6A4C93)),
                  ),
                ),
              ),
            ] else if (errorMessage != null && userData == null) ...[
              Expanded(
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.error_outline,
                        size: 50,
                        color: Colors.grey[400],
                      ),
                      SizedBox(height: 16),
                      Text(
                        errorMessage!,
                        style: TextStyle(fontSize: 16, color: Colors.grey[600]),
                        textAlign: TextAlign.center,
                      ),
                      SizedBox(height: 16),
                      ElevatedButton(
                        onPressed: _loadUserData,
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Color(0xFF6A4C93),
                          foregroundColor: Colors.white,
                        ),
                        child: Text('Retry'),
                      ),
                    ],
                  ),
                ),
              ),
            ] else ...[
              Text(
                'Choose which notifications you want to receive:',
                style: TextStyle(
                  fontSize: 16,
                  color: Colors.grey[700],
                ),
              ),
              SizedBox(height: 24),
              
              Expanded(
                child: ListView(
                  children: subscriptionLabels.entries.map((entry) {
                    return Container(
                      margin: EdgeInsets.only(bottom: 8),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey[300]!),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: SwitchListTile(
                        title: Text(
                          entry.value,
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        value: subscriptionStates[entry.key] ?? false,
                        onChanged: (bool value) {
                          setState(() {
                            subscriptionStates[entry.key] = value;
                          });
                        },
                        activeColor: Color(0xFF6A4C93),
                        contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                      ),
                    );
                  }).toList(),
                ),
              ),
              
              if (errorMessage != null) ...[
                Container(
                  padding: EdgeInsets.all(12),
                  margin: EdgeInsets.only(bottom: 16),
                  decoration: BoxDecoration(
                    color: Colors.red[50],
                    border: Border.all(color: Colors.red[300]!),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Row(
                    children: [
                      Icon(Icons.error_outline, color: Colors.red[700], size: 20),
                      SizedBox(width: 8),
                      Expanded(
                        child: Text(
                          errorMessage!,
                          style: TextStyle(color: Colors.red[700]),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
              
              Container(
                width: double.infinity,
                height: 56,
                child: ElevatedButton(
                  onPressed: isSaving ? null : _saveSettings,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Color(0xFF6A4C93),
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: isSaving
                      ? SizedBox(
                          width: 20,
                          height: 20,
                          child: CircularProgressIndicator(
                            strokeWidth: 2,
                            valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                          ),
                        )
                      : Text(
                          'Save Settings',
                          style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
                        ),
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }
}