import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';
import '../services/flux_service.dart';
import 'feedback_screen.dart';
import 'about_screen.dart';

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

  void _showGoalDialog(BuildContext context) {
    final flux = context.read<FluxService>();
    double tempGoal = flux.dailyGoal;

    showDialog(
      context: context,
      builder: (context) => StatefulBuilder(
        builder: (context, setState) {
          return AlertDialog(
            title: const Text("Daily Target"),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  "${tempGoal.toInt()} ml",
                  style: GoogleFonts.nunito(
                    fontSize: 32,
                    fontWeight: FontWeight.bold,
                    color: const Color(0xFFAED581),
                  ),
                ),
                const SizedBox(height: 20),
                Slider(
                  value: tempGoal,
                  min: 1000,
                  max: 4000,
                  divisions: 30,
                  activeColor: const Color(0xFFAED581),
                  inactiveColor: const Color(0xFFFFF3E0),
                  onChanged: (val) => setState(() => tempGoal = val),
                ),
              ],
            ),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: Text(
                  "Cancel",
                  style: GoogleFonts.quicksand(color: Colors.grey),
                ),
              ),
              TextButton(
                onPressed: () {
                  flux.updateGoal(tempGoal);
                  Navigator.pop(context);
                },
                child: Text(
                  "Save",
                  style: GoogleFonts.quicksand(
                    fontWeight: FontWeight.bold,
                    color: const Color(0xFFAED581),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  void _showReminderDialog(BuildContext context) {
    final flux = context.read<FluxService>();
    bool tempEnabled = flux.remindersEnabled;

    showDialog(
      context: context,
      builder: (context) => StatefulBuilder(
        builder: (context, setState) {
          return AlertDialog(
            title: const Text("Gentle Reminders"),
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Icon(
                  Icons.notifications_active_rounded,
                  size: 48,
                  color: Color(0xFFFFCC80),
                ),
                const SizedBox(height: 16),
                Text(
                  "We'll gently nudge you every 2 hours if you haven't hydrated.",
                  textAlign: TextAlign.center,
                  style: GoogleFonts.quicksand(color: const Color(0xFF795548)),
                ),
                const SizedBox(height: 24),
                SwitchListTile(
                  title: Text(
                    "Enable Reminders",
                    style: GoogleFonts.quicksand(
                      fontWeight: FontWeight.bold,
                      color: const Color(0xFF5D4037),
                    ),
                  ),
                  value: tempEnabled,
                  activeColor: const Color(0xFFAED581),
                  onChanged: (val) => setState(() => tempEnabled = val),
                ),
              ],
            ),
            actions: [
              TextButton(
                onPressed: () {
                  flux.toggleReminder(tempEnabled);
                  Navigator.pop(context);
                },
                child: Text(
                  "Done",
                  style: GoogleFonts.quicksand(
                    fontWeight: FontWeight.bold,
                    color: const Color(0xFFAED581),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }

  void _openPrivacyPolicy(BuildContext context) async {
    const url = 'https://www.example.com/privacy_policy';
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text("Opening $url ...", style: GoogleFonts.quicksand()),
        backgroundColor: const Color(0xFFAED581),
        duration: const Duration(seconds: 2),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final flux = context.watch<FluxService>();

    return Scaffold(
      backgroundColor: const Color(0xFFFFFBF0),
      appBar: AppBar(
        title: Text(
          "Me",
          style: GoogleFonts.nunito(fontWeight: FontWeight.bold),
        ),
      ),
      body: ListView(
        padding: const EdgeInsets.all(24),
        children: [
          _buildSettingsCard(
            title: "Goals",
            children: [
              _buildRow(
                context,
                "Daily Target",
                "${flux.dailyGoal.toInt()} ml",
                onTap: () => _showGoalDialog(context),
              ),
              _buildRow(
                context,
                "Reminder",
                flux.remindersEnabled ? "On" : "Off",
                onTap: () => _showReminderDialog(context),
              ),
            ],
          ),
          const SizedBox(height: 20),
          _buildSettingsCard(
            title: "Support",
            children: [
              _buildRow(
                context,
                "Feedback",
                "Tell us",
                icon: Icons.chat_bubble_outline_rounded,
                onTap: () => Navigator.push(
                  context,
                  MaterialPageRoute(builder: (_) => const FeedbackScreen()),
                ),
              ),
              _buildRow(
                context,
                "Privacy Policy",
                "Read",
                icon: Icons.lock_outline_rounded,
                onTap: () => _openPrivacyPolicy(context),
              ),
              _buildRow(
                context,
                "About",
                "v1.0",
                icon: Icons.info_outline_rounded,
                onTap: () => Navigator.push(
                  context,
                  MaterialPageRoute(builder: (_) => const AboutScreen()),
                ),
              ),
              _buildRow(
                context,
                "Share App",
                "Friends",
                icon: Icons.share_rounded,
                onTap: () {
                  Share.share('Stay cozy and hydrated with FluxWater:Monitor!');
                },
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildSettingsCard({
    required String title,
    required List<Widget> children,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 8, bottom: 8),
          child: Text(
            title,
            style: GoogleFonts.nunito(
              fontWeight: FontWeight.bold,
              color: const Color(0xFFA1887F),
            ),
          ),
        ),
        Container(
          padding: const EdgeInsets.symmetric(vertical: 8),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(24),
            boxShadow: [
              BoxShadow(
                color: const Color(0xFF8D6E63).withOpacity(0.05),
                blurRadius: 10,
                offset: const Offset(0, 5),
              ),
            ],
          ),
          child: Column(children: children),
        ),
      ],
    );
  }

  Widget _buildRow(
    BuildContext context,
    String label,
    String value, {
    IconData? icon,
    VoidCallback? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
        child: Row(
          children: [
            if (icon != null) ...[
              Icon(icon, size: 20, color: const Color(0xFFBCAAA4)),
              const SizedBox(width: 12),
            ],
            Expanded(
              child: Text(
                label,
                style: GoogleFonts.quicksand(
                  color: const Color(0xFF5D4037),
                  fontSize: 16,
                ),
              ),
            ),
            Text(
              value,
              style: GoogleFonts.nunito(
                fontWeight: FontWeight.bold,
                color: const Color(0xFF8D6E63),
              ),
            ),
            const SizedBox(width: 8),
            const Icon(
              Icons.chevron_right_rounded,
              color: Color(0xFFE0E0E0),
              size: 20,
            ),
          ],
        ),
      ),
    );
  }
}
