import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../theme/app_theme.dart';

class FocusRatingSheet extends StatefulWidget {
  final ValueChanged<double> onSave;
  const FocusRatingSheet({super.key, required this.onSave});

  @override
  State<FocusRatingSheet> createState() => _FocusRatingSheetState();
}

class _FocusRatingSheetState extends State<FocusRatingSheet> {
  double _rating = 3.0;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(30),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(30)),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text(
            "Session Paused",
            style: TextStyle(
              fontSize: 22,
              fontWeight: FontWeight.w800,
              color: AppTheme.textMain,
            ),
          ),
          const SizedBox(height: 10),
          const Text(
            "How deep was your focus?",
            style: TextStyle(color: AppTheme.textSub, fontSize: 16),
          ),
          const SizedBox(height: 30),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: List.generate(5, (index) {
              return IconButton(
                onPressed: () => setState(() => _rating = index + 1.0),
                icon: Icon(
                  index < _rating
                      ? Icons.star_rounded
                      : Icons.star_outline_rounded,
                  color: Colors.amber,
                  size: 44,
                ),
              );
            }),
          ),
          const SizedBox(height: 30),
          SizedBox(
            width: double.infinity,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: AppTheme.primary,
                padding: const EdgeInsets.symmetric(vertical: 16),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16),
                ),
              ),
              onPressed: () {
                widget.onSave(_rating);
                Navigator.pop(context);
              },
              child: const Text(
                "Save Entry",
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.w800,
                  fontSize: 16,
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

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

  @override
  State<SoundMixerSheet> createState() => _SoundMixerSheetState();
}

class _SoundMixerSheetState extends State<SoundMixerSheet> {
  double volRain = 0.5;
  double volFire = 0.0;
  double volWind = 0.2;
  double volBirds = 0.0;

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: const BorderRadius.vertical(top: Radius.circular(30)),
      child: BackdropFilter(
        filter: ImageFilter.blur(sigmaX: 20, sigmaY: 20),
        child: Container(
          color: Colors.white.withOpacity(0.95),
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Center(
                child: Container(
                  width: 40,
                  height: 4,
                  decoration: BoxDecoration(
                    color: Colors.grey[400],
                    borderRadius: BorderRadius.circular(2),
                  ),
                ),
              ),
              const SizedBox(height: 20),
              const Text(
                "Ambient Mixer",
                style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.w800,
                  color: AppTheme.textMain,
                ),
              ),
              const SizedBox(height: 30),
              _buildSoundRow(
                Icons.water_drop_outlined,
                "Heavy Rain",
                volRain,
                (v) => setState(() => volRain = v),
              ),
              _buildSoundRow(
                Icons.local_fire_department_outlined,
                "Campfire",
                volFire,
                (v) => setState(() => volFire = v),
              ),
              _buildSoundRow(
                Icons.air,
                "Windy Forest",
                volWind,
                (v) => setState(() => volWind = v),
              ),
              _buildSoundRow(
                Icons.music_note_outlined,
                "Lo-Fi Beats",
                volBirds,
                (v) => setState(() => volBirds = v),
              ),
              const SizedBox(height: 30),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildSoundRow(
    IconData icon,
    String label,
    double value,
    ValueChanged<double> onChanged,
  ) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 12),
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: AppTheme.bgLight,
              borderRadius: BorderRadius.circular(12),
            ),
            child: Icon(icon, color: AppTheme.primary, size: 22),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Text(
              label,
              style: const TextStyle(
                fontWeight: FontWeight.w700,
                color: AppTheme.textMain,
                fontSize: 15,
              ),
            ),
          ),
          SizedBox(
            width: 150,
            child: CupertinoSlider(
              value: value,
              activeColor: AppTheme.primary,
              onChanged: onChanged,
            ),
          ),
        ],
      ),
    );
  }
}
