import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/session_manager.dart';
import '../services/settings_service.dart';
import '../utils/discipline_challenge.dart';

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

  @override
  State<SessionModal> createState() => _SessionModalState();
}

class _SessionModalState extends State<SessionModal> {
  double _customMinutes = 5.0;

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

    return Container(
      padding: const EdgeInsets.all(24),
      decoration: const BoxDecoration(
        color: Color(0xFF121212),
        borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text(
                'Start Reel Session',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
              IconButton(
                onPressed: () => Navigator.pop(context),
                icon: const Icon(Icons.close, color: Colors.white54),
              ),
            ],
          ),
          const SizedBox(height: 8),
          Text(
            'Remaining Daily: ${sm.dailyRemainingSeconds ~/ 60}m',
            style: const TextStyle(color: Colors.white70),
          ),
          if (sm.isCooldownActive)
            Padding(
              padding: const EdgeInsets.only(top: 8.0),
              child: Text(
                'Cooldown active: ${sm.cooldownRemainingSeconds ~/ 60}m ${sm.cooldownRemainingSeconds % 60}s left',
                style: const TextStyle(color: Colors.orangeAccent),
              ),
            ),
          const SizedBox(height: 24),
          const Text(
            'Presets',
            style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
          ),
          const SizedBox(height: 12),
          Wrap(
            spacing: 8,
            runSpacing: 8,
            children: [1, 3, 5, 10, 15, 20, 30].map((m) {
              return SizedBox(
                width: 72,
                child: ElevatedButton(
                  onPressed: (sm.isCooldownActive || sm.isDailyLimitExhausted)
                      ? null
                      : () => _start(m),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.white12,
                    foregroundColor: Colors.white,
                    padding: const EdgeInsets.symmetric(vertical: 12),
                  ),
                  child: Text('${m}m'),
                ),
              );
            }).toList(),
          ),
          const SizedBox(height: 32),
          const Text(
            'Custom Duration',
            style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
          ),
          Slider(
            value: _customMinutes,
            min: 1,
            max: 60,
            divisions: 59,
            label: '${_customMinutes.toInt()}m',
            onChanged: (v) => setState(() => _customMinutes = v),
          ),
          const SizedBox(height: 24),
          SizedBox(
            width: double.infinity,
            height: 50,
            child: ElevatedButton(
              onPressed: (sm.isCooldownActive || sm.isDailyLimitExhausted)
                  ? null
                  : () => _start(_customMinutes.toInt()),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                foregroundColor: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              child: const Text(
                'Start Session',
                style: TextStyle(fontSize: 16),
              ),
            ),
          ),
          const SizedBox(height: 16),
        ],
      ),
    );
  }

  void _start(int minutes) async {
    final sm = context.read<SessionManager>();
    final settings = context.read<SettingsService>();

    if (settings.requireWordChallenge) {
      final success = await DisciplineChallenge.show(
        context,
        count: settings.resolvedWordChallengeCount(),
      );
      if (!success) return;
    }

    if (sm.startSession(minutes)) {
      if (mounted) Navigator.pop(context);
    }
  }
}
